外观模式

什么是外观模式

外观模式(Facade Pattern)门面模式,隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。

这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。

 

 

外观模式例子

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

public interface EamilSmsService {
      public void sendSms();    
}
public class EamilSmsServiceImpl implements   EamilSmsService{
    public void sendSms() {
        System.out.println("发送邮件消息");
        
    }
}
//微信消息推送
public interface WeiXinSmsService {
  public void sendSms();    
}
public class EamilSmsServiceImpl implements   EamilSmsService{

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

}
//阿里短信消息
public interface AliSmsService {
    public void sendSms();
}
public class AliSmsServiceImpl implements AliSmsService {

    @Override
    public void sendSms() {
     System.out.println("支付宝发送消息...");
    }

}

门面

 

public class Computer {
    AliSmsService aliSmsService;
    EamilSmsService eamilSmsService;
    WeiXinSmsService weiXinSmsService;

    public Computer() {
        aliSmsService = new AliSmsServiceImpl();
        eamilSmsService = new EamilSmsServiceImpl();
        weiXinSmsService = new WeiXinSmsServiceImpl();
    }

    public void sendMsg() {
        aliSmsService.sendSms();
        eamilSmsService.sendSms();
        weiXinSmsService.sendSms();

    }

}

 

public class Client {

    public static void main(String[] args) {
        // AliSmsService aliSmsService= new AliSmsServiceImpl();
        // EamilSmsService eamilSmsService= new EamilSmsServiceImpl();
        // WeiXinSmsService weiXinSmsService= new WeiXinSmsServiceImpl();
        // aliSmsService.sendSms();
        // eamilSmsService.sendSms();
        // weiXinSmsService.sendSms();
        new Computer().sendMsg();
    }

}
posted @ 2019-12-20 09:44  人心冇变  阅读(158)  评论(0编辑  收藏  举报