标签:string for you var ret aced oid 声明 att
外观模式的思想是隐藏系统的复杂性。
假设餐馆的厨房分为三部分,分别放置冷热食物和饮料的区域。但是你作为顾客的话,并不关心这些。然而服务员知道这些。
顾客
/// <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/
标签:string for you var ret aced oid 声明 att
原文地址:https://www.cnblogs.com/talentzemin/p/9856248.html