Bridge mode
Pattern): separate the abstract part from its implementation part so that they can change independently. It is an object structured pattern,
Pattern structure
The builder mode includes the following roles:
Take the example code of this article as a reference
- MotorcycleProduce - motorcycle assembly Abstract Class: establish motorcycle assembly standard process
- MotocycleProduct - motorcycle product itself
- Motorcycle Scooter - pedal motorcycle assembly
- MotorcycleStraddle motorcycle assembly
- Abstraction bridging abstract classes
- RefinedAbstraction - bridge pattern implementation class
UML legend
Code example
<?php namespace Bridge; //Motorcycle product itself class MotocycleProduct{ private $motor = [ "engine"=>"", "body"=>"", "whell"=>"", "bodyColor"=>"blue" ]; //New engine parts public function addEngine($engine){ $this->motor["engine"] = $engine; } public function addBody($body){ $this->motor["body"] = $body; } public function addWhell($whell){ $this->motor["whell"] = $whell; } public function setBodyColor(string $color){ $this->motor["bodyColor"] = $color; } //Get complete motorcycle object public function getMotor(){ return $this->motor; } } //Abstract class - defines the standard Target class of motorcycle production line interface MotorcycleProduce { //Engine method public function addEngine(); //Body method public function addBody(); //Wheel method public function addWhell(); //Get a motorcycle public function getMotor(); } class MotorcycleScooter implements MotorcycleProduce { public function __construct() { $this->motor = new MotocycleProduct(); } function addEngine() { $this->motor->addEngine("Pedal motorcycle-Engine installed"); } function addBody() { $this->motor->addBody("Pedal motorcycle-The car body has been installed"); } function addWhell() { $this->motor->addWhell("Pedal motorcycle-The wheels are mounted"); } function getMotor() { return $this->motor; } } class MotorcycleStraddle implements MotorcycleProduce{ public function __construct() { $this->motor = new MotocycleProduct(); } function addEngine() { $this->motor->addEngine("Straddle motorcycle-Engine installed"); } function addBody() { $this->motor->addBody("Straddle motorcycle-The car body has been installed"); } function addWhell() { $this->motor->addWhell("Straddle motorcycle-The wheels are mounted"); } function getMotor() { return $this->motor; } } //Bridge abstract class abstract class Abstraction { protected $motorProduce; public function SetImplementor(MotorcycleProduce $motorProduce) { $this->motorProduce = $motorProduce; } abstract public function addEngine(); abstract public function addBody(); abstract public function addWhell(); abstract public function getMotor(); } /**Bridging class */ class RefinedAbstraction extends Abstraction { public function addEngine() { $this->motorProduce->addEngine(); } public function addBody() { $this->motorProduce->addBody(); } public function addWhell() { $this->motorProduce->addWhell(); } public function getMotor() { return $this->motorProduce->getMotor(); } } $motorcycleScooter = new MotorcycleScooter; $motorcycleStraddle = new MotorcycleStraddle; $refinedAbstraction = new RefinedAbstraction; $refinedAbstraction->SetImplementor($motorcycleScooter); $refinedAbstraction->addBody(); $refinedAbstraction->addEngine(); $refinedAbstraction->addWhell(); var_dump($refinedAbstraction->getMotor()); echo "<br>"; $refinedAbstraction->SetImplementor($motorcycleStraddle); $refinedAbstraction->addBody(); $refinedAbstraction->addEngine(); $refinedAbstraction->addWhell(); var_dump($refinedAbstraction->getMotor());
pattern analysis
To understand the bridging pattern, we need to understand how to decouple abstraction from implementation, so that they can change independently.
- Abstraction: abstraction is to ignore some information and treat different entities as the same entity. In object-oriented, the process of extracting the common properties of objects to form classes is the process of abstraction.
- Realization: the concrete realization given for abstraction is realization. Abstraction and realization are a pair of mutually inverse concepts. The object produced by realization is more specific than abstraction and is the product of further concretization of abstract things.
- Decoupling: decoupling is to separate the coupling between abstraction and realization, or change the strong association between them into weak association, and change the inheritance relationship between two roles into association relationship. The so-called decoupling in the bridging mode refers to the use of association relationship (combination or aggregation relationship) rather than inheritance relationship between the abstraction and implementation of a software system, so that the two can change relatively independently. This is the purpose of the bridging mode.
Advantages and disadvantages
Advantages of bridging mode:
- Separate the abstract interface and its implementation.
- The bridging mode is sometimes similar to the multi inheritance scheme, but the multi inheritance scheme violates the principle of single responsibility of a class (that is, a class has only one reason for change), has poor reusability, and the number of classes in the multi inheritance structure is very large. The bridging mode is a better solution than the multi inheritance scheme.
- The bridging mode improves the scalability of the system. If one of the two change dimensions is extended arbitrarily, there is no need to modify the original system.
- Implementation details are transparent to customers and can be hidden from users.
Disadvantages of bridging mode:
- The introduction of bridging mode will increase the difficulty of system understanding and design. Because the aggregation association relationship is based on the abstraction layer, developers are required to improve the abstraction
Line design and programming- Bridging mode requires to correctly identify two independent changing dimensions in the system, so its application scope has certain limitations.
Applicable environment
Bridge mode can be used when:
- A class has two independent dimensions, and both dimensions need to be expanded.
- Systems with inheritance or multiple inheritance are not suitable
- A system that requires relatively high flexibility in abstraction and implementation