Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Facade Pattern

Posted in Design Patterns
This post has been read 1603 times

This is the continuing post in a series of post on Design patterns. Presently we are going through the structural patterns. I will be writing about the Façade pattern today. This would be the last structural pattern.

You can read about the other patterns from the following links

  1. Decorator Pattern
  2. Proxy Pattern
  3. Bridge Pattern
  4. Composite Pattern
  5. Flyweight Pattern
  6. Adapter Pattern

Façade pattern deals with encapsulating a subsystem which means providing a different high level view of subsystem whose details are hidden from users.


    1 class FacadePattern

    2 {

    3     public class Person

    4     {

    5         public string Position { get; set; }

    6         public double Salary { get; set; }

    7 

    8         public Person( string position, double salary )

    9         {

   10             Position = position;

   11             Salary = salary;

   12         }

   13     }

   14 

   15 

   16     //Subsystem

   17     internal class Bonus

   18     {

   19         internal bool IsDeveloper( Person person )

   20         {

   21             return ( person.Position.Equals( "Developer" ) );

   22         }

   23 

   24         internal void GetBonus( Person person )

   25         {

   26             if ( IsDeveloper( person ) )

   27             {

   28                 person.Salary += person.Salary * .10;

   29             }

   30             else

   31             {

   32                 person.Salary += person.Salary * .12;

   33             }

   34         }

   35     }

   36 

   37     //Facade

   38     class BonusFacade

   39     {

   40         Bonus bonus = new Bonus();

   41         public void GetBonus( Person p )

   42         {

   43             bonus.GetBonus( p );

   44             Console.WriteLine( p.Salary );

   45         }

   46     }

   47 

   48     static void Main()

   49     {

   50         BonusFacade bonus = new BonusFacade();

   51         Person person1 = new Person( "Developer", 5000 );

   52         bonus.GetBonus( person1 );

   53         //Output : 5500;

   54 

   55         Person person2 = new Person( "Tester", 5000 );

   56         bonus.GetBonus( person2 );

   57         //Output : 5600;

   58 

   59         Console.ReadLine();

   60     }

   61 }

As you can see from the above example, the Bonus class is a subsystem which is encapsulated in the façade. When we call the Bonusfacade’s GetBonus method, it internally creates an object of the subsystem and does the necessary functionality. We have to use the façade pattern when the system might get more complex but the present users shouldn’t be aware of the change or when the abstraction and implementation of a system are tightly coupled.

This is the last of the structural patterns. In my next post I will start with the Creational patterns.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll