Written before: The meaning of the design pattern is to solve the actual design problems, the reason why the poor understanding is because there is no personal experience, so to keep learning practical thinking.
Bridge is translated as bridging. Since there must be two objects bridged for bridging. This article intends to analyze the bridging mode from three aspects.
1. What are the two objects bridged? 2. How to Bridge 3. Why use bridging mode.
1. What are the two objects bridged?
First look at the definition of bridging mode: separate the abstract part of a class from the implementation part, so that they can all be independent changes. From this sentence, section A rough know the bridge of the object
: An abstract part and an implementation part of a class.
2. Why do I need to bridge the connection:
First look at the UML diagram of the bridging mode given in the design pattern book.
The client invokes the operation method in abstraction to realize the function that the client wants. Where the specific operation implementation is implemented by Implementor (whose subclasses are responsible for implementation), operation through a combination of methods to complete the call to Implementor (generally through the Factory mode to obtain Implementor instances, Use combination rather than inherit ~ ~)
In the actual program design, when a class has multidimensional changes, I understand the multidimensional change refers to the method in the abstract class has a variety of implementations, in the subclass of different implementations and constitute a different subclass. Give me a simple chestnut.
Man this abstract class, inside there is a virtual method, virtual run () = 0;
1 class 2{3 0; 4 }
We have men, women inherit the Persion class. and rewrite the Run method, but some people run fast, some people run slowly, and some people do not run (special cases, such as baby ~), this time we want to
A fast-running woman, a man who runs fast, a man who runs slowly, a woman who runs slowly, needs four subclasses (each inheriting men and women). At this time if a neutral person, the programmer hit the keyboard, pick up the mouse to start copying and pasting t_t.
At this point, the bridging pattern shows power. The definition of a class called runimplementor concrete implementation is as follows:
1 classRunimplementor2 {3 Virtual voidRun () =0;4 }5 6 7 classSlowrunimplementor: PublicRunimplementor8 {9 Virtual voidRun ()Ten { One //Slow Run A } - } - the - classFastrunimplementor: PublicRunimplementor - { - Virtual voidRun () + { - //Fast Run + } A}
At this time, want a fast running woman, person *womanfast = new Person ("fast"); Womanfast->getrunimplementor (). run ();
Finally, why use bridging mode:
As can be seen from the chestnut above, bridging mode separates the implementation of the class from the abstraction, and it is convenient to expand it separately when it is extended, to be coupled at the upper level, and to reduce the redundancy of the code.
Bridge mode of design mode