(10) Bridging mode-code implementation

Source: Internet
Author: User

Intention: To separate the abstract part from its implementation, so that they can all change independently.

Usage scenario: When there are multiple method definitions in one port, there are many different implementations of its methods, in order to avoid repeating the same methods in subclasses.

Peel Function 1 interface

 Package Note.com.bridge; /**  @author*/Publicinterface  icolour        {  publicvoid  buildcolour ();    }

Feature 1 implementation

 package   Note.com.bridge;  public  class  Blue implements   icolour{ public  void   Buildcolour () {System.out.    println ( "Blue" ); }}
 Package Note.com.bridge;  Public class Implements icolour{    publicvoid  buildcolour () {        System.out.println ("Red");}    } 

Peel function 2 interface

 Package Note.com.bridge; /**  @author*/Publicinterface  itaste    {  publicvoid  buildtaste ();    }

Feature 2 Implementation

 package   Note.com.bridge;  /**   * Sweet *   @author   Lxz *  */ public  class  Sweet implements   itaste{ public  void   Buildtaste () {SYSTEM.OUT.P    Rintln ( "Sweet" ); }}
 Package Note.com.bridge; /**  @author*/Publicclassimplements  itaste{      Public void Buildtaste () {        System.out.println ("sour");}    }

A true functional interface

 PackageNote.com.bridge;/*** Beverage base class *@authorLXZ **/ Public Abstract classAdrink {PrivateIcolour colour =NULL; PrivateItaste taste =NULL;  Publicadrink (icolour colour,itaste taste) { This. Colour =colour;  This. Taste =Taste; }         Public Abstract voidfinish ();  PublicIcolour Getcolour () {returncolour; }     Public voidSetcolour (Icolour colour) { This. Colour =colour; }     Publicitaste Gettaste () {returnTaste; }     Public voidsettaste (Itaste taste) { This. Taste =Taste; }            }

Functional interface Implementation Class

 Package Note.com.bridge;  Public class extends adrink{    public  Drink (icolour colour,itaste taste) {        super( colour,taste);    }        @Override    publicvoid  finish () {        System.out.println ("wine:");        Getcolour (). Buildcolour ();        Gettaste (). Buildtaste ();    }}

Test class

 PackageNote.com.bridge; Public classDrinktest { Public Static voidMain (string[] args) {Icolour Blue=NewBlue (); Itaste Sweet=NewSweet (); Drink Drink1=NewDrink (Blue,sweet);        Drink1.finish (); Icolour Red=NewRed (); Itaste Sour=NewSour (); Drink Drink2=NewDrink (Red,sour);    Drink2.finish (); }}

Results

Wine:
Blue
Sweet
Wine:
Red
Sour

(10) Bridging mode-code implementation

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.