Design Mode-Bridge)

Source: Internet
Author: User

Bridging Mode

Overview

Separate the abstract part from its implementation part so that they can all change independently.
Applicability
1. You do not want to have a fixed binding relationship between the abstraction and its implementation. For example, this may be because the implementation part of the program should be selected or switched during the runtime. 2. Class abstraction and its implementation should be expanded by generating subclass methods. In this case, the bridge mode allows you to combine different abstract interfaces and implementation parts and expand them separately. 3. Modifications to an abstract implementation part should not affect the customer, that is, the customer's Code does not have to be re-compiled. 4. Many classes need to be generated. This hierarchical structure indicates that you must break an object into two parts. 5. You want to share the implementation among multiple objects (reference count may be used), but the customer is not required to know this.
Participatory
1. Define Action defines the abstract class interface. Maintain a pointer to an implementor object. 2. refinedabstraction expands the interface defined by the specified action. 3. implementor defines the interface for implementing classes. This interface does not have to be exactly the same as the interface for implementing actions. In fact, these two interfaces can be completely different. Generally, the implementor interface only provides basic operations, while the role Action defines high-level operations based on these basic operations. 4. concreteimplementor implements the implementor interface and defines its specific implementation.
Your action public abstract class Person {private Clothing clothing; private String type; public Clothing getClothing () {return clothing;} public void setClothing () {this. clothing = ClothingFactory. getClothing ();} public void setType (String type) {this. type = type;} public String getType () {return this. type;} public abstract void dress ();} refined1_action public class Man extends Person {public Man () {setType ("Man");} public void dress () {Clothing clothing = getClothing (); clothing. personDressCloth (this) ;}} public class Lady extends Person {public Lady () {setType ("") ;}public void dress () {Clothing clothing = getClothing (); clothing. personDressCloth (this) ;}} Implementor public abstract class Clothing {public abstract void personDressCloth (Person person);} ConcreteImplementor public class Jacket extends Clothing {public void personDressCloth (Person person Person. out. println (person. getType () + "wear vest") ;}} public class Trouser extends Clothing {public void personDressCloth (Person person) {System. out. println (person. getType () + "Pants") ;}} Test public class Test {public static void main (String [] args) {Person man = new Man (); person lady = new Lady (); Clothing jacket = new Jacket (); Clothing trouser = new Trouser (); jacket. personDressCloth (man); trouser. personDressCloth (man); jacket. personDressCloth (lady); trouser. personDressCloth (lady) ;}result men wear vests men wear trousers women wear vests women wear pants

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.