PHP设计模式漫谈之结构模式(PHP设计模式详解:深入探讨结构型模式)
原创
一、引言
在软件开发中,设计模式是一套被反复使用的、大多数人认可的、经过分类编目的、代码设计经验的总结。PHP作为一种流行的服务器端脚本语言,在设计模式的应用上同样具有广泛的应用场景。本文将深入探讨PHP中的结构型模式,这些模式关键关注类和对象之间的组合,以形成更大的结构。
二、结构型模式概述
结构型模式关键涉及怎样组合类和对象,以形成更大的结构。结构型模式可以分为以下几种:
- 适配器模式(Adapter)
- 装饰器模式(Decorator)
- 代理模式(Proxy)
- 外观模式(Facade)
- 桥接模式(Bridge)
- 组合模式(Composite)
- 享元模式(Flyweight)
三、适配器模式(Adapter)
适配器模式是一种将一个类的接口转换成客户期望的另一个接口的模式。适配器模式让原本接口不兼容的类可以协作无间。
class Target {
public function request() {
return 'Target: The target handles the request.';
}
}
class Adaptee {
public function specificRequest() {
return 'Adaptee: The specific request.';
}
}
class Adapter implements Target {
private $adaptee;
public function __construct(Adaptee $adaptee) {
$this->adaptee = $adaptee;
}
public function request() {
$result = $this->adaptee->specificRequest();
return "Adapter: (TRANSLATED) " . $result;
}
}
$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
echo $adapter->request();
四、装饰器模式(Decorator)
装饰器模式是一种动态地给对象添加一些额外的职责的模式。就扩展对象功能而言,它比生成子类更为灵活。
class Component {
public function operation() {
return 'Component: I am doing something.';
}
}
class ConcreteComponent extends Component {
public function operation() {
return 'ConcreteComponent: I am doing something.';
}
}
class Decorator extends Component {
protected $component;
public function __construct(Component $component) {
$this->component = $component;
}
public function operation() {
return $this->component->operation();
}
}
class ConcreteDecoratorA extends Decorator {
public function operation() {
return parent::operation() . ' + ConcreteDecoratorA: I add something.';
}
}
$component = new ConcreteComponent();
$decorator = new ConcreteDecoratorA($component);
echo $decorator->operation();
五、代理模式(Proxy)
代理模式是一种为其他对象提供一种代理,以控制对这个对象的访问的模式。
class Subject {
public function request() {
return 'Subject: Handle the request.';
}
}
class RealSubject extends Subject {
public function request() {
return 'RealSubject: Real request.';
}
}
class Proxy extends Subject {
private $realSubject;
public function request() {
if ($this->realSubject === null) {
$this->realSubject = new RealSubject();
}
return $this->realSubject->request();
}
}
$proxy = new Proxy();
echo $proxy->request();
六、外观模式(Facade)
外观模式是一种为子系统中的一组接口提供一个统一的接口,让子系统更容易使用的模式。
class SubsystemOne {
public function operationOne() {
return 'SubsystemOne: Ready!';
}
}
class SubsystemTwo {
public function operationTwo() {
return 'SubsystemTwo: Fire!';
}
}
class SubsystemThree {
public function operationThree() {
return 'SubsystemThree: No problem!';
}
}
class Facade {
private $子系统One;
private $子系统Two;
private $子系统Three;
public function __construct() {
$this->子系统One = new SubsystemOne();
$this->子系统Two = new SubsystemTwo();
$this->子系统Three = new SubsystemThree();
}
public function operation() {
return $this->子系统One->operationOne()
. ' ' . $this->子系统Two->operationTwo()
. ' ' . $this->子系统Three->operationThree();
}
}
$facade = new Facade();
echo $facade->operation();
七、桥接模式(Bridge)
桥接模式是一种将抽象部分与实现部分分离,使它们可以自由变化。
abstract class Abstraction {
protected $implementation;
public function __construct(Implementation $implementation) {
$this->implementation = $implementation;
}
public abstract function operation();
}
class ConcreteAbstractionA extends Abstraction {
public function operation() {
return $this->implementation->operation() . ' ConcreteAbstractionA';
}
}
class ConcreteAbstractionB extends Abstraction {
public function operation() {
return $this->implementation->operation() . ' ConcreteAbstractionB';
}
}
interface Implementation {
public function operation();
}
class ConcreteImplementationA implements Implementation {
public function operation() {
return 'ConcreteImplementationA: The result of the operation.';
}
}
class ConcreteImplementationB implements Implementation {
public function operation() {
return 'ConcreteImplementationB: The result of the operation.';
}
}
$implementation = new ConcreteImplementationA();
$abstraction = new ConcreteAbstractionA($implementation);
echo $abstraction->operation();
$implementation = new ConcreteImplementationB();
$abstraction = new ConcreteAbstractionB($implementation);
echo $abstraction->operation();
八、组合模式(Composite)
组合模式是一种将对象组合成树形结构以描述部分-整体的层次结构,组合模式使用户对单个对象和组合对象的使用具有一致性。
interface Component {
public function operation();
}
class Leaf implements Component {
public function operation() {
return 'Leaf';
}
}
class Composite implements Component {
private $children = [];
public function operation() {
$result = '';
foreach ($this->children as $child) {
$result .= $child->operation();
}
return $result;
}
public function add(Component $component) {
$this->children[] = $component;
}
public function remove(Component $component) {
$index = array_search($component, $this->children);
if ($index !== false) {
unset($this->children[$index]);
}
}
}
$root = new Composite();
$root->add(new Leaf());
$root->add(new Leaf());
$composite = new Composite();
$composite->add(new Leaf());
$composite->add(new Leaf());
$root->add($composite);
echo $root->operation();
九、享元模式(Flyweight)
享元模式是一种运用共享技术有效地赞成大量细粒度的对象。
class FlyweightFactory {
private $flyweights = [];
public function getFlyweight($key) {
if (!array_key_exists($key, $this->flyweights)) {
$this->flyweights[$key] = new ConcreteFlyweight($key);
}
return $this->flyweights[$key];
}
}
class ConcreteFlyweight {
private $intrinsicState;
public function __construct($intrinsicState) {
$this->intrinsicState = $intrinsicState;
}
public function operation($extrinsicState) {
return $this->intrinsicState . ' ' . $extrinsicState;
}
}
class UnsharedConcreteFlyweight {
private $intrinsicState;
public function __construct($intrinsicState) {
$this->intrinsicState = $intrinsicState;
}
public function operation($extrinsicState) {
return $this->intrinsicState . ' ' . $extrinsicState;
}
}
$factory = new FlyweightFactory();
$f1 = $factory->getFlyweight('A');
$f2 = $factory->getFlyweight('B');
$f3 = $factory->getFlyweight('A');
echo $f1->operation(' extrinsicState1');
echo $f2->operation(' extrinsicState2');
echo $f3->operation(' extrinsicState3');
十、总结
结构型模式在PHP开发中具有广泛的应用,能够帮助我们更好地管理类和对象之间的关系,减成本时间代码的复用性和灵活性。通过本文的介绍,我们了解了各种结构型模式的基本概念和应用场景,期待在实际开发中能够灵活运用这些模式,减成本时间我们的代码质量。