设计模式(10)外观模式

Posted talentzemin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式(10)外观模式相关的知识,希望对你有一定的参考价值。

模式介绍

外观模式的思想是隐藏系统的复杂性。

示例

假设餐馆的厨房分为三部分,分别放置冷热食物和饮料的区域。但是你作为顾客的话,并不关心这些。然而服务员知道这些。

顾客

/// <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)外观模式的主要内容,如果未能解决你的问题,请参考以下文章

java设计模式 GOF23 10 外观模式

JAVA SCRIPT设计模式--结构型--设计模式之FACADE外观模式(10)

JAVA SCRIPT设计模式--结构型--设计模式之FACADE外观模式(10)

设计模式(10) 外观模式

GOF设计模式(10)外观模式

设计模式(10)外观模式