码迷,mamicode.com
首页 > 编程语言 > 详细

java中23种设计模式之6-外观模式(facade pattern)

时间:2015-03-30 20:50:06      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

概念:为子系统中的一组接口提供一个统一接口。Facade模式定义了一个高层接口,这个接口使得这子系统更容易使用。

应用场景:

1)为一个复杂子系统提供一个简单接口。

2)提高子系统的独立性。

3)在层次化结构中,可以使用Facade模式定义系统中每一层的入口。

 

以大型娱乐商场提供的休闲一条龙服务为例,包括购物,餐饮,按摩。其中购物,按摩,餐饮作为子系统的组成,提供统一对外接口PackageService

类图如下:

技术分享

class Shopping
{
String customer=null;
public Shopping(String customer)
{
this.customer=customer;
}

public void doService()
{
System.out.println("Shopping service for "+customer);
}
}

class Meal
{
String customer=null;
public Meal(String customer)
{
this.customer=customer;
}

public void doService()
{
System.out.println("Meal service for "+customer);
}
}

class Massage
{
String customer=null;
public Massage(String customer)
{
this.customer=customer;
}

public void doService()
{
System.out.println("Massage service for "+customer);
}
}
class PackageService
{
String customer=null;
Shopping aShopping=null;
Meal aMeal=null;
Massage aMassage=null;
public PackageService(String customer)
{
this.customer=customer;
aShopping=new Shopping(customer);
aMeal=new Meal(customer);
aMassage=new Massage(customer);
}
public void doService()
{
aShopping.doService();
aMeal.doService();
aMassage.doService();
}
}
public class FacadePatternTest
{
public static void main(String[] args)
{
String customer="wudy";
Shopping aShopping=new Shopping(customer);
aShopping.doService();
Meal aMeal=new Meal(customer);
aMeal.doService();
Massage aMassage=new Massage(customer);
aMassage.doService();
//----------------------
System.out.println("------now is use facade pattern-------");
PackageService aPackageService=new PackageService(customer);
aPackageService.doService();
}
}

////////////////////////////////////////

输出结果:

Shopping service for wudy
Meal service for wudy
Massage service for wudy
------now is use facade pattern-------
Shopping service for wudy
Meal service for wudy
Massage service for wudy

java中23种设计模式之6-外观模式(facade pattern)

标签:

原文地址:http://www.cnblogs.com/wudymand/p/4378992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!