-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarFactoryPattern.php
More file actions
87 lines (68 loc) · 1.64 KB
/
CarFactoryPattern.php
File metadata and controls
87 lines (68 loc) · 1.64 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* Created by PhpStorm.
* User: kin.salayon
* Date: 25/01/2018
* Time: 4:34 PM
*/
/*
Summary of Factory Pattern Structure
abstract class Creator
contains the actual implementation of the object creation (e.g create method)
contains interface method to impose create method (e.g. factoryMethod)
class Factory extends Creator
contains methods creature method to be used by Creator's create method
e.g. protected function factoryMethod(ICar $car)
$car->show();
interface ICreature - Interface to group types of creations and enforces create method
e.g. function show();
class CreatureOne implements ICreature
class CreatureTwo implements ICreature
- Object to be created
class Client
- Uses abstract Creator to create creatures objects
$creator = new Factory();
$creator->create(new CreatureOne);
$creator->create(new CreatureTwo);
*/
abstract class CarCreator
{
protected abstract function factoryMethod(ICar $car);
public function create(ICar $car)
{
return $this->factoryMethod($car);
}
}
class CarFactory extends CarCreator
{
protected function factoryMethod(ICar $car)
{
$car->show();
}
}
interface ICar
{
function show();
}
class Toyota implements ICar
{
public function show(){
echo 'Im a Toyota!';
}
}
class Honda implements ICar
{
public function show(){
echo 'Im a Honda!';
}
}
class Client
{
function __construct()
{
$factory = new CarFactory();
$factory->create(new Toyota());
$factory->create(new Honda());
}
}
$worker = new Client();