java中23種設計模式之6-面板模式(facade pattern)

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.