Design mode (-----bridging mode)

Source: Internet
Author: User

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)

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.