所谓门面模式就是将子系统中的一系列复杂操作隐藏,将其封装成一个接口,给调用者提供方便的操作。
public class FacadeDemo {
public static void main(String[] args) throws Exception {
//having dinner
Payment payment = new Payment();
Cook cook = new Cook();
Waiter waiter = new Waiter();
String food = payment.payForYouFood();
cook.cookFood(food);
waiter.serviceForYou(food);
//
Facade facade = new Facade();
facade.havingDinner();
}
}
class Payment {
public String payForYouFood() {
String food = "noddle";
System.out.println(" pay for you food");
return food;
}
}
class Cook {
public void cookFood(String food) {
System.out.println(" cook you food:" + food);
}
}
class Waiter {
public void serviceForYou(String food) {
System.out.println("take food:" + food + " to you");
}
}
class Facade {
private Payment payment;
private Cook cook;
private Waiter waiter;
public Facade() {
payment = new Payment();
cook = new Cook();
waiter = new Waiter();
}
public Facade(Payment payment, Cook cook, Waiter waiter) {
this.payment = payment;
this.cook = cook;
this.waiter = waiter;
}
public void havingDinner() {
String food = payment.payForYouFood();
cook.cookFood(food);
waiter.serviceForYou(food);
}
}
网友评论