Bridging mode-bridge (Java implementation)

Source: Internet
Author: User

Bridging mode-bridge

The bridge pattern is intended to "decouple abstraction (abstraction) from implementation (implementation), separating the functional hierarchy of a class from the implementation hierarchy of a class into two separate class hierarchies."

Class implementation hierarchy Interface Displayimpl interface
Public interface Displayimpl {    void Rawopen ();    void Rawprint ();    void Rawclose ();}
Stringdisplayimpl class
public class Stringdisplayimpl implements Displayimpl {    private string string;    private int width;    Public Stringdisplayimpl (String string) {        this.string = string;        This.width = String.getbytes (). length;    }    public void Rawopen () {        printline ();    }    public void Rawprint () {        System.out.println ("|" + String + "|");    public void Rawclose () {        printline ();    }    private void PrintLine () {        System.out.print ("+");        for (int i = 0; i < width; i++) {            System.out.print ("-");        }        System.out.println ("+");}    }
Class's functional hierarchy display class

The Displayimpl interface is associated at the highest level of the feature implementation, where it is bridged.

public class Display {    private displayimpl impl;    Public Display (Displayimpl impl) {        This.impl = impl;    }    public void Open () {        impl.rawopen ();    }    public void print () {        impl.rawprint ();    }    public void Close () {        impl.rawclose ();    }    Public final void display () {        open ();        Print ();        Close ();    }}
CountDisplay class

On the basis of the display class, the function is extended. Extend a multidisplay () feature.

public class CountDisplay extends Display {public    countdisplay (Displayimpl impl) {        super (IMPL);    }    public void Multidisplay (int times) {        open ();        for (int i = 0; I < times; i++) {            print ();        }        Close ();    }}
Main

For Test runs

public class Main {public    static void Main (string[] args) {        display D1 = new display (New Stringdisplayimpl ("Hello , China. "));        Display D2 = new CountDisplay (New Stringdisplayimpl ("Hello, World"));        CountDisplay d3 = new CountDisplay (New Stringdisplayimpl ("Hello, Universe."));        D1.display ();        D2.display ();        D3.display ();        D3.multidisplay (5);}    }

Bridging mode-bridge (Java 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.