将抽象部分与实现部分分离,使他们可以独立的变化。
package com.hml.bridge; /** * 实现部分 */ public abstract class Implementor { public abstract void operation(); }
package com.hml.bridge; public class ConcreateImplementorA extends Implementor { @Override public void operation() { System.out.println("A"); } }
package com.hml.bridge; public class ConcreateImplementorB extends Implementor { @Override public void operation() { System.out.println("B"); } }
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() ; }
package com.hml.bridge; public class RefinedAbstration extends Abstraction { @Override public void operation() { implementor.operation(); } }
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(); } }
桥接模式主要解决的问题是实现系统可能有多角度的分类,每一种分类都有可能变化,那么就把这种多角度分离出来让他们独立的变化,减少他们之间的耦合。
原文:http://www.cnblogs.com/heml/p/4652465.html