Bridging mode and php implementation

Source: Internet
Author: User
Tags abstract definition
Bridge Pattern ):
Separate the abstract part from its implementation part so that they can all change independently. It is an object structure mode, also known as the Handle and Body mode or Interface mode.

The bridging mode includes the following roles:
Abstract action: abstract class
Refinedmediaaction: extends the abstract class
Implementor: class interface implementation
ConcreteImplementor: implementation class

UML diagram:

  

To understand the bridge mode, we need to understand how to decouple abstract action and Implementation so that the two can change independently.
Abstraction: abstraction ignores some information and treats different entities as the same entities. In object orientation, the process of extracting the common properties of objects to form classes is abstract.
Implementation: the specific implementation given for abstraction is implementation. abstraction and implementation are a pair of reciprocal concepts. the objects produced by implementation are more specific than those produced by abstraction, is the product of further concrete abstraction.
Decoupling: decoupling is to free up coupling between abstraction and reality, or change strong associations between them into weak associations, and change the inheritance relationships between two roles into associations. In the bridge mode, decoupling refers to the association (combination or aggregation) between the abstraction and implementation of a software system, rather than the inheritance relationship, so that the two can change relatively independently, this is the purpose of the bridge mode.

Advantages:
Separates abstract interfaces and their implementations.
The bridge mode is sometimes similar to a multi-inheritance scheme, but the multi-inheritance scheme violates the single responsibility principle of the class (that is, a class has only one reason for change), and the reusability is poor, in addition, the number of classes in the multi-inheritance structure is very large, and the bridge mode is a better solution than the multi-inheritance solution.
The bridge mode improves the scalability of the system. you do not need to modify the original system if you want to expand one of the two dimensions.
The implementation details are transparent to customers and can be hidden from users.
Disadvantages:
The introduction of the bridge mode will increase the system's understanding and design difficulty. since the aggregation Association is established on the abstraction layer, developers are required
Line design and programming. -The Bridge mode requires correct identification of two independent dimensions in the system, so the scope of use has certain limitations.

Applicable environment:
If a system requires more flexibility between the abstract role and the specific role of the component, it will avoid establishing static inheritance relationships between the two layers, the bridge mode enables them to establish an association at the abstraction layer.
Abstract roles and real-time roles can be independently extended in an inherited manner without affecting each other. when running a program, you can dynamically combine the objects of an abstract subclass and the objects of an implemented subclass, that is, the system needs to dynamically couple abstract roles and actual roles.
A class has two independent dimensions, and both dimensions need to be extended.
Although there is no problem in using inheritance in the system, the abstract roles and specific roles need to be changed independently, and the design requirements need to be managed independently.
The bridge mode is especially applicable to systems that do not want to use inheritance or because of the dramatic increase in the number of system classes due to multi-level inheritance.

Code implementation:

 Imp-> operationImp () ;}// modify abstract roles, expand abstract roles, and modify the abstract definition of the parent class. Class RefinedAbstraction extends action {public function _ construct (Implementor $ imp) {$ this-> imp = $ imp ;} // The operation method implements public function operation () {echo 'refined1_action operation' in the corrected abstract role; $ this-> imp-> operationImp ();}} // implement the role. the interface for implementing the role is provided, but no specific implementation is provided. Abstract class Implementor {// implementation declaration of the operation method abstract public function operationImp ();} // concrete role, the specific implementation of the implemented role interface class ConcreteImplementorA extends Implementor {// implementation of the operation method public function operationImp () {echo 'concrete implementor A operation
';}} // Role B provides the specific implementation class ConcreteImplementorB extends Implementor implementation public function operationImp () {echo 'concrete implementor B operation
';}}

Specific instance:

// Enable playback of videos in MPEG, AVI, and WMV formats on various operating systems. // an abstract class OperationSystem {public $ musicParren; function playMpeg () is defined () {$ this-> musicParren-> playMpeg ();} function playWmv () {$ this-> musicParren-> playWmv ();} function playAvi () {$ this-> musicParren-> playAvi () ;}// extends the abstract class to define the playing mode of Linux. class linux extends OperationSystem {function _ construct (MusicParren $ musicParren) {$ this-> musicParren = $ musicParren;} // extends the abstract class to define the playing mode of Linux. class Windows extends OperationSystem {function _ construct (MusicParren $ musicParren) {$ this-> musicParren = $ musicParren;} // An extended abstract class that defines the Unix playing mode class Unix extends OperationSystem {function _ construct (MusicParren $ musicParren) {$ this-> musicParren = $ musicParren;} // implementation class interface abstract class MusicParrenInterface {abstract function playMpeg (); abstract function playWmv (); abstract function playAvi ();} // implementation class MusicParren extends MusicParrenInterface {function playMpeg () {echo "playing Mpeg video";} function playWmv () {echo "playing a video in the Wmv format ";} function playAvi () {echo "video in Avi format" ;}}// test $ operationSystem = new Windows (new MusicParren (); $ operationSystem-> playMpeg (); $ operationSystem-> playAvi ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.