Java Bridging mode (Liar design mode)

Source: Internet
Author: User

The bridging pattern is defined as separating the abstract part from its implementation, so that both can be independently changed.

The first time to look at the design pattern, not very clear the meaning of this sentence, along with the author's continuous development, found that there is a scene,

Inheritance relationship is more, not easy to maintain the parent class, and I think the emergence of bridging mode is a good solution to this problem. Resolving inherited parent classes with aggregations is difficult to maintain. First look at the bridge mode design.

Liar design mode-Class diagram

The above class diagram is very simple and easy to understand, first look at the author's demo

/***/Publicinterface  ioperate    {publicvoid  operate ();}
/*** Brand parent category*/ Public classBrand {PrivateIoperate operate;  PublicBrand (ioperate operate) {Super();  This. operate =operate; }     Public voidoperate () {operate.operate (); }     Publicioperate getoperate () {returnoperate; }     Public voidSetoperate (ioperate operate) { This. operate =operate; }}
 /**   * play games   */ public  class  Game implements   ioperate{@Override  public  void   operate () {System.out.prin    TLN ( play the game "
 /**   * see contacts   */ public  class  maillist implements   ioperate {@Override  public  void   operate () {SYSTEM.OUT.P    RINTLN ( see Address Book 
 /**   * M brand   */ public  class  mbrand extends   brand{ public   Mbrand (Ioperate operate) { super   (operate); }}
/***/Publicclassextends  brand{    public Nbrand (ioperate operate) {        Super(operate);    }}
/*** Client*/ Public classTest { Public Static voidMain (string[] args) {ioperate game=NewGame (); Ioperate maillist=Newmaillist (); Brand Nbrand=NewNbrand (game);        Nbrand.operate (); Nbrand=NewNbrand (maillist);        Nbrand.operate (); Brand Mbrand=NewMbrand (game);        Mbrand.operate (); Mbrand=NewMbrand (maillist);    Mbrand.operate (); }}

The operation results are as follows

Play games see contacts play games See contacts

Better understand, but I think we in the actual development, may be used in many places bridge mode, but we all forget this mode, and with inheritance to achieve.

Still the old saying, understand its core thought, refuse to mechanically. A small partner who wants to help learn bridging patterns.

Java Bridging mode (Liar design mode)

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.