Bridging mode (bridge) definition
separate The abstractions from the implementation , so that they can be changed independently.
UML Class Diagram
Role
Bridge mode is based on the minimal design principle of the class, and allows different classes to assume different responsibilities by using behaviors such as encapsulation, aggregation, and inheritance. Its main feature is to separate the abstraction (abstraction) from the Behavior implementation (implementation), so that the independence of each part can be maintained and the function extension should be addressed.
The concreteimplementor is the realization of the burst, and the refineabstraction is the abstraction, let the two separate, is the bridge mode main function.
Example
The remote control allows the TV to perform a variety of functions.
Implementor
PackageCom.csdhsm.pattemdesign.bridge;/*** @Title: Itv.java * @Description: TV Function Interface *@author: Han * @date: June 27, 2016 PM 1:48:07*/ Public InterfaceITV {//turn on the TV Public voidOn (); //turn off the TV Public voidoff (); //Select Channel Public voidSwitchchannel (intchannel);}
Concrete Implementation Class
PackageCom.csdhsm.pattemdesign.bridge;/*** @Title: Samsungtv.java * @Description: Samsung TV *@author: Han * @date: June 27, 2016 morning 11:25:28*/ Public classSamsungtvImplementsITV {@Override Public voidOn () {System.out.println ("Samsung is turned on."); } @Override Public voidoff () {System.out.println ("Samsung is turned off."); } @Override Public voidSwitchchannel (intChannel) {System.out.println ("Samsung:channel-" +channel); }}
PackageCom.csdhsm.pattemdesign.bridge;/*** @Title: Sonytv.java * @Description: Sony TV *@author: Han * @date: June 27, 2016 morning 11:27:49*/ Public classSonytvImplementsITV {@Override Public voidOn () {System.out.println ("Sony is turned on."); } @Override Public voidoff () {System.out.println ("Sony is turned off."); } @Override Public voidSwitchchannel (intChannel) {System.out.println ("Sony:channel-" +channel); }}
Remote Control abstract class
PackageCom.csdhsm.pattemdesign.bridge;/*** @Title: Abstractremotecontrol.java * @Description: Remote control abstract class containing a reference to TV *@author: Han * @date: June 27, 2016 PM 1:25:21*/ Public Abstract classAbstractremotecontrol {PrivateITV TV; PublicAbstractremotecontrol (ITV TV) { This. TV =TV; } Public voidturnOn () {Tv.on (); } Public voidturnoff () {Tv.off (); } Public voidSetchannel (intChannel) {Tv.switchchannel (channel); }}
The specific implementation class of the remote control
PackageCom.csdhsm.pattemdesign.bridge;/*** @Title: Logitechremotecontrol.java * @Description: Specific implementation class of the remote control *@author: Han * @date: June 27, 2016 PM 1:51:53*/ Public classLogitechremotecontrolextendsAbstractremotecontrol { PublicLogitechremotecontrol (ITV TV) {Super(TV); } Public voidSetchannelkeyboard (intChannel) { Super. Setchannel (channel); System.out.println ("Logitech use keyword to set channel."); }}
Client
Package Com.csdhsm.pattemdesign.bridge; Public class Solution { publicstaticvoid main (string[] args) { new Sonytv (); New Logitechremotecontrol (TV); Lrc.setchannelkeyboard (+); }}
Summarize
The key of bridging mode is to separate the abstraction from the behavior realization and combine it dynamically.
Design mode (-----bridging mode)