Talk programming - bridge mode

Bridge Pattern

Separate the abstract part from its implementation part so that they can all change independently.

motivation

When an abstraction can have multiple implementations, inheritance is usually used to coordinate them. Abstract classes define the interface of the object, while concrete subclasses are implemented in different ways. But sometimes this method is not flexible enough. Inheritance mechanism fixes the abstract part and its implementation part together, which makes it difficult to modify, expand and reuse the abstract part independently.

Applicable scenarios

  • You don't want a fixed binding between the abstraction and its implementation. For example, this is because the implementation part can be selected or switched when the program is running.

  • Class abstraction and its implementation can be extended by generating subclasses. The bridge pattern allows you to combine and extend different abstract interfaces and implementation parts.

  • Changes to an abstract implementation do not affect the customer.

  • Multiple objects share the implementation, but at the same time the customer is not aware of this.

structure

Code example

//Abstract class, TV related operation
public abstract class RemoteCotrol {

    private TV tv;

    public RemoteCotrol(TV tv) {
        this.tv = tv;
    }

    public TV getTv() {
        return tv;
    }

    public abstract void on();
    public abstract void off();
    public abstract void setChannel();
}
//tv interface 
public interface TV {

    void on();

    void off();

    void tuneChannel();
}
//tv interface implementation class to realize the operation related to tv series
public class RCA implements TV {

    @Override
    public void on() {
        System.out.println("rca on");
    }

    @Override
    public void off() {
        System.out.println("rca off");
    }

    @Override
    public void tuneChannel() {
        System.out.println("rca CCTV");
    }
}
//tv interface implementation class to realize the operation related to tv series
public class Sony implements TV {
    @Override
    public void on() {
        System.out.println("sony on");
    }

    @Override
    public void off() {
        System.out.println("sony off");
    }

    @Override
    public void tuneChannel() {
        System.out.println("sony cctv");
    }
}
//Abstract implementation class, association interface, use interface to implement Abstract TV series related operations
public class ConsreteRemode extends RemoteCotrol {

    public ConsreteRemode(TV tv) {
        super(tv);
    }

    @Override
    public void on() {
        getTv().on();
    }

    @Override
    public void off() {
        getTv().off();
    }

    @Override
    public void setChannel() {
        getTv().tuneChannel();
    }
}
public class Client{
  public static void main(String[] args) {
        RemoteCotrol sony = new ConsreteRemode(new Sony());
        sony.on();

        RemoteCotrol rca = new ConsreteRemode(new RCA());
        rca.on();
    }
}

: sony on : rca on

summary

Bridging patterns can help us decouple abstraction and implementation completely. This pattern also conforms to the principle of composition / aggregation reuse, that is, the composition or aggregation of objects is preferred rather than inheritance. The reason is that inheritance is a strong coupling structure. If the parent class changes, the child class must change.

Welcome to personal blog

Keywords: Programming

Added by Loki on Sat, 30 May 2020 18:36:09 +0300