Facade Pattern

facade

Don’t shoot the messenger!

This is something that you are doing all the time. We just name it now. The Facade Pattern is a structural programming design pattern. To explain it in a nutshell:

A class contains many references to objects, which are invisible to the client. The client has a reduced command set to keep things simple.

The class receives the simple command and takes care about the real complexity behind the facade, which is presented to the client. Think of a cash dispenser. All you have to know is a few buttons. The legal structure, the buildings, the accounting, the risk etc. … all is invisible to the client, who only cares about getting cash at this very moment.

 

namespace FacadePattern {

  internal class Burger {
    public void Prepare() { }
    public void WarmUp() { }
    public void Wrap() { }
  } // class

  internal class Fries {
    public void Fry() { }
    public void KeepWarm() { }
  } // class

  internal class Drink {
    public enum eDrink { Coke, SevenUp, Orange, Apple, Milk, Tea, Coffee }
    public bool IsSugarFree { set; get; }
    public bool IsHot { set; get; }
    public void Fill(eDrink xDrink) { }
  } // class

  internal class Extras {
    public void AddSalt() { }
    public void AddKetchup() { }
    public void AddMayo() { }
    public void AddNapkin() { }
  } // class

  public class MealFacade {
    private Burger _Burger = new Burger();
    private Fries _Fries = new Fries();
    private Drink _Drink = new Drink();
    private Extras _Extras = new Extras();

    public void MakeClientHappy() {
      _Drink.IsHot = false;
      _Drink.IsSugarFree = true;
      _Drink.Fill(Drink.eDrink.Coke);
      _Fries.Fry();
      _Burger.Prepare();
      _Burger.Wrap();
      _Extras.AddKetchup();
      _Extras.AddSalt();
      _Extras.AddNapkin();
    } //
  } // class

  class Program {
    static void Main(string[] args) {
      MealFacade lBurgerMeal = new MealFacade();
      lBurgerMeal.MakeClientHappy();
    } // main

  } // class
} // namespace
Advertisements

About Bastian M.K. Ohta

Happiness only real when shared.

Posted on July 2, 2014, in Basic, C#, Programming Patterns, Structural Patterns and tagged , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: