Structure diagram of bridge pattern
second, the main role in the bridge model
Abstract (abstraction) Role: Defines an interface for an abstract class and holds a reference to the materialized object.
Fixed abstract (refined abstraction) role: Extends the abstraction role, changes and corrects the definition of the abstract of the parent class.
Materialized (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. The implementation role should give only the underlying operations, and the abstract role should only give a higher level of operation based on the underlying operation.
Concrete implementation (concrete Implementor) Role: Implementing the Role interface and defining its specific implementation.
Third, the advantages of the bridge model
1, separation interface and its implementation part
Sharing abstraction with implementor helps reduce dependencies on implementing partial compile times
interface and implementation sharing help tiering, resulting in better structured systems
2. Improve scalability
3, realize the details of the customer transparent.
Four, the bridge pattern applies the scene
1. If a system needs to add more flexibility between component abstraction and materialization roles, avoid establishing static relationships between the two levels.
2. Any change in the implementation role of the design requirements should not affect the client, or the implementation of the role changes to the client is completely transparent.
3. An artifact has more than one abstract role and an implementation role, and the system requires dynamic coupling between them.
4. Although there is no problem with inheritance in the system, the design requirements need to be managed independently because of the need for an independent change in the abstraction and materialization roles.
v. Bridge model and other modes
Abstract Factory mode (in abstract Factory mode):Abstract factory patterns can be used to create and configure a specific bridge pattern.
Adapter Mode (adapter mode):Adapter patterns are used to help unrelated classes work together. It is usually used only after the system design is complete. However, the bridge mode is used at the beginning of the system, which allows the abstraction interface and the implementation part to be changed independently.
status Mode (state mode):The bridge pattern describes the relationship between two hierarchical structures, and the state pattern describes the relationship between an object and a state object. The state pattern is a special case of the degradation of the bridge model.
six, bridge model PHP sample
<?php/** * Abstract role * Abstract the definition given, and save a reference to the materialized object.
* * Abstract class Abstraction {/* The reference to the materialized object/* protected $imp;
/** * A method of operation */Public function operation () {$this->imp->operationimp ();
}/** * Corrects the abstract role * extends the abstraction role, changing and correcting the parent's definition of abstraction. * * Class Refinedabstraction extends Abstraction {public function __construct (implementor $imp) {$this->imp = $imp
;
The implementation of the/** * action method in the modified abstract role/Public function operation () {echo ' refinedabstraction operation ';
$this->imp->operationimp ();
}/** * Implementation role * gives the interface for implementing the role, but does not give a specific implementation.
* * Abstract class Implementor {/** * Implementation declaration of the operation method/abstract Public Function Operationimp ();} /** * Materialized role A * gives the realization of the implementation of the role of the interface/class Concreteimplementora extends Implementor {/** * Operation method Implementation of the implementation of * * Public func
tion Operationimp () {echo ' concrete implementor A operation <br '; }/** * materialized role b * gives the realization of the implementation of the role interface * * * Class Concreteimplementorb extends Implementor {/** * Implementation of the operation method * * Publi C function Operationimp () {echo ' concrete implementor B operation <br/> ';
}/** * Client/class Client {/** * Main program.
*/public static function main () {$abstraction = new refinedabstraction (new Concreteimplementora ());
$abstraction->operation ();
$abstraction = new Refinedabstraction (new Concreteimplementorb ());
$abstraction->operation (); } client::main ();?>
The above is the use of PHP to implement the bridge model of the code, there are some decorative patterns of the concept of distinction, I hope to help you learn.