設計模式(10)外觀模式
阿新 • • 發佈:2018-10-26
是你 代碼 write reason sse foo code 部分 args
模式介紹
外觀模式的思想是隱藏系統的復雜性。
示例
假設餐館的廚房分為三部分,分別放置冷熱食物和飲料的區域。但是你作為顧客的話,並不關心這些。然而服務員知道這些。
顧客
/// <summary> /// Patron of the restaurant (duh!) /// </summary> class Patron { private string _name; public Patron(string name) { this._name = name; } public string Name { get { return _name; } } }
聲明廚房區域的接口、訂單類
/// <summary> /// All items sold in the restaurant must inherit from this. /// </summary> class FoodItem { public int DishID; } /// <summary> /// Each section of the kitchen must implement this interface. /// </summary> interface KitchenSection { FoodItem PrepDish(int DishID); } /// <summary> /// Orders placed by Patrons. /// </summary> class Order { public FoodItem Appetizer { get; set; } public FoodItem Entree { get; set; } public FoodItem Drink { get; set; } }
廚房的三個區域
/// <summary> /// A division of the kitchen. /// </summary> class ColdPrep : KitchenSection { public FoodItem PrepDish(int dishID) { //Go prep the cold item return new FoodItem() { DishID = dishID }; } } /// <summary> /// A division of the kitchen. /// </summary> class HotPrep : KitchenSection { public FoodItem PrepDish(int dishID) { //Go prep the hot entree return new FoodItem() { DishID = dishID }; } } /// <summary> /// A division of the kitchen. /// </summary> class Bar : KitchenSection { public FoodItem PrepDish(int dishID) { //Go mix the drink return new FoodItem() { DishID = dishID }; } }
服務員
/// <summary>
/// The actual "Facade" class, which hides the complexity of the KitchenSection classes.
/// After all, there‘s no reason a patron should order each part of their meal individually.
/// </summary>
class Server
{
private ColdPrep _coldPrep = new ColdPrep();
private Bar _bar = new Bar();
private HotPrep _hotPrep = new HotPrep();
public Order PlaceOrder(Patron patron, int coldAppID, int hotEntreeID, int drinkID)
{
Console.WriteLine("{0} places order for cold app #" + coldAppID.ToString()
+ ", hot entree #" + hotEntreeID.ToString()
+ ", and drink #" + drinkID.ToString() + ".");
Order order = new Order();
order.Appetizer = _coldPrep.PrepDish(coldAppID);
order.Entree = _hotPrep.PrepDish(hotEntreeID);
order.Drink = _bar.PrepDish(drinkID);
return order;
}
}
客戶端調用
static void Main(string[] args)
{
Server server = new Server();
Console.WriteLine("Hello! I‘ll be your server today. What is your name?");
var name = Console.ReadLine();
Patron patron = new Patron(name);
Console.WriteLine("Hello " + patron.Name + ". What appetizer would you like? (1-15):");
var appID = int.Parse(Console.ReadLine());
Console.WriteLine("That‘s a good one. What entree would you like? (1-20):");
var entreeID = int.Parse(Console.ReadLine());
Console.WriteLine("A great choice! Finally, what drink would you like? (1-60):");
var drinkID = int.Parse(Console.ReadLine());
Console.WriteLine("I‘ll get that order in right away.");
server.PlaceOrder(patron, appID, entreeID, drinkID); //Here‘s what the Facade simplifies
Console.ReadKey();
}
可以看出,顧客之和服務員進行了交互。
總結
外觀模式是復雜系統上的一層簡單覆蓋,使用較為普遍。
源代碼
https://github.com/exceptionnotfound/DesignPatterns/tree/master/Facade
原文
https://www.exceptionnotfound.net/the-daily-design-pattern-facade/
設計模式(10)外觀模式