原文 https://github.com/kamranahmedse/design-patterns-for-humans
作者用简练的语句概括几个设计模式的场景和用法,比看长篇大论心情舒畅多了,简要翻译一下加深记忆 #介绍 设计模式是对某种特定问题的解决之道 #注意 设计模式不是银弹
#设计模式的类型
- 创造型
- 结构型
- 行为型
#创造性设计模式 创造性设计模式专注于如何表达一个object或一组相关的object,通过从某种程度上控制这些object的创造
- 简单工厂模式
- 工厂模式
- 抽象工厂模式
- 建造者模式
- 原型模式
- 单例模式
##抽象工厂模式
创建工厂们的工厂,表达一类各自独立但又相关的工厂的抽象
工厂模式是创建拥有相同方法的实例,抽象工厂模式是创建拥有相同方法的工厂
###代码实例
在创建门的同时,需要创建于门相对应的工匠
interface Door {
public function getDescription();
}
class WoodenDoor implements Door {
public function getDescription() {
echo 'I am a wooden door';
}
}
class IronDoor implements Door {
public function getDescription() {
echo 'I am an iron door';
}
}
interface DoorFittingExpert {
public function getDescription();
}
class Welder implements DoorFittingExpert {
public function getDescription() {
echo 'I can only fit iron doors';
}
}
class Carpenter implements DoorFittingExpert {
public function getDescription() {
echo 'I can only fit wooden doors';
}
}
使用抽象工厂,创建出可以创建门及对应工匠的工厂
interface DoorFactory {
public function makeDoor() : Door;
public function makeFittingExpert() : DoorFittingExpert;
}
// Wooden factory to return carpenter and wooden door
class WoodenDoorFactory implements DoorFactory {
public function makeDoor() : Door {
return new WoodenDoor();
}
public function makeFittingExpert() : DoorFittingExpert{
return new Carpenter();
}
}
// Iron door factory to get iron door and the relevant fitting expert
class IronDoorFactory implements DoorFactory {
public function makeDoor() : Door {
return new IronDoor();
}
public function makeFittingExpert() : DoorFittingExpert{
return new Welder();
}
}
使用
$woodenFactory = new WoodenDoorFactory();
$door = $woodenFactory->makeDoor();
$expert = $woodenFactory->makeFittingExpert();
$door->getDescription(); // Output: I am a wooden door
$expert->getDescription(); // Output: I can only fit wooden doors
// Same for Iron Factory
$ironFactory = new IronDoorFactory();
$door = $ironFactory->makeDoor();
$expert = $ironFactory->makeFittingExpert();
$door->getDescription(); // Output: I am an iron door
$expert->getDescription(); // Output: I can only fit iron doors
###什么时候使用
当两个实例相关联,但又不是拥有同一逻辑的时候
##建造者模式
建立一个步骤或属性过多的实例 ###代码实例
难看的建立方式
public function __construct($size, $cheese = true, $pepperoni = true, $tomato = false, $lettuce = true) {
}
使用建造者
class Burger {
protected $size;
protected $cheese = false;
protected $pepperoni = false;
protected $lettuce = false;
protected $tomato = false;
public function __construct(BurgerBuilder $builder) {
$this->size = $builder->size;
$this->cheese = $builder->cheese;
$this->pepperoni = $builder->pepperoni;
$this->lettuce = $builder->lettuce;
$this->tomato = $builder->tomato;
}
}
建造者
class BurgerBuilder {
public $size;
public $cheese = false;
public $pepperoni = false;
public $lettuce = false;
public $tomato = false;
public function __construct(int $size) {
$this->size = $size;
}
public function addPepperoni() {
$this->pepperoni = true;
return $this;
}
public function addLettuce() {
$this->lettuce = true;
return $this;
}
public function addCheese() {
$this->cheese = true;
return $this;
}
public function addTomato() {
$this->tomato = true;
return $this;
}
public function build() : Burger {
return new Burger($this);
}
}
$burger = (new BurgerBuilder(14))
->addPepperoni()
->addLettuce()
->addTomato()
->build();
###什么时候使用
实例属性多或建造时步骤多