Design Mode (10) Appearance Mode

Source: Internet
Author: User
Mode Introduction

The idea of appearance mode is to hide the complexity of the system.

Example

It is assumed that the kitchen of a restaurant is divided into three parts: the area where cold and hot food and drinks are placed respectively. But you don't care about it as a customer. However, the waiter knows this.

Customer

/// <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; }    }}

Declare the interface and order class of the kitchen area

/// <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; }}

Three areas of the kitchen

/// <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        };    }}

Attendant

/// <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;    }}

Client call

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();}

We can see that the customer and the waiter interact.

Summary

The appearance mode is a simple coverage of Complex Systems and is widely used.

Source code

Https://github.com/exceptionnotfound/DesignPatterns/tree/master/Facade

Original

Https://www.exceptionnotfound.net/the-daily-design-pattern-facade/

Design Mode (10) Appearance Mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.