b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

解释器设计模式_lua解释器交互模式_解释器模式c语言(7)

电脑杂谈  发布时间:2017-02-22 18:11:38  来源:网络整理
public class Client {
    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.setId("1");
        ShapeCache.setPrototype(circle.getId(),circle);

        Square square = new Square();
        square.setId("2");
        ShapeCache.setPrototype(square.getId(),square);

        Rectangle rectangle = new Rectangle();
        rectangle.setId("3");
        ShapeCache.setPrototype(rectangle.getId(),rectangle);

        System.out.println("Shape : " + ShapeCache.getShape("1").getType());
        System.out.println("Shape : " + ShapeCache.getShape("2").getType());
        System.out.println("Shape : " + ShapeCache.getShape("3").getType());
    }
}

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。

我想说,这应该是最常见的设计模式了。但是建议尽量少用,使用不当会使系统变得特别复杂

适配器模式的实现一般有两种,继承和组合。推荐使用组合的方式

public class Adaptee {
    public void sampleOperation1(){
        System.out.println("adaptee");
    }
}
public intece Target {
    /**
     * 这是源类Adaptee也有的方法
     */
    void sampleOperation1();
    /**
     * 这是源类Adapteee没有的方法
     */
    void sampleOperation2();
}
public class Adapter implements Target{
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee){
        this.adaptee = adaptee;
    }
    /**
     * 源类Adaptee有方法sampleOperation1
     * 因此适配器类直接委派即可
     */
    public void sampleOperation1(){
        this.adaptee.sampleOperation1();
    }
    /**
     * 源类Adaptee没有方法sampleOperation2
     * 因此由适配器类需要补充此方法
     */
    public void sampleOperation2(){
        //写相关的代码
        System.out.print("adapter");
    }
}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-33899-7.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...