桥接模式:用于把抽象化与实现化解耦,使得二者可以独立变换。这种涉及模式属于结构型模式,通过抽象化与实现化之间的桥接结构,实现二者的解耦。
1.先定义Implementor接口的,代码如下:
public interface Implementor{ public void operationImpl(); }
2.定义Abstraction接口,器实现成为抽象类,代码如下:
public abstract class Abstraction{ protected Implementor impl; public Abstraction(Implemntor impl){ this.imple=impl; } public void operation(){ imple.opration(); } }
3.具体的实现如下:
public class ConcreateImpletorA implements Implementor{ public void operationImpl(){} } public class RefinedAbstraction extends Abstraction{ public RefinedAbstraction(Implementor impl){ } public void otherOperation(){} }
考虑这样的一个业务需求,发送消息提示,从业务看消息又分成普通消息,加急消息和特急消息,发送的方式分为系统内短消息和邮件,sms等。通过桥接模式进行设计。