Introduction:
Façade is one of the most used design patterns on software industry. Basically it is about providing a simple interface for large body of code, like class library and it will help you on :
- Make large body of code easier to be understand.
- Make client code more readable.
- Reduce dependency of client code on class library, thus you will be able to change the internal implementation of your component without affecting client code.
- Wrap a bad-designed APIs with a single well-designed ASPI
A Façade design pattern is used when you want to implement a new way to use component which is easier and more convenient. While Adapter design pattern is used when an interface must match a specific contract and it should support polymorphic behavior.
Example:
class Program { static void Main(string[] args) { Facade fcadeInstance = new Facade(); Console.WriteLine(“Calling DoSometihng1() Method”); fcadeInstance.DoSometihng1(); Console.WriteLine(“——————-“); Console.WriteLine(“Calling DoSometihng2() Method”); fcadeInstance.DoSometihng2(); Console.ReadKey(); } class Facade { private System1 sys1 = new System1(); private System2 sys2 = new System2(); public void DoSometihng1() { Console.WriteLine(sys1.System1Mehtod1()); Console.WriteLine(sys2.System2Mehtod1()); } public void DoSometihng2() { Console.WriteLine(sys1.System1Mehtod2()); Console.WriteLine(sys2.System2Mehtod2()); } } class System1 { public string System1Mehtod1() { return “System 1 Mehtod 1 is called”; } public string System1Mehtod2() { return “System 1 Mehtod 2 is called”; } } class System2 { public string System2Mehtod1() { return “System 2 Mehtod 1 is called”; } public string System2Mehtod2() { return “System 2 Mehtod 2 is called”; } } |