Android设计模式之结构型模式

桥接模式java

定义: 将抽象部分与它的实现部分分离,使它们均可以独立地变化.ide

/**
 * 抽象类
 */
public abstract class Implementor{
    public abstract void operation();
}

/**
 * Implementor 派生类 ImplementorA
 */
public class ImplementorA extends Implementor{

    @Override
    public void operation() {
        System.out.println("classA");
    }
}
/**
 * Implementor 派生类 ImplementorB
 */
public class ImplementorB extends Implementor{
    
    @Override
    public void operation() {
        System.out.println("classB");
    }
}

public abstract class Abstraction{
    protected Implementor implementor;
    
    public void setImplementor(Implementor implementor){
        this.implementor = implementor;
    }
    public abstract void operation();
}

public class RefinedAbstraction extends Abstraction{

    @Override
    public void operation() {
        implementor.operation();
    }
}
/**
 * 客户端代码
 * @param args
 */
public static void main (String[] args){
    Abstraction abstraction = new RefinedAbstraction();
    abstraction.setImplementor(new ImplementorA());
    abstraction.operation();
    
    abstraction.setImplementor(new ImplementorB());
    abstraction.operation();
}

使用情况:this

1.不但愿在抽象和它的实现之间有一个固定的绑定关系时.spa

2.类的抽象以及他的实现都应该能够经过生成子类的方法加以扩充时.code

3.对一个抽象类的实现部分的修改应该对客户不产生影响时.对象

适配器模式接口

定义: 将一个类的接口转换成客户但愿的另一个接口io

适用范围: 当想要使用一个已经存在的类,可是该类的接口不符合现有的要求时.class

               当须要建立一个能够被复用的类,该类可以与其余无关的类甚至没法预见的类协同工做时.方法

               当须要使用一些已经存在的子类,可是不可能对全部的都进行子类化以匹配它们的接口时,对象适配器能够对其父

               类接口进行适配.

相关文章
相关标签/搜索