《设计模式》8.桥接模式(结构型)

将多维度变化的事物,通过抽象化和行为实现化将分离解耦,使得它们各自可以独立变化,针对行为进行桥接。

角色

抽象化(Abstraction):抽象类,作为桥接类,包含对实现化对象的引用,使实现化对象可以相互组合
扩展抽象化(Refined Abstraction):抽象化的子类,调用实现化中的行为方法
实现化(Implementor):行为接口,供扩展抽象化角色调用
具体实现化(Concrete Implementor):实现化接口的具体实现

抽象化 Abstraction

可通过 构造函数 或者 方法设置 等方式设置 实现化对象的引用

public abstract class Abstraction {
    protected Implementor implementor;
    public Abstraction(Implementor implementor) {
        this.implementor = implementor;
    }

	public void setImplementor(Implementor implementor) {
		this.implementor = implementor;
	}

    public abstract void operation();

	public abstract void action();
}

扩展抽象化 Refined Abstraction

public class RefinedAbstraction extends Abstraction {
    public RefinedAbstraction(Implementor implementor){
        super(implementor);
    }

    public void operation() {
        System.out.println("do something...");
    }

	public void action() {
		implementor.action();
	}
}

实现化 Implementor

public interface Implementor {
    void action();
}

具体实现化 ConcreteImplementor

public class ConcreteImplementor implements Implementor {
    public void operationImp() {
        System.out.println("Concrete Implementor...");
    }
}

通过汽车的分类举例:
汽车可通过:品牌,车型,颜色 等分类
品牌可分为:Benz、BMW、Lexus…
车型可分为:紧凑型SUV、SUV、MPV…
颜色可分为:白色、黑色、灰色…

选取品牌作为抽象化的主属性,车型和颜色作为实现化行为:

抽象化 Car

public abstract class Car {
    protected static final String TEMPLATE = "This is a %s %s %s car";
    private String brand;
    private Color color;
    private Model model;

    public Car(String brand) {
        this.brand = brand;
    }

    public abstract void specification();

    public String getBrand() {
        return brand;
    }

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

    public void setColor(Color color) {
        this.color = color;
    }

    public String getModel() {
        return this.model.getModel();
    }

    public void setModel(Model model) {
        this.model = model;
    }
}

扩展抽象化 Benz / BMW / Lexus

public class Benz extends Car {
    public Benz() {
        super("Benz");
    }

    @Override
    public void specification() {
        System.out.println(String.format(TEMPLATE, getColor(), getModel(), getBrand()));
    }
}

public class Bmw extends Car {
    public Bmw() {
        super("BMW");
    }

    @Override
    public void specification() {
        System.out.println(String.format(TEMPLATE, getColor(), getModel(), getBrand()));
    }
}

public class Lexus extends Car {
    public Lexus() {
        super("Lexus");
    }

    @Override
    public void specification() {
        System.out.println(String.format(TEMPLATE, getColor(), getModel(), getBrand()));
    }
}

实现化 Model / Color

public interface Model {
    String getModel();
}

public interface Color {
    String getColor();
}

具体实现化 CompactSuv / Suv / Mpv / White / Black / Gray

public class CompactSuv implements Model {
    @Override
    public String getModel() {
        return "compact SUV";
    }
}

public class Suv implements Model {
    @Override
    public String getModel() {
        return "SUV";
    }
}

public class Mpv implements Model {
    @Override
    public String getModel() {
        return "MPV";
    }
}

public class White implements Color {
    @Override
    public String getColor() {
        return "white";
    }
}

public class Black implements Color {
    @Override
    public String getColor() {
        return "black";
    }
}

public class Gray implements Color {
    @Override
    public String getColor() {
        return "gray";
    }
}

测试类

public class BridgeTester {
    public static void main(String args[]) {
        Car benz = new Benz();
        benz.setColor(new Black());
        benz.setModel(new Suv());
        benz.specification();

        Car bmw = new Bmw();
        bmw.setColor(new White());
        bmw.setModel(new CompactSuv());
        bmw.specification();

        Car lexus = new Lexus();
        lexus.setColor(new Gray());
        lexus.setModel(new Mpv());
        lexus.specification();
    }
}
This is a black SUV Benz car
This is a white compact SUV BMW car
This is a gray MPV Lexus car

参考:
https://blog.csdn.net/javaloveiphone/article/details/85226795
https://www.cnblogs.com/lixiuyu/p/5923160.html
https://www.cnblogs.com/chenssy/p/3317866.html
http://c.biancheng.net/view/1364.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值