-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstractFactory.php
54 lines (44 loc) · 996 Bytes
/
abstractFactory.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
<?php
interface ProductFactory
{
public function make(string $name): Product;
}
interface Product
{
public function getMethodName(): string;
}
class ProductFactoryImplementation implements ProductFactory
{
/**
* @throws Exception
*/
public function make(string $name): Product
{
return match ($name) {
ProductA::class => new ProductA(),
ProductB::class => new ProductB(),
default => throw new Exception('Wrong product name!'),
};
}
}
class ProductA implements Product
{
public function getMethodName(): string
{
return __METHOD__;
}
}
class ProductB implements Product
{
public function getMethodName(): string
{
return __METHOD__;
}
}
/**
* Client
*/
$factory = new ProductFactoryImplementation();
$productA = $factory->make(ProductA::class);
$productB = $factory->make(ProductB::class);
echo $productA->getMethodName(), '<br>', $productB->getMethodName();