要求一个子系统的外部与其内部的通信必须通过一个统一的对象进行。
门面模式提供一个高层次接口,使得子系统更加易于使用。
package Standard;
public class ClassA {
public void doSomethingA() {
//业务逻辑
}
}
package Standard;
public class ClassB {
public void doSomethingB() {
//业务逻辑
}
}
package Standard;
public class ClassC {
public void doSomethingC() {
//业务逻辑
}
}
package Standard;
public class Facade {
//被委派对象
private ClassA a = new ClassA();
private ClassB b = new ClassB();
private ClassC c = new ClassC();
//提供给外部访问的方法
public void methodA() {
this.a.doSomethingA();
}
public void methodB() {
this.b.doSomethingB();
}
public void methodC() {
this.c.doSomethingC();
}
}
package Office;
public interface ILetterProcess {
//书写内容
public void writeContext(String context);
//填写信封,内容为地址之类
public void fillEnvelope(String address);
//将信件装进信封
public void letterInotoEnvelope();
//发送信件
public void sendLetter();
}
package Office;
public class LetterProcessImpl implements ILetterProcess {
@Override
public void writeContext(String context) {
System.out.println("填写信件内容" + context);
}
@Override
public void fillEnvelope(String address) {
System.out.println("填写投递地址" + address);
}
@Override
public void letterInotoEnvelope() {
System.out.println("把信件放到信封...");
}
@Override
public void sendLetter() {
System.out.println("发送信件...");
}
}
package Office;
public class ModernPostOffice {
private ILetterProcess letterProcess = new LetterProcessImpl();
public void sendLetter(String context, String address) {
//写信
letterProcess.writeContext(context);
//写地址
letterProcess.fillEnvelope(address);
//装信件
letterProcess.letterInotoEnvelope();
//发送
letterProcess.sendLetter();
}
}
package Office;
public class Client {
public static void main(String[] args) {
ModernPostOffice postOffice = new ModernPostOffice();
String address = "顺城路";
String context = "你好,这里是snowalker";
postOffice.sendLetter(context, address);
}
}
填写信件内容你好,这里是snowalker
填写投递地址顺城路
把信件放到信封...
发送信件...