Facade
The objective of Facade pattern is to make a complex system simple. It is achieved by providing a unified or general interface, which is a higher layer to these subsystems.Facade pattern decouples subsystems, reduce its dependency, and improve portability,makes an entry point to your subsystems and hides clients from subsystem components and their implementation.
JDBC design is a good example of Façade pattern. A database design is complicated. JDBC is used to connect the database and manipulate data without exposing details to the clients.
A client is exposed to certain set of methods like just getting a single instance of database connection or closing it when not required.
Another possibility is designing security of a system with Façade pattern. Clients' authorization to access information may be classified. General users may be allowed to access general information, special guests may be allowed to access more information,administrators and executives may be allowed to access the most important information. These subsystems may be generalized by oneinterface. The identified users may be directed to the related
subsystems.
One example use of the facade pattern is for integrating a web site with a business application. The existing software may include large amounts of business logic that must be accessed in a particular manner. The web site may require only limited access to this business logic. For example, the web site may need to show whether an item for sale has reached a limited level of stock. The IsLowStock method of the facade class could return a Boolean value to indicate this. Behind the scenes, this method could be hiding the complexities of processing the current physical stock, incoming stock, allocated items and the low stock level for each item.
The following shows the basic code of the facade design pattern implemented using C#. For simplicity, all of the classes involved are defined in a single code listing. In reality, these would probably be in different assemblies. Each class includes a sample method that is called from within the single facade method, with the results being passed to other methods from the subsystem.
public class Facade
{
public void PerformAction()
{
Class1A c1a = new Class1A();
Class1B c1b = new Class1B();
Class2A c2a = new Class2A();
Class2B c2b = new Class2B();
int result1a = c1a.Method1A();
int result1b = c1b.Method1B(result1a);
int result2a = c2a.Method2A(result1a);
c2b.Method2B(result1b, result2a);
}
}
public class Class1A
{
public int Method1A()
{
// Sample code
}
}
public class Class1B
{
public int Method1B(int param)
{
// Sample code
}
}
public class Class2A
{
public int Method2A(int param)
{
// Sample code
}
}
public class Class2B
{
public void Method2B(int param1, int param2)
{
// Sample code
}}
JDBC design is a good example of Façade pattern. A database design is complicated. JDBC is used to connect the database and manipulate data without exposing details to the clients.
A client is exposed to certain set of methods like just getting a single instance of database connection or closing it when not required.
Another possibility is designing security of a system with Façade pattern. Clients' authorization to access information may be classified. General users may be allowed to access general information, special guests may be allowed to access more information,administrators and executives may be allowed to access the most important information. These subsystems may be generalized by oneinterface. The identified users may be directed to the related
subsystems.
One example use of the facade pattern is for integrating a web site with a business application. The existing software may include large amounts of business logic that must be accessed in a particular manner. The web site may require only limited access to this business logic. For example, the web site may need to show whether an item for sale has reached a limited level of stock. The IsLowStock method of the facade class could return a Boolean value to indicate this. Behind the scenes, this method could be hiding the complexities of processing the current physical stock, incoming stock, allocated items and the low stock level for each item.
The following shows the basic code of the facade design pattern implemented using C#. For simplicity, all of the classes involved are defined in a single code listing. In reality, these would probably be in different assemblies. Each class includes a sample method that is called from within the single facade method, with the results being passed to other methods from the subsystem.
public class Facade
{
public void PerformAction()
{
Class1A c1a = new Class1A();
Class1B c1b = new Class1B();
Class2A c2a = new Class2A();
Class2B c2b = new Class2B();
int result1a = c1a.Method1A();
int result1b = c1b.Method1B(result1a);
int result2a = c2a.Method2A(result1a);
c2b.Method2B(result1b, result2a);
}
}
public class Class1A
{
public int Method1A()
{
// Sample code
}
}
public class Class1B
{
public int Method1B(int param)
{
// Sample code
}
}
public class Class2A
{
public int Method2A(int param)
{
// Sample code
}
}
public class Class2B
{
public void Method2B(int param1, int param2)
{
// Sample code
}}