美文网首页
java 门面模式

java 门面模式

作者: torres9gogogo | 来源:发表于2018-08-14 21:06 被阅读6次

所谓门面模式就是将子系统中的一系列复杂操作隐藏,将其封装成一个接口,给调用者提供方便的操作。

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);
    }
}

相关文章

网友评论

      本文标题:java 门面模式

      本文链接:https://www.haomeiwen.com/subject/hddbbftx.html