1. 适配器模式(Adapter Pattern):
适配器模式允许接口不兼容的类可以一起工作。它允许你将一个类的接口转换成另一个客户端期望的接口。
示例代码:
// 目标接口
interface Target {
public function request();
}
// 适配者
class Adaptee {
public function specificRequest() {
return "Specific request";
}
}
// 适配器
class Adapter implements Target {
private $adaptee;
public function __construct(Adaptee $adaptee) {
$this->adaptee = $adaptee;
}
public function request() {
return $this->adaptee->specificRequest();
}
}
// 客户端代码
$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
echo $adapter->request(); // 输出 "Specific request"
2. 桥接模式(Bridge Pattern):
桥接模式将抽象部分与实现部分分离,使它们可以独立变化。它通过将抽象类与实现类分开,以便它们可以独立地进行扩展。
示例代码:
// 实现部分接口
interface Implementor {
public function operationImpl();
}
// 具体实现类A
class ConcreteImplementorA implements Implementor {
public function operationImpl() {
return "Concrete Implementor A";
}
}
// 具体实现类B
class ConcreteImplementorB implements Implementor {
public function operationImpl() {
return "Concrete Implementor B";
}
}
// 抽象部分
abstract class Abstraction {
protected $implementor;
public function __construct(Implementor $implementor) {
$this->implementor = $implementor;
}
abstract public function operation();
}
// 扩展抽象部分
class RefinedAbstraction extends Abstraction {
public function operation() {
return "Refined Abstraction: " . $this->implementor->operationImpl();
}
}
// 客户端代码
$implementorA = new ConcreteImplementorA();
$abstraction = new RefinedAbstraction($implementorA);
echo $abstraction->operation(); // 输出 "Refined Abstraction: Concrete Implementor A"
3. 组合模式(Composite Pattern):
组合模式允许你将对象组合成树形结构以表示"部分-整体"的层次结构。它使得客户端对单个对象和组合对象的使用具有一致性。
示例代码:
// 抽象构件
interface Component {
public function operation();
}
// 叶子构件
class Leaf implements Component {
public function operation() {
return "Leaf";
}
}
// 容器构件
class Composite implements Component {
private $children = [];
public function add(Component $component) {
$this->children[] = $component;
}
public function operation() {
$result = "Composite: ";
foreach ($this->children as $child) {
$result .= $child->operation() . " ";
}
return $result;
}
}
// 客户端代码
$leaf1 = new Leaf();
$leaf2 = new Leaf();
$composite = new Composite();
$composite->add($leaf1);
$composite->add($leaf2);
echo $composite->operation(); // 输出 "Composite: Leaf Leaf"
4. 装饰器模式(Decorator Pattern):
装饰器模式允许你通过将对象封装在装饰器类中,动态地添加新行为或责任。它提供了比继承更灵活的替代方案。
示例代码:
// 抽象组件
interface Component {
public function operation();
}
// 具体组件
class ConcreteComponent implements Component {
public function operation() {
return "Concrete Component";
}
}
// 抽象装饰器
abstract class Decorator implements Component {
protected $component;
public function __construct(Component $component) {
$this->component = $component;
}
public function operation() {
return $this->component->operation();
}
}
// 具体装饰器A
class ConcreteDecoratorA extends Decorator {
public function operation() {
return "Concrete Decorator A: " . parent::operation();
}
}
// 具体装饰器B
class ConcreteDecoratorB extends Decorator {
public function operation() {
return "Concrete Decorator B: " . parent::operation();
}
}
// 客户端代码
$component = new ConcreteComponent();
$decoratorA = new ConcreteDecoratorA($component);
$decoratorB = new ConcreteDecoratorB($decoratorA);
echo $decoratorB->operation(); // 输出 "Concrete Decorator B: Concrete Decorator A: Concrete Component"
这些结构型设计模式为构建更灵活、可维护和可扩展的代码提供了一些模板。你可以根据具体的问题和需求选择适当的设计模式。
转载请注明出处:http://www.pingtaimeng.com/article/detail/11939/PHP