1类图
2定义
将抽象部分与实现部分分离,使他们都可以独立地进行变化。
3应用场景
3.1 如果一个系统需要在构建的抽象化角色和具体化角色之间增加更多的灵活性,避免这两个层次之间建立静态的继承联系,可以通过桥接模式使它们在抽象层建立一个关联关系。
通用模板
4实现部分的抽象接口
public interface Implementor {
public void operationImpl();
}
5实现部分具体的实现一
public class ConcreteImplementorA implements Implementor{
@Override
public void operationImpl() {
System.out.println("ConcreteImplementorA");
}
}
6实现部分具体的实现二
public class ConcreteImplementorB implements Implementor{
@Override
public void operationImpl() {
System.out.println("ConcreteImplementorB");
}
}
7抽象部分实现
public abstract class Abstraction {
private Implementor mImplementor;
public Abstraction(Implementor implementor) {
mImplementor = implementor;
}
public void operation(){
mImplementor.operationImpl();
}
}
8抽象部分的子类
public class RefinedAbstraction extends Abstraction {
private Implementor mImplementor;
public RefinedAbstraction(Implementor implementor) {
super(implementor);
mImplementor = implementor;
}
public void refinedOperation(){
System.out.println("RefinedAbstraction ");
mImplementor.operationImpl();
}
}
9客户类
public class Client {
public static void main(String[] args) {
Implementor implementorA = new ConcreteImplementorA();
Implementor implementorB = new ConcreteImplementorB();
RefinedAbstraction refinedA = new RefinedAbstraction(implementorA);
refinedA.refinedOperation();
System.out.println("---------------------");
RefinedAbstraction refinedB = new RefinedAbstraction(implementorB);
refinedB.refinedOperation();
}
}
简单实现
10咖啡类
public abstract class Coffee {
protected CoffeeAdditives impl;
public Coffee(CoffeeAdditives impl) {
this.impl = impl;
}
public abstract void makeCoffee();
}
11大杯
public class LargeCoffee extends Coffee {
public LargeCoffee(CoffeeAdditives impl) {
super(impl);
}
@Override
public void makeCoffee() {
System.out.println("大杯的" + impl.addSomething() + "咖啡");
}
}
12中杯
public class MiddleCoffee extends Coffee {
public MiddleCoffee(CoffeeAdditives impl) {
super(impl);
}
@Override
public void makeCoffee() {
System.out.println("中杯的" + impl.addSomething() + "咖啡");
}
}
13小杯
public class SmallCoffee extends Coffee {
public SmallCoffee(CoffeeAdditives impl) {
super(impl);
}
@Override
public void makeCoffee() {
System.out.println("小杯的" + impl.addSomething() + "咖啡");
}
}
14不同味的咖啡抽象类
public abstract class CoffeeAdditives {
public abstract String addSomething();
}
15加糖
public class Sugar extends CoffeeAdditives {
@Override
public String addSomething() {
return "加糖";
}
}
16原味
public class Ordinary extends CoffeeAdditives {
@Override
public String addSomething() {
return "原味";
}
}
17测试类
public class Test {
public static void main(String[] args) {
Ordinary implOrdinary = new Ordinary();
Sugar implSuhar = new Sugar();
LargeCoffee largeCoffeeOrdinary = new LargeCoffee(implOrdinary);
largeCoffeeOrdinary.makeCoffee();
LargeCoffee largeCoffeeSuhar = new LargeCoffee(implSuhar);
largeCoffeeSuhar.makeCoffee();
System.out.println("-----------------------------------");
SmallCoffee smallCoffeeOrdinary = new SmallCoffee(implOrdinary);
smallCoffeeOrdinary.makeCoffee();
SmallCoffee smallCoffeeSuhar = new SmallCoffee(implSuhar);
smallCoffeeSuhar.makeCoffee();
System.out.println("-----------------------------------");
MiddleCoffee middleCoffeeOrdinary = new MiddleCoffee(implOrdinary);
middleCoffeeOrdinary.makeCoffee();
MiddleCoffee middleCoffeeSuhar = new MiddleCoffee(implSuhar);
middleCoffeeOrdinary.makeCoffee();
}
}