首页 > 其他 > 详细

桥接模式

时间:2017-10-22 00:55:08      阅读:187      评论:0      收藏:0      [点我收藏+]

桥接(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();
    }
}

 

桥接模式

原文:http://www.cnblogs.com/m2492565210/p/7461549.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!