举例:去医院买药的时候,要先挂号、挂号、门诊、划价、化验、收费、取药等,这时候就可以有一个接待员的位置与各个部门打交道,病人只要与接待人接触就行了,接待员就是门面模式的体现。
这里再写个写信的例子:写信的时候,要先写信的内容,再写信封,再把信放到信封中,最后投递到信箱中
public interface LetterProcess {
//首先,要写信的内容
public void writeContext(String context);
//其次,写信封
public void fillEnvelope(String address);
//把信放到信封里
public void letterIntoEnvelope();
//然后邮递
public void sendLetter();
}
public class LetterProcessImplimplements LetterProcess {
@Override
public void writeContext(String context) {
System.out.println("填写信的内容..." + context);
}
@Override
public void fillEnvelope(String address) {
System.out.println("填写收信人的地址及姓名..." + address);
}
@Override
public void letterIntoEnvelope() {
System.out.println("把信放到信封中...");
}
@Override
public void sendLetter() {
System.out.println("邮递信中...");
}
}
//门面模式的体现
public class ModenPostOffice {
private LetterProcess letterProcess = new LetterProcessImpl();
public void sendLetter(String context,String address){
letterProcess.writeContext(context);
letterProcess.fillEnvelope(address);
letterProcess.letterIntoEnvelope();
letterProcess.sendLetter();
}
}
public class Client {
publicstatic void main(String[] args){
//现代化的邮局
ModenPostOffice hellRoadPostOffice =new ModenPostOffice();
String address ="happy road";
String context ="hello,it is me.";
hellRoadPostOffice.sendLetter(context, address);
}
}