Factory

Factory Patterns are normally use to abstract away the logic for create a product.
i.e creating a product by passing through some param defer the creation of different types object.

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

interface IFactory() {
public function createProduct($productType);
}

class factory implements IFactory {

public function createProduct($productType) : productParent {

switch ($productType) {
case 'A':
return new productA();
break;
case 'B':
return new productB();
break;
default:
throw new Exception("product type is not defined");
}
}
}

abstract class AProductParent {

abstract public function doSomething();
}

class productA extends AProductParent{

public function doSomething() {
echo "do somehting a";
}
}

class productB extends AProductParent{

public function doSomething() {

echo "do somehting b";
}
}



 Previous
Reflection Reflection
What I learn from a collapse of a startup.For the last two years I have been working in a startup named DSYNC. It provid
2019-05-20 Ray Zhang
Next 
Decorator Decorator
Decorator Pattern Decorator AddOnDecoractor is-A Beverage SolidCoffeeCLass is-A Beverage Decorator is-A AddOnDecoractor
2019-05-15 Ray Zhang
  TOC