標籤:
概念:為子系統中的一組介面提供一個統一介面。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)