设计模式之外观模式

外观模式

本质: 封装交互,简化调用

何时使用外观模式
设计模式之外观模式_第1张图片

相关模式

设计模式之外观模式_第2张图片

外观模式经典代码:

package com.tao.design.facade;

/**
 * @projectName: DesignMode
 * @package: com.tao.design.facade
 * @className: ServiceA
 * @author: TT_Hugo
 * @description: TODO
 * @date: 2022/12/26 22:05
 * @version: 1.0
 */
public interface ServiceA {
    void methodA();
}

package com.tao.design.facade;

/**
 * @projectName: DesignMode
 * @package: com.tao.design.facade
 * @className: ServiceAImpl
 * @author: TT_Hugo
 * @description: TODO
 * @date: 2022/12/26 22:06
 * @version: 1.0
 */
public class ServiceAImpl implements ServiceA{
    @Override
    public void methodA() {
        System.out.println("这是服务A");
    }
}

package com.tao.design.facade;

/**
 * @projectName: DesignMode
 * @package: com.tao.design.facade
 * @className: ServiceB
 * @author: TT_Hugo
 * @description: TODO
 * @date: 2022/12/26 22:05
 * @version: 1.0
 */
public interface ServiceB {
    void methodB();
}

package com.tao.design.facade;

/**
 * @projectName: DesignMode
 * @package: com.tao.design.facade
 * @className: ServiceBImpl
 * @author: TT_Hugo
 * @description: TODO
 * @date: 2022/12/26 22:06
 * @version: 1.0
 */
public class ServiceBImpl implements ServiceB{
    @Override
    public void methodB() {
        System.out.println("这是服务B");
    }
}

package com.tao.design.facade;

/**
 * @projectName: DesignMode
 * @package: com.tao.design.facade
 * @className: ServiceC
 * @author: TT_Hugo
 * @description: TODO
 * @date: 2022/12/26 22:05
 * @version: 1.0
 */
public interface ServiceC {
    void methodC();
}

package com.tao.design.facade;

/**
 * @projectName: DesignMode
 * @package: com.tao.design.facade
 * @className: ServiceCImpl
 * @author: TT_Hugo
 * @description: TODO
 * @date: 2022/12/26 22:06
 * @version: 1.0
 */
public class ServiceCImpl implements ServiceC{
    @Override
    public void methodC() {
        System.out.println("这是服务C");
    }
}

门面类:将交互封装起来,简化使用者的调用

package com.tao.design.facade;

/**
 * @projectName: DesignMode
 * @package: com.tao.design.facade
 * @className: Facade
 * @author: TT_Hugo
 * @description: TODO
 * @date: 2022/12/26 22:08
 * @version: 1.0
 */
public class Facade {
    ServiceA sa;
    ServiceB sb;
    ServiceC sc;

    public Facade() {
        sa = new ServiceAImpl();
        sb = new ServiceBImpl();
        sc = new ServiceCImpl();
    }

    public void methodA() {
        sa.methodA();
        sb.methodB();
    }

    public void methodB() {
        sb.methodB();
        sc.methodC();
    }

    public void methodC() {
        sc.methodC();
        sa.methodA();
    }
}

测试类

package com.tao.design.facade;

/**
 * @Author Mi_Tao
 * @Date 2023/7/23
 * @Description
 * @Version 1.0
 **/
public class Client {
    public static void main(String[] args) {
        new Facade().methodA();
    }
}

设计模式之外观模式_第3张图片

你可能感兴趣的:(设计模式,设计模式,外观模式,java)