Bridge mode in java (big talk design mode)

Keywords: Java

Bridging mode is defined as separating the abstract part from its implementation part so that they can change independently.

When I first looked at the design mode, I didn't know the meaning of this sentence very well. With the continuous development of the author, I found a scene.

There are so many inheritance relationships that it is not easy to maintain the parent class, but I think the appearance of bridging mode solves this problem very well.Resolve inherited parent maintainability with aggregation.First look at the bridge mode design.

Dahua Design Mode-Class Diagram

The above kind of diagrams are very easy to understand, let's first look at the author's demo

/**
 * Operational Interface
 */
public interface IOperate {

    public void operate();
}
/**
 * Brand Parent
 */
public class Brand {

    private IOperate operate;


    public Brand(IOperate operate) {
        super();
        this.operate = operate;
    }

    public void operate() {
        operate.operate();
    }

    public IOperate getOperate() {
        return operate;
    }

    public void setOperate(IOperate operate) {
        this.operate = operate;
    }
}
/**
 * Play a game
 */
public class Game implements IOperate{
    @Override
    public void operate() {
        System.out.println("Play a game");
    }

}
/**
 * Look at the address book
 */
public class MailList implements IOperate {
    @Override
    public void operate() {
        System.out.println("Look at the address book");
    }

}
/**
 * M brand
 */
public class MBrand extends Brand{

    public MBrand(IOperate operate) {
        super(operate);
    }

}
/**
 * N brand
 */
public class NBrand extends Brand{

    public NBrand(IOperate operate) {
        super(operate);
    }

}
/**
 * Client
 */
public class Test {
    public static void main(String[] args) {
        IOperate game = new Game();
        IOperate mailList = new MailList();

        Brand nBrand = new NBrand(game);
        nBrand.operate();

        nBrand = new NBrand(mailList);
        nBrand.operate();

        Brand mBrand = new MBrand(game);
        mBrand.operate();

        mBrand = new MBrand(mailList);
        mBrand.operate();
    }
}

 

The results are as follows

Play a game
 Look at the address book
 Play a game
 Look at the address book

 

Good understanding, but the author thinks that in our actual development, bridging mode may be used in many places, but everyone forget this mode and use inheritance to achieve it.

Or the old saying, understand its core ideas and refuse to copy hard.A small partner who wants to help learn bridging patterns.

Posted by skip on Thu, 07 May 2020 10:11:08 -0700