Name: Bridge Mode (refer to the bridge mode used by spring)
Intention: Separate the abstract part from its implementation part so that they can all change independently.
Applicability:
- You do not want to have a fixed binding relationship between the abstraction and its implementation. For example, this may be becauseProgramThe runtime implementation part can be selected or switched.
- Class abstraction and its implementation should be expanded by generating sub-classes. In this case, the B r I d G E mode allows you to combine different abstract interfaces and implementation parts and expand them separately.
- The modification of an abstract implementation part should not affect the customer, that is, the customer'sCodeYou do not need to re-compile.
- (C ++) You want to completely hide the abstract implementation part of the customer. In C ++, the class representation is visible in the class interface.
- There are many classes to generate. Such a hierarchical structure indicates that an object must be divided into two parts. R u m B A u g h refers to this type of hierarchy as "nested generalization" (nested generalizations ).
- You want to share the implementation among multiple objects (the reference count may be used), but the customer is not required to know this. A simple example is c o p l I e n s t r I N G class [c o p 9 2], multiple objects in this class can share the same string representation (s t r I n g r e p ).
Instance
1. Define an interface work
Package bridge_pattern; public interface work {public void show ();}
2. Define a class permission sion
Package bridge_pattern; Public Class Partition sion {private work; Public partition Sion (Work work) {This. Work = work;} public void theme () {work. Show ();}}
3. Define specific IT work itwork
Package bridge_pattern; public class itwork implements work {@ override public void show () {system. Out. println ("Hello this is itwork ");}}
4. Define a sales job sellwork
Package bridge_pattern; public class sellwork implements work {@ override public void show () {system. Out. println ("Hello this is sellwork ");}}
5. Define a test class
Package bridge_pattern; public class client {public static void main (string [] ARGs) {// display it occupation new career Sion (New itwork ()). theme (); // display the sales profession new career Sion (New sellwork ()). theme ();}}
Summary:
In the Spring framework, many bridging modes are used. Please make an axe!