"Graphic design patterns," the study notes 4-1 Bridge mode

concept

That bridge mode Bridge mode. As the name suggests, this mode of action is the class of functional hierarchy and classes implemented hierarchy connected.

  • Function Hierarchy
Something
    -SomethingGood
        -SomethingBetter

Add a subclass of a parent class does not function, the structure is functional hierarchy between them.

  • Realization hierarchy
AbstractClass
    -ConcreteClass
    -AnotherConcreteClass

In a template method pattern, for example, the parent class declares an abstract method defines abstract methods of use; subclass inherits and implements the abstract methods - to achieve a separation of duties: concerned parent process, child care class implementation details, like leading the charge distribution of tasks, the staff responsible for completing the task. Such a relationship between parent and child classes is to achieve a hierarchical structure. Note that: the subclass does not add features just realized the function .

When adding a subclass to make it clear: I want to add new features, or to achieve another increase ? If mixed together, the structure will be more complex, it is not clear;

But if completely separate, they will lack contact, so we need to build a bridge - Bridge Mode, specific practices: use inheritance extension, use a delegate functions to achieve

Code

  • father
public class Display {
    private DisplayImpl displayImpl;
    public Display(DisplayImpl displayImpl) {
        this.displayImpl = displayImpl;
    }

    public void open() {
        displayImpl.rawOpen();
    }
    public void print() {
        displayImpl.rawPrint();
    }
    public void close() {
        displayImpl.rawClose();
    }
    public final void display() {
        open();
        print();
        close();
    }
}
  • Functional class hierarchy
public class CountDisplay extends Display{
    public CountDisplay(DisplayImpl displayImpl) {
        super(displayImpl);
    }

    public void multiDisplay(int times) {
        open();
        for (int i = 0; i < times; i++) {
            print();
        }
        close();
    }
}
  • Implementation class hierarchy
public abstract class DisplayImpl {
    public abstract void rawOpen();
    public abstract void rawPrint();
    public abstract void rawClose();
}
public class StringDisplayImpl extends DisplayImpl {
    private String string;                              // 要显示的字符串
    private int width;                                  // 以字节单位计算出的字符串的宽度
    public StringDisplayImpl(String 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++) {               // 显示width个"-"
            System.out.print("-");                      // 将其用作方框的边框
        }
        System.out.println("+");                        // 显示用来表示方框的角的"+"
    }
}
  • run
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);
}

/*结果*/
+-------------+
|Hello, China.|
+-------------+
+-------------+
|Hello, World.|
+-------------+
+----------------+
|Hello, Universe.|
+----------------+
+----------------+
|Hello, Universe.|
|Hello, Universe.|
|Hello, Universe.|
|Hello, Universe.|
|Hello, Universe.|
+----------------+

Character

Abstraction (abstraction) : located at the top of the hierarchy of functional classes, the most basic definition, saving of Implementor example, in this embodiment, implemented by the Display class.

RefinedAbstraction (the improved abstract) : increase the role of new features on the basis of Abstraction, in the present example, this role played by the CountDisplay class.

The Implementor (implementors) : defines the methods for achieving Abstraction interfaces, in the present example this role played by the DisplayImpl.

ConcreteImplementor (specific implementer) : Implementor responsible for implementing the interface defined. In this case, the StringDisplayImpl this role.

Class Diagram

idea

  • After separate more easily extended

Features bridge mode is the class of functional hierarchies expand and classes implement hierarchy expand separated, so that the benefits are in favor of independence for them to expand.

For example, in a Windows, MacOS, Linux software running on the need to develop three versions. We can use the bridge mode, the definition of a Implementor, then write each version corresponding ConcreteImplementor, thus completing the implementation level of development. Next, whether functional level need to increase the number of functions, it can work on all three operating systems.

  • Inheritance and delegation

Inheritance is a strong relationship, trust is a weak relationship.

Implement three methods of observation open the Display class, close, print, they are calling the method DisplayImpl. Display category will be handed over to DisplayImpl working class, which is called trust. We can achieve a variety of XXXDisplayImpl, which is passed as a parameter inside the constructor Display and modify the implementation without changing the premise Display class and the lower class DisplayImpl. This is the commission the benefits.

Guess you like

Origin www.cnblogs.com/qianbixin/p/10992918.html