外观模式:也叫门面模式,隐藏系统的复杂性,向客户端提供一个客户端可以访问系统的接口
添加一个接口用这一个接口可以隐藏实际系统的复杂性。
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();
}
}