桥接模式

结构型模式

桥接模式

桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化,通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦

优点

缺点

使用场景

示例

public class brigdePattern {
    public static void main(String[] args) {
        xiaoMian xm = new xiaoMian(new hotStyle(), new redColor());
        System.out.println(xm.eat());
        laMian lm = new laMian(new lightStyle(), new greenColor());
        System.out.println(lm.eat());
    }

}

interface style {
    String getName();
}

class hotStyle implements style {
    public String getName() {
        return "hot style";
    }
}

class lightStyle implements style {
    public String getName() {
        return "light style";
    }
}

interface color {
    String getColor();
}

class greenColor implements color {
    public String getColor() {
        return "green";
    }
}

class redColor implements color {
    public String getColor() {
        return "red";
    }
}

abstract class nodle {
    private style style;
    private color color;

    nodle(style style, color color) {
        this.color = color;
        this.style = style;
    }

    public String getStyle() {
        return style.getName();
    }

    public void setStyle(style style) {
        this.style = style;
    }

    public String getColor() {
        return color.getColor();
    }

    abstract String getName();

    abstract String eat();
}

class xiaoMian extends nodle {

    xiaoMian(style style, color color) {
        super(style, color);
    }

    public String getName() {
        return "xiaomian";
    }

    public String eat() {
        return "eat color:" + this.getColor() + "style:" + this.getStyle() + getName();
    }
}

class laMian extends nodle {
    laMian(style style, color color) {
        super(style, color);
    }

    public String getName() {
        return "laMian";
    }

    public String eat() {
        return "eat color:" + this.getColor() + "style:" + this.getStyle() + getName();
    }
}