码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式(10)外观模式

时间:2018-10-26 15:06:34      阅读:175      评论:0      收藏:0      [点我收藏+]

标签: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/

设计模式(10)外观模式

标签:string   for   you   var   ret   aced   oid   声明   att   

原文地址:https://www.cnblogs.com/talentzemin/p/9856248.html

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