桥接模式
Posted 啊哈咧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了桥接模式相关的知识,希望对你有一定的参考价值。
桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化
Abstraction
package design.pattern.bridge; public abstract class Abstraction { protected Implementor impl; public Abstraction(Implementor impl) { this.impl = impl; this.impl.setAbstraction(this); } public void operation() { impl.operation(); } }
Implementor
package design.pattern.bridge; public interface Implementor { public void operation(); public void setAbstraction(Abstraction abstraction); }
ConcreteAbstraction
package design.pattern.bridge; public class ConcreteAbstraction extends Abstraction { public ConcreteAbstraction(Implementor impl) { super(impl); } @Override public void operation() { System.out.println("ConcreteAbstraction do sth"); impl.operation(); } }
ConcreteImplementor
package design.pattern.bridge; public class ConcreteImplementor implements Implementor{ private Abstraction abstraction; public void setAbstraction(Abstraction abstraction) { this.abstraction = abstraction; } @Override public void operation() { System.out.println("ConcreteImplementor do sth with: " + abstraction); } }
Client
package design.pattern.bridge; public class Client { public static void main(String[] args) { new ConcreteAbstraction(new ConcreteImplementor()).operation(); } }
以上是关于桥接模式的主要内容,如果未能解决你的问题,请参考以下文章