码迷,mamicode.com
首页 > 其他好文 > 详细

桥接模式

时间:2017-10-22 01:40:15      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:pattern   ima   ges   xtend   jpg   cte   string   桥接   protected   

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

 

桥接模式

标签:pattern   ima   ges   xtend   jpg   cte   string   桥接   protected   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!