0%

PHP知识点整理(三) - 工厂模式

1. 简单工厂模式

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
<?php

interface Math
{
function calc($num1, $num2);
}

class Add implements Math
{
public function calc($num1, $num2)
{
// TODO: Implement calc() method.
return $num1 + $num2;
}
}

class Sub implements Math
{
public function calc($num1, $num2)
{
// TODO: Implement calc() method.
return $num1 - $num2;
}
}

class Cal
{
public static function getMath($type)
{
switch ($type) {
case "+" :
return new Add();
break;
case "-" :
return new Sub();
break;
default :
die("不支持其他计算方法");
}
}
}

$math = Cal::getMath("+");
var_dump($math->calc(1,2));
$math2 = Cal::getMath("-");
var_dump($math2->calc(3,2));

好处就是,只需要管理getMath里面的类型就行了,不需要管如何实现的,但是缺点就是,当需要实现的方法较多时就比较麻烦,每增加一种,就需要增加一个类型。

2. 工厂模式

定义工厂的接口,让工厂的子类来确定实例化哪一个具体的产品类,延迟了类的实例化。

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
<?php
namespace FactoryMethod2;

//父类
abstract class Math
{
abstract public function create();
}
//操作类
abstract class Operation
{
protected $num1 = 0;

protected $num2 = 0;

public function setNum1($num1){
$this->num1 = $num1;
}

public function setNum2($num2){
$this->num2 = $num2;
}

abstract public function calc();
}

class Add extends Operation
{

public function calc(){
return $this->num1 + $this->num2;
}
}

class Sub extends Operation
{
public function calc(){
return $this->num1 - $this->num2;
}
}
//工厂类
class AddFactory extends Math
{
public function create()
{
// TODO: Implement create() method.
return new Add();
}
}
//工厂类
class SubFactory extends Math
{
public function create()
{
// TODO: Implement create() method.
return new Sub();
}
}

$factory1 = new AddFactory();
$math1 = $factory1->create();
$math1->setNum1(1);
$math1->setNum2(2);
var_dump($math1->calc());

$factory2 = new SubFactory();
$math2 = $factory2->create();
$math2->setNum1(2);
$math2->setNum2(1);
var_dump($math2->calc());
?>

3. 抽象工厂模式

抽象工厂模式和工厂模式最大的区别就是把对象的创建抽象成了接口

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
<?php
namespace FactoryMethod3;


interface User
{
public function insert();

public function select();
}

interface Factory
{
public function createUser();

public function createArticle();
}

class MySqlUser implements User
{
public function insert()
{
// TODO: Implement insert() method.
}

public function select()
{
// TODO: Implement select() method.
}
}

class MySqlArticle implements User
{
public function select()
{
// TODO: Implement select() method.
}

public function insert()
{
// TODO: Implement insert() method.
}
}

class MySqlFactory implements Factory
{
public function createUser()
{
// TODO: Implement createUser() method.
return new MySqlUser();
}

public function createArticle()
{
// TODO: Implement createArticle() method.
return new MySqlArticle();
}
}
$factory1 = new MySqlFactory();

$user = $factory1->createUser();
$user->insert();
$user->select();

$article = $factory1->createArticle();
$article->select();
$article->insert();
?>

总结

  1. 简单工厂模式一般用于生产同一类产品,操作相同的,对于新的产品类就比较麻烦
  2. 工厂模式一般用于生产同一结构的固定产品,对于新增产品也比较支持
  3. 抽象工厂模式可以生产不同产品族的全部产品,但也对新增产品无能为力

Welcome to my other publishing channels