门面模式

随着系统的不断改进和开发,它们会变得越来越复杂,系统会生成大量的类,这使得程序流程更难被理解。门面模式可以为这些类提供一个简化的接口,从而简化访问这些类的复杂性,有时这种简化可能降低访问这些底层类的灵活性,但除了要求特别苛刻的客户端外,它通常都可以提供所需的全部功能。

package com.fcy.model;
//准备三个接口
interface Payment{
	public String pay();
}
interface Cook{
	public String cook(String food);
}
interface Waiter {
	public void serve(String food);
}
class PaymentImpl implements Payment{
	public String pay(){
		String food="快餐";
		System.out.println("你已经向收银员支付了费用,您购买的食物是:"+food);
		return food;
	}
}
//分别实现这三个接口
class CookImpl implements Cook{
	public String cook(String food){
		System.out.println("厨师正在烹调:"+food);
		return food;
	}
}
class WaiterImpl implements Waiter{
	public void serve(String food){
		System.out.println("服务员已经将"+food+"端过来了,请慢用...");
	}
}
class Facade{						//门面类,封装三个对象
	Payment pay;
	Cook cook;
	Waiter waiter;
	public Facade(){
		this.pay=new PaymentImpl();
		this.cook=new CookImpl();
		this.waiter=new WaiterImpl();
	}
	public void serveFood(){
		String food=pay.pay();
		food=cook.cook(food);
		waiter.serve(food);
	}
}
class Customer{						//顾客吃东西
	public void haveDinner(){
		Facade f=new Facade();
		f.serveFood();				//只需要调用一个Facade对象
	}
}

public class FacadeTest {
	public static void main(String[] args) {
		new Customer().haveDinner();
	}
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值