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 HouseBuilder class which shield the House Owner or client from
directly interacting with subsystems such as designers, government bodies, material supplier etc.
HouseBuilder and subsystem classes are shown below,
/**
* House builder makes the task easier for owner by interacting
* with the subsystems. House owner doesn't required to know
* anything about sub systems.
*
* The three core subsystems the facade is
* going to interact with is listing below,
*/ import com.bethecoder.tutorials.dp.facade.core.HouseMaterialSupplier; import com.bethecoder.tutorials.dp.facade.core.Workers;
public HouseBuilder() {
civilEngineer = new CivilEngineer();
workers = new Workers();
houseMaterialSupplier = new HouseMaterialSupplier();
waterSupplier = new WaterSupplier();
currentSupplier = new CurrentSupplier();
interiorDesigner = new InteriorDesigner();
}
HouseBuilder facade = new HouseBuilder();
facade.buildNewHouse();
}
}
It gives the following output,
House plan completed by Civil Engineer
Civil Engineer explained plan to Workers
Workers started working DAILY from 8AM to 6PM
Payed Rs.500000 and received house contruction material
Got new water connection
Got power supply
House interior design completed
House contruction completed