9th. Structural mode-Bridging mode

Source: Internet
Author: User

1. Definition of bridging mode ( bridge pattern)

(1) Separating the part of the abstraction from its implementation so that they can vary independently

① General "abstract" and "implementation" refers to the inheritance relationship of the parent-child class. But here, Gof's so-called "abstraction" is that if a class change is caused by a multidimensional factor (set to 2-D), the other variables are abstracted into an interface , leaving only this interface in the "Abstraction class", and then using the object combination (rather than inheriting) the way to rely on this interface. "Implementation" means implementing an interface (a change in the 2nd dimension) by having another subclass of "Implementor class" .

② "Abstraction class" and "Implementor class" respectively represent the two dimensions that cause class changes (the former is an abstract class, the latter is an interface), and because of this separation, they can change independently.

③ 's simple understanding is that extracting a "method" from a class forms another "class". such as animal dogs, in the design, the dog design as a class, the inside of the "walking" method separated from the dog, to form a "walking" interface, and in the "Dog" class to use the "walking" object. This allows the "dog" and "walk" classes to change independently.

(2) Structure and description of bridging mode

 ①abstraction: An interface to an abstract section. Usually in this object, to maintain a reference to an implementation part of the object, the abstract object inside the method, you need to call the implementation part of the object to complete. The methods in this object are often related to specific business methods.

②refinedabstraction: An interface that extends the abstraction section. Typically, in these objects, you define the methods that are related to the actual business, and the implementations of these methods typically use the methods defined in abstraction, or you may need to invoke the implementation part of the object to complete. as in the Operation method, the general call Impl->operationimpl ();

③implementor: The interface that defines the implementation section. This interface is not consistent with the method in abstraction.

④concreteimplementor: The object that really implements the Implementor interface.

(3) Cases where bridging mode is not used

① crayons and brushes: The key difference between the two is whether the pen and color can be separated .

② Cross-platform image browsing system

A, the use of multi-layer inheritance structure, resulting in the number of classes in the system increased sharply (total 17).

B, System expansion trouble, because each specific class (*image) contains both image file format information, but also contains operating system information. So whether you add a new image File format or add a new operating system, you need to add a lot of concrete classes. (If you add TIF, you need to add 3 specific classes to display in 3 different operating systems.) If you add a new operating system, you need to add a specific class under each *image.

C, it can be seen that the system has two independently varying dimensions : image File format and operating system .

(4) Thinking bridge mode

The nature of the ①bridge model: separating abstraction and implementation . It is a set of solutions for multi-inheritance , which uses inheritance as a combination of objects to decouple the inherent binding between abstractions and implementations so that "abstractions" and "implementations" can change independently of each other's latitude.

The motive of ②bridge mode is: Bridge mode is to deal with the multi-dimensional change of class , which is to decouple.

③ because the abstract part and the implementation part are completely separated. So you can dynamically combine the actual implementation at runtime to dynamically transform the function. In addition, the same real implementation can be used by different abstract objects, and conversely, the same abstraction cannot have many different implementations.

④ Bridge mode is a very useful structural design pattern, if a class has multiple independently changing dimensions, the pattern can separate the multiple dimensions so that they can be expanded independently. Make the system more consistent with the "single responsibility Principle".

"Programming Experiment" sends a prompt message

//Structural mode: Bridging mode//scenario: Sending a prompt message//messages: Normal, expedited, and express messages//How to send: Station message, mobile phone message, e-mail#include <iostream>#include<string>using namespacestd;//implementing a unified interface for sending messagesclassmessageimplementor{ Public:    //Send message://@param message content to be sent by a message//@param touser message sent to the person    Virtual voidSendstringMessagestringTouser) =0; };//Abstract Message Objectclassabstractmessage{Private:    //an object that holds an implementation partmessageimplementor&Impl; Public:    //constructor, an object passed into the implementation partAbstractmessage (messageimplementor&mi): Impl (mi) {}//to send a message, transpose the implementation part of the method    Virtual voidSendMessage (stringMessagestringTouser)    {impl.send (message, touser); }};//************************ How to send a specific message *******************//the implementation of the message inside the stationclassMessagesms: Publicmessageimplementor{ Public:    voidSendstringMessagestringTouser) {cout<<"sms:\ ""<< message <<"\ "to:"<< Touser <<Endl; }    };//e-mail Send MessageclassMessageemail: Publicmessageimplementor{ Public:    voidSendstringMessagestringTouser) {cout<<"email:\ ""<< message <<"\ "to:"<< Touser <<Endl; }    };//Mobile mode of sending messagesclassMessagemobile: Publicmessageimplementor{ Public:    voidSendstringMessagestringTouser) {cout<<"mobile:\ ""<< message <<"\ "to:"<< Touser <<Endl; }    };//********************* The specific message type ************************//General NewsclassCommonmessage: Publicabstractmessage{ Public: Commonmessage (Messageimplementor&im): Abstractmessage (IM) {}};//Expedited MessageclassUrgencymessage: Publicabstractmessage{ Public: Urgencymessage (Messageimplementor&im): Abstractmessage (IM) {}voidSendMessage (stringMessagestringTouser) {Message="Urgency:"+message;    Abstractmessage::sendmessage (message, touser); }};//Express NewsclassSpecialurgencymessage: Publicabstractmessage{ Public: Specialurgencymessage (Messageimplementor&im): Abstractmessage (IM) {}voidSendMessage (stringMessagestringTouser) {Message="specialurgency:"+message;        Abstractmessage::sendmessage (message, touser); //we need to add a message to be urged.Hurry ( -); }        voidHurry (intmessageId) {        //carry out the business of stimulating, send out the information of the foot, here is a simple signalcout <<"hurry:messageid="<< messageId <<Endl; }};intMain () {//1, use the station short-message to send messages (ordinary, urgent, express, etc.)//to create a concrete implementation objectmessageimplementor* Impl =NewMessagesms ();//Choose the way to send short messages in the station//create a normal message objectabstractmessage* m =NewCommonmessage (*impl);//send a normal messageM->sendmessage ("Cup of Tee please","Santaclaus"); Deletem; //Create an expedited message objectm =NewUrgencymessage (*impl);//send an expedited messageM->sendmessage ("Cup of Tee please","Santaclaus"); Deletem; //Create an express message objectm =NewSpecialurgencymessage (*impl);//send an express messageM->sendmessage ("Cup of Tee please","Santaclaus"); Deletem; DeleteImpl; cout<<Endl; //2, switch the implementation into mobile phone text messages, and then realize again//to create a concrete implementation objectImpl =NewMessagemobile (); //create a normal message objectm =NewCommonmessage (*impl); M->sendmessage ("Cup of Tee please","Santaclaus"); Deletem; //Create an expedited message objectm =NewUrgencymessage (*impl); M->sendmessage ("Cup of Tee please","Santaclaus"); Deletem; //Create an express message objectm =NewSpecialurgencymessage (*impl); M->sendmessage ("Cup of Tee please","Santaclaus"); Deletem; return 0;}
View Code

2. who is bridging: who also creates Implementor objects?

(1) Mode 1: The client creates the Implementor object and sets it to the abstract part of the object.

(2) Mode 2: You can create an abstract part of an object by itself when it is constructed. Of course, you can also pass some parameters to the object in this abstract section so that it can create a specific Implementor object based on the parameters.

(3) Mode 3: You can select and create a default Implementor object in abstraction, and the subclass can change the implementation as needed.

(4) Mode 4: You can use an abstract factory or a simple factory to select and create a specific Implementor object. Classes in the abstract section can get the Implementor object by calling the factory's method.

3. Advantages of bridging mode

(1) Separating the abstraction and the implementation part. Let the abstraction and the implementation part can change independently. For the high-level part of the system, it is possible to change the dependency implementation to dependent interface programming only by knowing the interface of the abstract part and the implementation part.

(2) Better extensibility: Abstractions and implementations can be expanded independently, without affecting each other.

(3) Dynamic switching is possible: because an implementation is no longer fixed to an abstract interface, it can be dynamically toggled during run time.

(4) Can reduce the number of sub-categories: if the implementation of the inheritance, about two latitude on the number of the product of the variable quantity of the number of subclasses, whereas the use of bridging mode requires only two latitude and a variable number of subclasses and sub-classes.

4. usage Scenarios for bridging mode

(1) Graphics and Windows need to span multiple platforms on the system.

(2) A class has two independent change dimensions, and two dimensions need to be expanded.

(3) in the three- tier architecture , the business Logic Layer (BLL) is decoupled from the data manipulation layer ( DAL) through bridging mode.

9th. Structural mode-Bridging mode

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.