illustrate
1. Combine two originally unrelated classes together, and then use the methods and properties in the two classes to output a new result.
2. The structure is divided into Abstraction abstract class, RefindAbstraction abstract class, Implementor implementation class, ConcreteImplementor concrete implementation class, and Client client code.
Example
/** * Color abstract class * Class Colour */ abstract class Colour { /** * @return mixed */ abstract public function run(); } /** * black * Class Black */ class Black extends Colour { public function run() { // TODO: Implement run() method. return 'black'; } } /** * White * Class White */ class White extends Colour { public function run() { // TODO: Implement run() method. return 'White'; } } /** * red * Class Red */ class Red extends Colour { public function run() { // TODO: Implement run() method. return 'red'; } } /** * Shape abstract class * Class Shape */ abstract class Shape { /** * color * @var Colour */ protected $colour; /** * Shape constructor. * @param Colour $colour */ public function __construct(Colour $colour) { $this->colour = $colour; } /** * @return mixed */ abstract public function operation(); } /** * Circular * Class Round */ class Round extends Shape { /** * @return mixed|void */ public function operation() { // TODO: Implement operation() method. echo $this->colour->run() . 'Circular<br>'; } } /** * rectangle * Class Rectangle */ class Rectangle extends Shape { /** * @return mixed|void */ public function operation() { // TODO: Implement operation() method. echo $this->colour->run() . 'Rectangle<br>'; } } /** * square * Class Square */ class Square extends Shape { /** * @return mixed|void */ public function operation() { // TODO: Implement operation() method. echo $this->colour->run() . 'Square<br>'; } } // Client code// White round$whiteRound = new Round(new White()); $whiteRound->operation(); // Black square$blackSquare = new Square(new Black()); $blackSquare->operation(); // Red rectangle$redRectangle = new Rectangle(new Red()); $redRectangle->operation();
Running results
White round
Black square
Red rectangle
Content extension:
Bridge mode
Bridge mode: Separate abstract parts from implementation parts so that they can all be changed independently. It is a structural mode, also known as the Handle and body mode or the Interface mode. When an abstract may have multiple implementations, inheritance is usually used to coordinate them. The abstract class defines the interface to the abstract. Specific subclasses are implemented in different ways, but this method is sometimes not flexible enough. The inheritance mechanism fixes the abstract part with its sight part, making it difficult to independently modify, expand and recharge the abstract part and implementation part.
To understand the bridge pattern, the key is to understand how abstraction and implementation are decoupled so that the two can be changed independently.
•Abstract: Abstraction means ignoring some information and treating different entities as the same entities. In object-oriented, the process of extracting the common properties of the object into a class is an abstract process.
•Implementation: The concrete implementation given for abstraction is realization. Abstraction and realization are a pair of mutual inverse concepts. The objects generated by realization are more specific than abstraction, and are the product of further concreteization of abstract things.
•Decoupling: Decoupling means freeing the coupling between abstraction and implementation, or changing the strong correlation between them into weak correlation, and changing the inheritance relationship between two roles into an association relationship. The so-called decoupling in the bridge mode refers to the use of an association relationship (combination or aggregation relationship) instead of an 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 bridge mode.
applicability
1). You do not want a fixed bond between the abstract and its implementation part, such as the implementation part should be selected or switched at the moment of the program's running time.
2). The abstraction of the class and its visual image can be expanded by generating subclasses. At this time, the bridge pattern allows you to use different abstract interfaces
Combine and expand them with implementation parts.
3). Modification of an abstract implementation part should have no effect on the client, that is, the client's code does not need to be recompiled.
4). You want to completely hide the abstract implementation part from the client.
5). You want to share the implementation among multiple implementations, but at the same time require the customer to be unaware of this.
This is the article about the example usage and code analysis of php bridge mode. For more related contents of php bridge mode, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!