标签:
定义:将抽象和实现解耦,使得两者可以独立地变化
类图:
这个模式比较抽象,个人理解,该模式是通过组合的当时来实现一个大的封装,而不是继承的方式。
代码:
public interface Implement { public void doSomething(); public void doAnyting(); }
public class ConcreteImplement implements Implement{ @Override public void doSomething() { // TODO Auto-generated method stub } @Override public void doAnyting() { // TODO Auto-generated method stub } }
public abstract class Abstraction { private Implement implement; public Abstraction(Implement implement){ this.implement = implement; } public void request(){ implement.doSomething(); } public Implement getImp(){ return implement; } }
public class RefinedAbstraction extends Abstraction{ public RefinedAbstraction(Implement implement) { super(implement); // TODO Auto-generated constructor stub } @Override public void request(){ super.request(); super.getImp().doAnyting(); } }
public class Client { public static void main(String[] args){ Implement imp = new ConcreteImplement(); Abstraction abstraction = new RefinedAbstraction(imp); abstraction.request(); } }
抽象和实现隔离
优秀的扩充能力
实现细节对客户透明
实例讲解:点击打开链接
标签:
原文地址:http://blog.csdn.net/renpengddxx/article/details/52162499