IntroductionDefinitionProvide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. Note: 1. Wrap a complicated subsystem with a simpler interface. 2. The biggest assumption in Façade pattern is that objects are often Singletons because only one Facade object is required. 3. Facade defines a new interface 4. Both, Adapter and Facade are both wrappers. But they are different kinds of wrappers. The intent of Facade is to produce a simpler interface but new, and the intent of Adapter is to design to an existing interface may be one or more. Facade characteristically wraps multiple objects and Adapter wraps a single object. 5. Abstract Factory can be used as an option to Facade to hide platform-specific classes. Best examples are
1. Front desk for any company. 2. Customer care Help desk. 3. Amazon.com 1-Click® system DesignSee the basic and Singleton façade examples UML Diagram GOF CodeBasic Example public class SubSystemOne
{public void MethodOne()
{
Console.WriteLine(" Sub System One Method is called");
}
}public class SubSystemTwo
{public void MethodTwo()
{
Console.WriteLine(" Sub System Two Method is called");
}
}public class SubSystemThree
{public void MethodThree()
{
Console.WriteLine(" Sub System Three Method is called");
}
}// "Facade" public class Facade
{
FacadeSample.SubSystemOne _subSystemOne;
FacadeSample.SubSystemTwo _subSystemTwo;
FacadeSample.SubSystemThree _subSystemThree;
Facade()
{
_subSystemOne = new FacadeSample.SubSystemOne();
_subSystemTwo = new FacadeSample.SubSystemTwo();
_subSystemThree = new FacadeSample.SubSystemThree();
}public void MethodA()
{
Console.WriteLine("\n Facade MethodA() is calling ---- ");
_subSystemOne.MethodOne();
_subSystemTwo.MethodTwo();
}public void MethodB()
{
Console.WriteLine("\n Facade MethodB() is calling---- ");
_subSystemTwo.MethodTwo();
_subSystemThree.MethodThree();
}public void MethodC()
{
Console.WriteLine("\n Facade MethodC() is calling---- ");
_subSystemOne.MethodOne();
_subSystemThree.MethodThree();
}
}
Client Side Facade facade = new Facade();
facade.MethodA();
facade.MethodB();In the clasic example aboue, all the sub systems are exposed to the out side world. So we need to restic the access to the sub systems to the out side world. Make all sub system access modifier to internal.
Also, as said, Façade object should be one instance in the application. So just by changing to static class we can achive this but what if two threads are trying to create? Again it will allow to maintain single instance per thread.
So make sure it should be perfect singleton. namespace FacadeSample
{internal class SubSystemOne
{internal void MethodOne()
{
Console.WriteLine(" Sub System One Method is called");
}
}internal class SubSystemTwo
{internal void MethodTwo()
{
Console.WriteLine(" Sub System Two Method is called");
}
}internal class SubSystemThree
{internal void MethodThree()
{
Console.WriteLine(" Sub System Three Method is called");
}
}
}// "Facade" public class Facade
{
FacadeSample.SubSystemOne _subSystemOne;
FacadeSample.SubSystemTwo _subSystemTwo;
FacadeSample.SubSystemThree _subSystemThree;
Facade()
{
_subSystemOne = new FacadeSample.SubSystemOne();
_subSystemTwotwo = new FacadeSample.SubSystemTwo();
_subSystemThree = new FacadeSample.SubSystemThree();
}static Facade()
{
}static readonly Facade facadeInstance = new Facade();public static Facade FacadeInstance
{get { return facadeInstance; }
}public void MethodA()
{
Console.WriteLine("\n Facade MethodA() is calling ---- ");
_subSystemOne.MethodOne();
_subSystemTwo.MethodTwo();
}public void MethodB()
{
Console.WriteLine("\n Facade MethodB() is calling---- ");
_subSystemTwo.MethodTwo();
_subSystemThree.MethodThree();
}public void MethodC()
{
Console.WriteLine("\n Facade MethodC() is calling---- ");
_subSystemOne.MethodOne();
_subSystemThreethree.MethodThree();
} }
Client Side Facade facade = Facade.FacadeInstance;
facade.MethodA();
facade.MethodB();
Hopes help. thank you for reading. |