Home >Backend Development >PHP Tutorial >Learn PHP design patterns PHP implements bridge mode (bridge)_php skills

Learn PHP design patterns PHP implements bridge mode (bridge)_php skills

WBOY
WBOYOriginal
2016-05-16 20:03:411166browse

1. Bridge model structure diagram

2. Main characters in bridge mode
Abstraction role: Define the interface of the abstract class and save a reference to the implemented object.
Refined Abstraction role: Expand the abstraction role, change and modify the definition of abstraction by the parent class.
Implementor role: defines the interface of the implementation class without giving a specific implementation. This interface is not necessarily the same as the interface definition of the abstract role; in fact, the two interfaces can be completely different. Implemented roles should only provide low-level operations, while abstract roles should only provide higher-level operations based on low-level operations.
Concrete Implementor role: Implement the implemented role interface and define its specific implementation.
3. Advantages of the bridge model
1. Separate interface and its implementation part
Sharing Abstraction with Implementor helps reduce compile-time dependencies on parts of the implementation
Interface and implementation sharing facilitates layering, resulting in better structured systems
2. Improve scalability
3. Make implementation details transparent to customers.
4. Applicable Scenarios of Bridge Mode
1. If a system needs to add more flexibility between the abstract and concrete roles of components, avoid establishing a static connection between the two levels.
2. The design requires that any change in the implemented role should not affect the client, or that changes in the implemented role should be completely transparent to the client.
3. A component has more than one abstract role and implementation role, and the system requires dynamic coupling between them.
4. Although there is no problem using inheritance in the system, because abstract roles and concrete roles need to change independently, the design requires independent management of the two.
5. Bridge mode and other modes
Abstract factory pattern: The abstract factory pattern can be used to create and configure a specific bridge pattern.
Adapter mode (adapter mode): The adapter mode is used to help unrelated classes work together. It is usually used after the system design is completed. However, the bridge pattern is used at the beginning of the system, allowing the abstract interface and implementation parts to be changed independently.
State mode (state mode): The bridge mode describes the relationship between two hierarchical structures, while the state mode describes the relationship between an object and a state object. State pattern is a degenerate special case of Bridge pattern.
6. Bridge mode PHP example

<&#63;php
/**
 * 抽象化角色
 * 抽象化给出的定义,并保存一个对实现化对象的引用。
 */
abstract class Abstraction {
 
 /* 对实现化对象的引用 */
 protected $imp;
 
 /**
 * 某操作方法
 */
 public function operation() {
 $this->imp->operationImp();
 }
}
 
/**
 * 修正抽象化角色
 * 扩展抽象化角色,改变和修正父类对抽象化的定义。
 */
class RefinedAbstraction extends Abstraction {
 
 public function __construct(Implementor $imp) {
 $this->imp = $imp;
 }
 
 /**
 * 操作方法在修正抽象化角色中的实现
 */
 public function operation() {
 echo 'RefinedAbstraction operation ';
 $this->imp->operationImp();
 }
}
 
/**
 * 实现化角色
 * 给出实现化角色的接口,但不给出具体的实现。
 */
abstract class Implementor {
 
 /**
 * 操作方法的实现化声明
 */
 abstract public function operationImp();
}
 
/**
 * 具体化角色A
 * 给出实现化角色接口的具体实现
 */
class ConcreteImplementorA extends Implementor {
 
 /**
 * 操作方法的实现化实现
 */
 public function operationImp() {
 echo 'Concrete implementor A operation <br />';
 }
}
 
/**
 * 具体化角色B
 * 给出实现化角色接口的具体实现
 */
class ConcreteImplementorB extends Implementor {
 
 /**
 * 操作方法的实现化实现
 */
 public function operationImp() {
 echo 'Concrete implementor B operation <br />';
 }
}
 
/**
 * 客户端
 */
class Client {
 
 /**
 * Main program.
 */
 public static function main() {
 $abstraction = new RefinedAbstraction(new ConcreteImplementorA());
 $abstraction->operation();
 
 $abstraction = new RefinedAbstraction(new ConcreteImplementorB());
 $abstraction->operation();
 }
}
 
Client::main();
&#63;>

The above is the code to implement the bridge mode using PHP, and there are also some conceptual distinctions about the decoration mode. I hope it will be helpful to everyone's learning.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn