外观设计模式

x33g5p2x  于2022-03-07 转载在 其他  
字(1.1k)|赞(0)|评价(0)|浏览(261)

1.概念

为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用

2.简单实例代码

1.接口A及其实现类

package com.yl.demo.design;

public interface InterfaceA {
    public void test();
}
package com.yl.demo.design;

public class InterfaceAImpl implements InterfaceA {
    @Override
    public void test() {
        System.out.println("InterfaceA...");
    }
}

2.接口B及其实现类

package com.yl.demo.design;

public interface InterfaceB {
    public void test();
}
package com.yl.demo.design;

public class InterfaceBImpl implements InterfaceB{
    @Override
    public void test() {
        System.out.println("InterfaceB...");
    }
}

3.接口C及其实现类

package com.yl.demo.design;

public interface InterfaceC {
    public void test();
}
package com.yl.demo.design;

public class InterfaceCImpl implements InterfaceC {
    @Override
    public void test() {
        System.out.println("InterfaceC...");
    }
}

4.Facade类,客户端只需要调用Facade的方法就行,无需关心内部的实现细节

package com.yl.demo.design;

public class Facade {
    public void test() {
        InterfaceA interfaceA = new InterfaceAImpl();
        interfaceA.test();
        InterfaceB interfaceB = new InterfaceBImpl();
        interfaceB.test();
        InterfaceC interfaceC = new InterfaceCImpl();
        interfaceC.test();
    }
}

5.测试

package com.yl.demo.design;

public class test {
    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.test();
    }
}

相关文章