Bridge1. Affiliation Type: Structure Mode 2. Conditions of use: Typically, when an abstract class or interface has more than one specific implementation (concrete subclass), the relationships between these concrete may have the following two kinds: a. These multiple concrete implementations happen to be juxtaposed, such as fighting, or unarmed. , or bring some guys. Between the two. In practical applications, it is often possible to have a conceptual overlap between these many specific implementations. Then we need to separate the common parts of the abstract from the part of the Act, which is intended to be placed in an interface, and now we need to design two interfaces, respectively, to place abstractions and behaviors. (Key: The independent separation of the abstract common part from the common part of the behavior.) and a fight with a guy to see what's in it. What wood, control, brick, a large number of, in addition to the original unarmed in addition to the style of fighting, is the mob or this smart type .... Fight the family is ruthless enough, ignorant of a lot of inheritance, hang ...
3. Basic idea: Separating abstraction from behavior, independent, but dynamic.
4. Example/** * The abstract of Fight */public abstract class Fight {protected String name; public Abstract void Hitothers (); Public String GetName () {return name;} protected Fightimp GetProps (String prop) {//factory if (prop.equals ("Knife")) {return new Fightimpknife (); else if (prop.equals ("Brick")) {return new Fightimpbrick (); else {return new fightimpempty (); } }}
/** * The Refinedabstraction */public class Fightcrazy extends fight{fightimp prop; public Fightcrazy (String prop,string Name) {this.prop=getprops (prop); This.name=name; public void Hitothers () {System.out.println ("Mob:" +getname ()); Prop.useprops (); }}
/** * The Refinedabstraction */public class Fightsmart extends fight{fightimp prop; public Fightsmart (String prop,string Name) {this.prop=getprops (prop); This.name=name; public void Hitothers () {System.out.println ("Agile:" +getname ()); Prop.useprops (); }}
/** * The Concreteimplementor */public class Fightimpbrick implements fightimp{public void Useprops () {System.out.prin TLN ("Domestic tile, high quality and low price." If the use of satisfaction, please press 13XXXXXXXXX to continue to order "); }}
/** * The Concreteimplementor */public class Fightimpknife implements fightimp{public void Useprops () {System.out.pri Ntln ("thick Back Kill pig knife"); }}
/** * The Concreteimplementor */public class Fightimpempty implements fightimp{public void Useprops () {System.out.prin TLN ("The most Realm: Empty gloves White Wolf"); }}
Abstraction (fight)-Defines an interface for an abstract class. -maintains a pointer to the Implementor type object ... Refinedabstraction (Fightcrazy,fightsmart)-Expands the interface defined by abstraction. Implementor (Xximp)-Defines an interface to the implementation class that does not necessarily correspond exactly to the abstraction interface; in fact, the two interfaces can be completely different. Generally speaking, the Implementor interface provides only basic operations, while abstraction defines a higher level of operation based on these basic operations. Concreteimplementor (XXXIMP)-Implements the Implementor interface and defines its specific implementation.
5. Summary: 1. Bridge-mode execution classes such as "fighting" and "using props" are one-to-one relationships, and the proper creation of FIGHTIMP is the key to this model, 2. The abstract common part must have a interface as its attribute and a method as its initial entry: GetProps (); 3. In its "abstract common part" specific behavior process to add "behavior attribute" behavior, each refinedabstraction has an imp as its member variable, aggregated use
Link:
1. Design mode (the basis of reusable object-oriented software)
2. Design mode (Patterns in Java)--http://www.jdon.com
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.