-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyAbstractFactory.php
66 lines (53 loc) · 1.18 KB
/
MyAbstractFactory.php
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
<?php
// Abstract Factory pattern
// App must create new tank or airplane and move it
// Author Makarenkov Michael
// Interface of abstarct factory
interface UnitFactory {
public function create();
}
// Factory of tanks
class TankFactory implements UnitFactory {
public function create() {
return new Tank();
}
}
// Factory of airpalne
class AirFactory implements UnitFactory {
public function create() {
return new Air();
}
}
// Abstract class for unit
abstract class Unit {
protected $name;
public function moved() {
echo 'moved '.$this->name;
}
}
class Tank extends Unit {
public function __construct() {
$this->name = 'tank';
}
}
class Air extends Unit {
public function __construct() {
$this->name = 'air';
}
}
// Creation object from factory
class Application {
public function __construct(UnitFactory $obj) {
$unit = $obj->create();
$unit->moved();
}
}
// Application loader
class ApplicationRunner {
public static function run() {
$tank = new TankFactory();
//$air = new AirFactory();
new Application($air);
}
}
ApplicationRunner::run();