-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstractFactory.php
More file actions
47 lines (41 loc) · 841 Bytes
/
abstractFactory.php
File metadata and controls
47 lines (41 loc) · 841 Bytes
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
<?php
//Abstract Factory Design
class Truck {
function getName(){
echo "Truck";
}
}
class Car {
function getName(){
echo "Car";
}
}
interface VFactory {
function create();
}
class CarFactory implements VFactory {
function create(){
return new Car();
}
}
class TruckFactory implements VFactory {
function create(){
return new Truck();
}
}
class VehicleFactory {
function getVehicle($vehicleType='car'){
if('car' == $vehicleType){
return new CarFactory();
}elseif('truck' == $vehicleType){
return new TruckFactory();
}
}
}
$vf = new VehicleFactory();
$cf = $vf->getVehicle('car');
$tf = $vf->getVehicle('truck');
$car = $cf->create();
echo $car->getName().PHP_EOL;
$truck = $tf->create();
echo $truck->getName();