设计模式之外观模式

外观模式:也叫门面模式,隐藏系统的复杂性,向客户端提供一个客户端可以访问系统的接口
添加一个接口用这一个接口可以隐藏实际系统的复杂性。

package 外观模式;
//内部实际的业务,举三个例子
public interface AliSmsService {
     void sendSms();
}

package 外观模式;

public class AliSmsServiceImpl implements AliSmsService {
    @Override
    public void sendSms() {
        System.out.println("阿里短信消息");
    }
}
package 外观模式;

public interface EmaiSmsService {
     void sendSms();
}
package 外观模式;

public class EmailSmsServiceImpl implements EmaiSmsService {
    @Override
    public void sendSms() {
        System.out.println("发送邮件消息");
    }
}
package 外观模式;
public interface WeiXinSmsService {
     void  sendSms();
}
package 外观模式;

public class WeiXinSmsServiceImpl implements WeiXinSmsService {
    @Override
    public void sendSms() {
        System.out.println("向微信消息推送");
    }
}
提供一个门面来封装这三个业务
/*
    门面看起来很好用,实际上复杂的东西都被封装好了

 */
public class Computer {
    AliSmsService aliSmsService;
    EmaiSmsService emaiSmsService;
    WeiXinSmsService weiXinSmsService;
    public Computer(){
        aliSmsService=new AliSmsServiceImpl();
        emaiSmsService=new EmailSmsServiceImpl();
        weiXinSmsService=new WeiXinSmsServiceImpl();
    }
    public void send(){
        aliSmsService.sendSms();
        emaiSmsService.sendSms();
        weiXinSmsService.sendSms();
    }
}
用户在调用的时候只需要调用computer的send方法 就可以处理三个业务
package 外观模式;

public class Client {
    public static void main(String[] args) {
        //普通模式需要这样子
      /*  AliSmsService aliSmsService = new AliSmsServiceImpl();
        EmaiSmsService emaiSmsService = new EmailSmsServiceImpl();
        WeiXinSmsService weiXinSmsService = new WeiXinSmsServiceImpl();
        aliSmsService.sendSms();
        emaiSmsService.sendSms();
        weiXinSmsService.sendSms();*/

        //利用外观模式简化方法
        new Computer().send();

    }



}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值