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