zoukankan      html  css  js  c++  java
  • 设计模式【6】------>外观模式

    一、什么是外观模式

    • 外观模式:也叫门面模式,隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。
    • 它向现有的系统添加一个接口,用这一个接口来隐藏实际的系统的复杂性。
    • 使用外观模式,他外部看起来就是一个接口,其实他的内部有很多复杂的接口已经被实现

    二、外观模式结构

    • 外观(Facade)角色:为多个子系统对外提供一个共同的接口。
    • 子系统(Sub System)角色:实现系统的部分功能,客户可以通过外观角色访问它。
    • 客户(Client)角色:通过一个外观角色访问各个子系统的功能。

    结构图

    三、外观模式例子

    • 用户注册完之后,需要调用阿里短信接口、邮件接口、微信推送接口。

    1、创建阿里短信接口

    public interface AliSmsService {
        void sendSms();
    }
    public class AliSmsServiceImpl implements AliSmsService {
        @Override
        public void sendSms() {
            System.out.println("发送阿里短信消息...");
        }
    }

    2、创建邮件接口

    public interface EmailSmsService {
        void sendSms();
    }
    public class EmailSmsServiceImpl implements EmailSmsService {
        @Override
        public void sendSms() {
            System.out.println("发送邮件消息...");
        }
    }

    3、创建微信推送接口

    public interface WeixinSmsService {
        void sendSms();
    }
    public class WeixinSmsServiceImpl implements WeixinSmsService {
        @Override
        public void sendSms() {
            System.out.println("发送微信推送消息...");
        }
    }

    4、创建门面

    /**
     * 门面
     */
    public class Computer {
    
        private AliSmsService aliSmsService;
        private EmailSmsService emailSmsService;
        private WeixinSmsService weixinSmsService;
    
        public Computer(){
            aliSmsService=new AliSmsServiceImpl();
            emailSmsService = new EmailSmsServiceImpl();
            weixinSmsService = new WeixinSmsServiceImpl();
        }
    
        //只需调用它
        public void sendMsg(){
            aliSmsService.sendSms();
            emailSmsService.sendSms();
            weixinSmsService.sendSms();
        }
    }

    5、客户端测试

    /**
     * 客户端测试
     */
    public class Client {
        public static void main(String[] args){
            //普通模式需要这样
            AliSmsService aliSmsService = new AliSmsServiceImpl();
            EmailSmsService emailSmsService = new EmailSmsServiceImpl();
            WeixinSmsService weixinSmsService = new WeixinSmsServiceImpl();
            aliSmsService.sendSms();
            emailSmsService.sendSms();
            weixinSmsService.sendSms();
    
            System.out.println("------------------");
            
            //利用外观模式简化方法
            new Computer().sendMsg();
        }
    }

    打印结果

     四、外观模式优缺点

      外观(Facade)模式是“迪米特法则”的典型应用

    优点:

    1. 降低了子系统与客户端之间的耦合度,使得子系统的变化不会影响调用它的客户类。
    2. 对客户屏蔽了子系统组件,减少了客户处理的对象数目,并使得子系统使用起来更加容易。
    3. 降低了大型软件系统中的编译依赖性,简化了系统在不同平台之间的移植过程,因为编译一个子系统不会影响其他的子系统,也不会影响外观对象。

    缺点:

    1. 不能很好地限制客户使用子系统类,很容易带来未知风险。
    2. 增加新的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”。
    作者:donleo123
    本文如对您有帮助,还请多推荐下此文,如有错误欢迎指正,相互学习,共同进步。
  • 相关阅读:
    css+ul+li方式 横向再纵向排列
    b表中有的塞给a表
    .net remoting的两种实现方式 cow
    Prism之Module cow
    2012项目总结 cow
    WCF学习笔记 cow
    也谈委托,事件和回调 cow
    理清apply(),call()的区别和关系 cow
    CLR via C#学习之线程栈,托管堆,值类型和引用类型 cow
    细说系列笔记 cow
  • 原文地址:https://www.cnblogs.com/donleo123/p/14564126.html
Copyright © 2011-2022 走看看