Onga inc

subtitle

php-desain-factory

php factory パターン

Design Patterns
http://www.phptherightway.com/pages/Design-Patterns.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

Interface MakerInfo
{

public function getMaker();
public function getDateReleased();
}

class SmartPhone implements MakerInfo
{

private $model;
private $maker;
private $date_released;

public function __construct($maker, $model, $date_released)
{

$this->model = $model;
$this->maker = $maker;
$this->date_released = $date_released;
}

public function getModel()
{

return $this->model;
}

public function getMaker()

{

return $this->maker;
}

public function getDateReleased()
{

return $this->date_released;
}
}

class SmartPhoneFactory
{

public static function create($model, $maker, $date_released)
{

return new SmartPhone($model, $maker);
}
}

$iPhone5 = SmartPhoneFactory::create('iPhone5', 'Apple', '2012年9月12日');
print_r($iPhone5->getModel.':'.$iPhone5->getMaker.':'.$iPhone5->getDateReleased);
//iphone5:Apple:2012年9月12日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

class Automobile
{

private $vehicleMake;
private $vehicleModel;

public function __construct($make, $model)
{

$this->vehicleMake = $make;
$this->vehicleModel = $model;
}

public function getMakeAndModel()
{

return $this->vehicleMake . ' ' . $this->vehicleModel;
}
}

class AutomobileFactory
{

public static function create($make, $model)
{

return new Automobile($make, $model);
}
}

// have the factory create the Automobile object
$veyron = AutomobileFactory::create('Bugatti', 'Veyron');

print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron"

Onga

A designer, developer and entrepreneur. Spends his time travelling the world with a bag of kites. Likes journalism and publishing platforms.

Comments