美文网首页
外观模式

外观模式

作者: 金煜博 | 来源:发表于2021-05-07 20:10 被阅读0次

什么是外观模式

将复杂的业务流程抽取到不同的对象中进行实现,再定义统一的入口对象进行调用

示例场景

用户购买商品后,系统添加购买商品日志 添加订单信息 发送购买商品信息。

示例图

图片.png

示例代码

1.创建日志类

public class LogService {
    public  void addLog(){
        System.out.println("开始记录购买商品日志");
    }
}

2.创建订单类

public class OrderService {
    public void addOrder(){
        System.out.println("添加购买商品订单");
    }
}

3.创建消息类

public class NewsService {
    public void sendNews(){
        System.out.println("发送购买商品信息");
    }
}

4.外观包装类

public class Appearance {
    public void AppearanceEntrance(){
        LogService logService = new LogService();
        OrderService orderService = new OrderService();
        NewsService newsService = new NewsService();
        logService.addLog();
        orderService.addOrder();
        newsService.sendNews();
    }
}

5.启动类

public class Test {
    public static void main(String[] args) {
        Appearance appearance = new Appearance();
        appearance.AppearanceEntrance();
    }
}

6.运行结果


图片.png

相关文章

网友评论

      本文标题:外观模式

      本文链接:https://www.haomeiwen.com/subject/vwekdltx.html