Facade Pattern
Provide a unified interface to a set of interfaces in a subsystem.Facade defines a higher-level interface that makes the subsystem easier to use.(要求一个子系统的外部与其内部的通信必须通过一个统一的对象进行,门面模式提供一个高层次的接口,使得子系统更易于使用)
为什么这也算一种设计模式?
public interface ILetterProcess {
public void writerContext(String context);
public void fillEnvelope(String address);
public void letterIntoEnvelope();
public void sendLetter();
}
public class LetterProcess implements ILetterProcess {
public void fillEnvelope(String address) {
System.out.println("填写信的地址:"+address);
}
public void letterIntoEnvelope() {
System.out.println("把信放进信封");
}
public void sendLetter() {
System.out.println("邮递信件");
}
public void writerContext(String context) {
System.out.println("写信内容:"+context);
}
}
public class ModenPostOffice {
private ILetterProcess letterProcess=new LetterProcess();
public void sendLetter(String context,String address){
this.letterProcess.writerContext(context);
this.letterProcess.fillEnvelope(address);
this.letterProcess.letterIntoEnvelope();
this.letterProcess.sendLetter();
}
}
public class Client {
public static void main(String[] args) {
ModenPostOffice postOffice=new ModenPostOffice();
postOffice.sendLetter("你被录取了", "XX大学");
}
}
优点
灵活,封装,安全,减少依赖
缺点
不符合开闭原则,没办法扩展
使用场景
为一个复杂子系统或者模块提供一个外界访问的接口
子系统相对独立
预防一个低水平的技术人员参与项目开发,为降低个人代码质量对整体项目的影响风险
我是菜鸟,我在路上。