桥接模式
将抽象部分与实现部分分离,使他们可以独立的变化。
Implementor
package com.hml.bridge; /** * 实现部分 */ public abstract class Implementor { public abstract void operation(); }
ConcreateImplementorA
package com.hml.bridge; public class ConcreateImplementorA extends Implementor { @Override public void operation() { System.out.println("A"); } }
ConcreateImplementorB
package com.hml.bridge; public class ConcreateImplementorB extends Implementor { @Override public void operation() { System.out.println("B"); } }
Abstraction
package com.hml.bridge; /** * 抽象部分 */ public abstract class Abstraction { protected Implementor implementor; public Implementor getImplementor() { return implementor; } public void setImplementor(Implementor implementor) { this.implementor = implementor; } public abstract void operation() ; }
RefinedAbstration
package com.hml.bridge; public class RefinedAbstration extends Abstraction { @Override public void operation() { implementor.operation(); } }
Test
package com.hml.bridge; public class Test { public static void main(String[] args) { Abstraction abs = new RefinedAbstration(); abs.setImplementor(new ConcreateImplementorA()); abs.operation(); abs.setImplementor(new ConcreateImplementorB()); abs.operation(); } }
类图
![](https://images0.cnblogs.com/blog2015/559833/201507/162042325322434.png)
桥接模式主要解决的问题是实现系统可能有多角度的分类,每一种分类都有可能变化,那么就把这种多角度分离出来让他们独立的变化,减少他们之间的耦合。