Java design pattern--Structural mode--Bridging mode

Source: Internet
Author: User

1 Bridging Mode2 Overview3 separate the abstract part from its implementation, so that they can all change independently. 4  5  6 Applicability71you do not want to have a fixed binding relationship between the abstraction and its implementation section. 8 For example, this may be because the implementation part of the program run time should be selectable or switched. 9 Ten2The abstraction of the . class and its implementation should be augmented by methods of generating subclasses.  One Bridge mode allows you to combine different abstract interfaces and implementation parts and expand them separately.  A  -3The modification of an abstract implementation section should not have an impact on the customer that the customer's code does not have to be recompiled.  -  the4. As shown in the first class diagram in the intent section, there are many classes to build.  - such a class hierarchy illustrates that you must break up an object into two parts.  -  -5you want to share the implementation between multiple objects (which may use reference counting), but at the same time ask the customer not to know this.  +   -   + participants A1. Abstraction at defines an interface for an abstract class.  - maintains a pointer to an Implementor type object.  -  -2. Refinedabstraction - expands the interface defined by abstraction.  -  in3. Implementor - defines an interface for the implementation class that does not have to be exactly the same as the abstraction interface.  to 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.  -  the4. Concreteimplementor *Implement the Implementor interface and define its specific implementation.

Test class:

1  Public classTest {2 3      Public Static voidMain (string[] args) {4         5Person mans =NewMan ();6         7Person lady =NewLady ();8         9Clothing jacket =NewJacket ();Ten          OneClothing trouser =Newtrouser (); A          - Jacket.persondresscloth (man); - Trouser.persondresscloth (man); the  - Jacket.persondresscloth (lady); - Trouser.persondresscloth (lady); -     } +}

1  Public Abstract classPerson {2 3     Privateclothing clothing;4     5     PrivateString type;6 7      PublicClothing getclothing () {8         returnClothing;9     }Ten  One      Public voidsetclothing () { A          This. Clothing =clothingfactory.getclothing (); -     } -      the      Public voidsetType (String type) { -          This. Type =type; -     } -      +      PublicString GetType () { -         return  This. Type; +     } A      at      Public Abstract voiddress (); -}

1  Public Abstract class Clothing {23      Public Abstract void Persondresscloth (person person); 4 }

1  Public classLadyextendsPerson {2 3      PublicLady () {4SetType ("Woman");5     }6     7      Public voiddress () {8Clothing clothing =getclothing ();9Clothing.persondresscloth ( This);Ten     } One}

1  Public Abstract class Clothing {23      Public Abstract void Persondresscloth (person person); 4 }

1  Public class extends Clothing {23      Public void Persondresscloth (person person) {4         System.out.println (Person.gettype () + "Wear vest"); 5     }6 }

1@SuppressWarnings ("Static-access")2  Public classClothingfactory {3     Private Staticclothing clothing;4     5      Publicclothingfactory (clothing clothing) {6          This. Clothing =Clothing;7     }8     9      Public StaticClothing getclothing () {Ten         returnClothing; One     } A  -}

1  Public class extends Clothing {23      Public void Persondresscloth (person person) {4         System.out.println (Person.gettype () + "Wear pants"); 5     }6 }

Java design pattern--Structural mode--Bridging mode

Related Article

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.