Example usage and code analysis of PHP bridge mode

Time:2022-5-9

explain

1. Combine two unrelated classes together, and then use the methods and attributes in the two classes to output a new result.

2. The structure is divided into abstraction abstract class, refindabstraction refined abstract class, implementor implementation class, concreteimplementor concrete implementation class and 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
{
    /**
     *Colour
     * @var Colour
     */
    protected $colour;
 
 
    /**
     * Shape constructor.
     * @param Colour $colour
     */
    public function __construct(Colour $colour)
    {
        $this->colour = $colour;
    }
 
 
    /**
     * @return mixed
     */
    abstract public function operation();
}
 
 
/**
 *Round
 * 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() . ' Br '>;
    }
}
 
 
//Client code
//White circle
$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();

Operation results

White circle
black square
Red rectangle

Content extension:

Bridging mode

Bridging mode: separate the abstract part from the implementation part, so that they can change independently. It is a structural pattern, also known as handle and body pattern or interface pattern. When an abstraction may have multiple implementations, inheritance is often used to coordinate them. The definition of an abstract class is an interface to that abstraction. Specific subclasses are implemented in different ways, but this method is sometimes not flexible enough. The inheritance mechanism fixes the abstract part with his vision part, which makes it difficult to modify, expand and fully use the abstract part and the implementation part independently.

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 correlation between them into weak correlation, and change the inheritance relationship between two roles into correlation 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 Realization of a software system, so that the two can change relatively independently. This is the purpose of the bridging mode.

Applicability

1). You don’t want to have a fixed binding relationship between the abstract and its implementation part. For example, the implementation part should be able to be selected or switched at the running time of the program.

2). The abstraction of a class and its image can be extended by generating subclasses. At this point, the bridge pattern allows you to define different abstract interfaces

And implementation parts, and expand them.

3). The modification of an abstract implementation part should have no impact on the customer, that is, the customer’s code does not need to be recompiled.

4). You want to completely hide the abstract implementation from the customer.

5). You want to share the implementation among multiple implementations, but at the same time, ask the customer not to know this.

This is the end of this article about the example usage and code analysis of PHP bridge mode. For more information about what PHP bridge mode is, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Application of HTML and CSS in flash

Application of HTML and CSS in flash:I accidentally saw my colleague den making a small thing: Using HTML and CSS in flash. The code is as follows:var myStyle:TextField.StyleSheet = new TextField.StyleSheet();myStyle.load(“sample.css“);content_txt.styleSheet = myStyle;content_txt.multiline= true;content_txt.wordWrap = true;content_txt.html = true;var story:XML = new XML();story.ignoreWhite = true;story.load(“sample.html”);story.onLoad = function () {content_txt.htmlText = story;} This is to load external […]