Facade pattern comes under Structural design pattern.
It provides a unified interface to a set of interfaces in a subsystem.
Facade defines a higher-level interface that makes the subsystem easier to use.
Behaviour & Advantages
Simplifies the subsystem usage.
Acts as entry point to the subsystem.
Shields clients from subsystem components.
The interaction and communication among subsystems is hidden from client.
Facade
Provides a simplified API to interact with subsystems.
It knows how to handle a request and manages request processing with subsystems.
Subsystem classes
Complex set of classes designed to perform a specific task.
Client
One who uses Facade to interact with subsystems.
This example shows a JDBC Driver Manager Facade implementation which shields the clients
from dealing with database specific classes. Here Oracle & MySQL Driver related classes
represent two complex Subsystems. As soon as the driver class is loaded it gets
registered with JDBC Driver Manager. Then Client can request the Driver Manager to get connection
for a specific database.
/**
* The client doesn't need to know anything about
* the subsystem implementation except initializing
* driver class for required database.
*
* Client just interacts with framework interface
* rather than database specific classes.
*
* Here DriverManager acts as a facade for the
* underlying subsystem.
*
* We can impose security checks or any other
* access verifications in facade before
* accessing the actual subsystem.
*/
Class.forName("com.bethecoder.tutorials.dp.facade2.oracle.OracleJDBCDriver");
IConnection connection = DriverManager.getConnection("oracle");
IStatement statement = connection.createStatement();
statement.executeQuery("select * from Employee");