Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0f4ca4

Browse files
author
chenchangqin
committed
develop#PHP开发模式
1 parent 94a1e5e commit c0f4ca4

38 files changed

+1125
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
composer.lock

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:26
7+
*/
8+
9+
namespace DesignerPatterns\Builder;
10+
11+
12+
class BikeBuilder implements BuilderInterface
13+
{
14+
/**
15+
* @var Parts\Bike
16+
*/
17+
protected $bike;
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function addDoors()
23+
{
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function addEngine()
30+
{
31+
$this->bike->setPart('engine', new Parts\Engine());
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function addWheel()
38+
{
39+
$this->bike->setPart('forwardWheel', new Parts\Wheel());
40+
$this->bike->setPart('rearWheel', new Parts\Wheel());
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function createVehicle()
47+
{
48+
$this->bike = new Parts\Bike();
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function getVehicle()
55+
{
56+
return $this->bike;
57+
}
58+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:24
7+
*/
8+
9+
namespace DesignerPatterns\Builder;
10+
11+
/**
12+
* 建造者接口
13+
*/
14+
15+
interface BuilderInterface
16+
{
17+
/**
18+
* @return mixed
19+
*/
20+
public function createVehicle();
21+
22+
/**
23+
* @return mixed
24+
*/
25+
public function addWheel();
26+
27+
/**
28+
* @return mixed
29+
*/
30+
public function addEngine();
31+
32+
/**
33+
* @return mixed
34+
*/
35+
public function addDoors();
36+
37+
/**
38+
* @return mixed
39+
*/
40+
public function getVehicle();
41+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:26
7+
*/
8+
9+
namespace DesignerPatterns\Builder;
10+
11+
12+
class CarBuilder implements BuilderInterface
13+
{
14+
/**
15+
* @var Parts\Car
16+
*/
17+
protected $car;
18+
19+
/**
20+
* @return void
21+
*/
22+
public function addDoors()
23+
{
24+
$this->car->setPart('rightdoor', new Parts\Door());
25+
$this->car->setPart('leftDoor', new Parts\Door());
26+
}
27+
28+
/**
29+
* @return void
30+
*/
31+
public function addEngine()
32+
{
33+
$this->car->setPart('engine', new Parts\Engine());
34+
}
35+
36+
/**
37+
* @return void
38+
*/
39+
public function addWheel()
40+
{
41+
$this->car->setPart('wheelLF', new Parts\Wheel());
42+
$this->car->setPart('wheelRF', new Parts\Wheel());
43+
$this->car->setPart('wheelLR', new Parts\Wheel());
44+
$this->car->setPart('wheelRR', new Parts\Wheel());
45+
}
46+
47+
/**
48+
* @return void
49+
*/
50+
public function createVehicle()
51+
{
52+
$this->car = new Parts\Car();
53+
}
54+
55+
/**
56+
* @return Parts\Car
57+
*/
58+
public function getVehicle()
59+
{
60+
return $this->car;
61+
}
62+
}

DesignerPatterns/Builder/Director.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:23
7+
*/
8+
9+
namespace DesignerPatterns\Builder;
10+
/**
11+
* Director 是建造者模式的一部分,它知道建造者接口并通过建造者构建复杂对象。
12+
*
13+
* 可以通过依赖注入建造者的方式构造任何复杂对象
14+
*/
15+
16+
class Director
17+
{
18+
/**
19+
* “导演”并不知道具体实现细节
20+
*
21+
* @param BuilderInterface $builder
22+
*
23+
* @return Parts\Vehicle
24+
*/
25+
public function build(BuilderInterface $builder)
26+
{
27+
$builder->createVehicle();
28+
$builder->addDoors();
29+
$builder->addEngine();
30+
$builder->addWheel();
31+
32+
return $builder->getVehicle();
33+
}
34+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:27
7+
*/
8+
9+
namespace DesignerPatterns\Builder\Parts;
10+
11+
12+
class Bike extends Vehicle
13+
{
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:27
7+
*/
8+
9+
namespace DesignerPatterns\Builder\Parts;
10+
11+
12+
class Car extends Vehicle
13+
{
14+
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:28
7+
*/
8+
9+
namespace DesignerPatterns\Builder\Parts;
10+
11+
/**
12+
* Door类
13+
*/
14+
15+
class Door
16+
{
17+
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:28
7+
*/
8+
9+
namespace DesignerPatterns\Builder\Parts;
10+
11+
/**
12+
* Engine类
13+
*/
14+
class Engine
15+
{
16+
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:27
7+
*/
8+
9+
namespace DesignerPatterns\Builder\Parts;
10+
11+
12+
abstract class Vehicle
13+
{
14+
/**
15+
* @var array
16+
*/
17+
protected $data;
18+
19+
/**
20+
* @param string $key
21+
* @param mixed $value
22+
*/
23+
public function setPart($key, $value)
24+
{
25+
$this->data[$key] = $value;
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 15:28
7+
*/
8+
9+
namespace DesignerPatterns\Builder\Parts;
10+
11+
/**
12+
* Wheel类
13+
*/
14+
class Wheel
15+
{
16+
17+
}

DesignerPatterns/Builder/Readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# PHP 设计模式系列 —— 建造者模式(Builder)
2+
## 1、模式定义
3+
建造者模式将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
4+
5+
## 2、问题引出
6+
假设我们有个生产车的工厂,可以制造各种车,比如自行车、汽车、卡车等等,如果每辆车都是从头到尾按部就班地造,必然效率低下。
7+
8+
## 3、解决办法
9+
我们可以试着将车的组装和零部件生产分离开来:让一个类似“导演”的角色负责车子组装,而具体造什么样的车需要什么样的零部件让具体的“构造者”去实现,“导演”知道什么样的车怎么造,需要的零部件则让“构造者”去建造,何时完成由“导演”来控制并最终返回给客户端。
10+
11+
## 4、UML类图
12+
![Builder-UML.png](/static/images/Builder-UML.png)
13+
14+
## 5、总结
15+
建造者模式和抽象工厂模式很像,总体上,建造者模式仅仅只比抽象工厂模式多了一个“导演类”的角色。与抽象工厂模式相比,建造者模式一般用来创建更为复杂的对象,因为对象的创建过程更为复杂,因此将对象的创建过程独立出来组成一个新的类 —— 导演类。也就是说,抽像工厂模式是将对象的全部创建过程封装在工厂类中,由工厂类向客户端提供最终的产品;而建造者模式中,建造者类一般只提供产品类中各个组件的建造,而将完整建造过程交付给导演类。由导演类负责将各个组件按照特定的规则组建为产品,然后将组建好的产品交付给客户端。
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/19
6+
* Time: 14:10
7+
*/
8+
9+
namespace DesignerPatterns\DependencyInjection;
10+
11+
12+
abstract class AbstractConfig
13+
{
14+
/**
15+
* @var Storage of data
16+
*/
17+
protected $storage;
18+
19+
public function __construct($storage)
20+
{
21+
$this->storage = $storage;
22+
}
23+
}

0 commit comments

Comments
 (0)