Abstraction抽象类接口/**
* 抽象类接口
* Created by R on 2016/8/16.
*/publicabstractclassAbstraction {protected Implementor mImp;
publicAbstraction(Implementor implementor){
this.mImp = implementor;
}
publicabstractvoidmakeYouHappy();
}RefinedAbstractionA 、B抽象实现类A和B/**
* 抽象类实现类
* Created by R on 2016/8/16.
*/publicclassRefinedAbstractionAextendsAbstraction{publicRefinedAbstractionA(Implementor implementor) {
super(implementor);
}
@OverridepublicvoidmakeYouHappy() {
System.out.println("A类型的妹子");
mImp.skill();
}
}/**
* 抽象类实现类
* Created by R on 2016/8/16.
*/publicclassRefinedAbstractionBextendsAbstraction{publicRefinedAbstractionB(Implementor implementor) {
super(implementor);
}
@OverridepublicvoidmakeYouHappy() {
System.out.println("B类型的妹子");
mImp.skill();
}
}
Implementor实现类接口/**
* 实现类接口
* Created by R on 2016/8/16.
*/publicinterfaceImplementor {void skill();
}ConcreteImplementorA、B具体实现 A 和 B/**
* 具体实现
* Created by R on 2016/8/16.
*/publicclassConcreteImplementorAimplementsImplementor{@Overridepublicvoidskill() {
System.out.println("陪你撸啊撸~");
}
}/**
* 具体实现
* Created by R on 2016/8/16.
*/publicclassConcreteImplementorBimplementsImplementor{@Overridepublicvoidskill() {
System.out.println("陪你滚呀滚~");
}
}Client使用者/**
* 使用者
* Created by R on 2016/8/16.
*/publicclassClient {publicvoidmain(){
Implementor impA = new ConcreteImplementorA();
Implementor impB = new ConcreteImplementorB();
Abstraction absA = new RefinedAbstractionA(impA);
Abstraction absB = new RefinedAbstractionA(impB);
absA.makeYouHappy();
absB.makeYouHappy();
}
}输出:
A类型的妹子
陪你撸啊撸
B类型的妹子
陪你滚呀滚
总结
理解桥接模式,重点需要理解如何将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化。别名”柄体模式(Head and Body)”更能形象的表达他的模式结构,他由一个抽象类Head和一个实现接口Body。抽象话把不同的实体当做同样的实体对待;实现化针对抽象化给出的具体实现。他们之间是可逆的。
简而言之:同一个世界,不同的梦想!