Facade Design Pattern in java
The Facade Design Pattern is a structural design
pattern that provides a simplified interface to a complex subsystem. It
acts as a wrapper that hides the complexity of one or more subsystems
and provides a single, unified interface for the client to interact with.
Facade Pattern means having one simple class
(like a helper or agent) that talks to many hard classes for you. It hides
the hard stuff and gives you one easy way to get things done.
Key Concepts of Facade Pattern
Aspect |
Description |
Purpose |
To simplify access to a complex system or set of
subsystems. |
Category |
Structural design pattern. |
Problem it Solves |
When a system is complex with many interdependent classes,
the facade pattern provides a higher-level interface that makes the system
easier to use. |
Real-World Analogy |
Think of a universal TV remote – it simplifies
control over multiple devices like TV, DVD player, and sound system. |
Components of the Facade Pattern
- Facade:
The simplified interface class that clients use.
- Subsystem
Classes: The internal complex classes that perform actual work. These
are hidden from the client.
- Client:
The code that interacts with the facade rather than the subsystems
directly.
UML Diagram
When to Use
- You
have a complex system with many interdependent components.
- You
want to provide a unified interface to several interfaces in a subsystem.
- You
want to layer your system (e.g., UI <-> Facade <-> Business
Logic <-> Database).
- You
want to reduce dependencies between client code and the system internals.
When Not to Use
- If the
subsystem is already simple.
- If the
client needs fine-grained control over every part of the subsystem.
Advantages
- Simplifies
usage for clients.
- Reduces
coupling between client and subsystem.
- Improves
readability and maintainability.
- Provides
a point for adding additional functionalities (e.g., logging, caching).
Example :
Use Case: Book a Trip
Subsystems:
- FlightBookingService
- HotelBookingService
·
CarRentalService
Facade:
- TravelFacade
Client:
- Main
class
Client → TravelFacade → FlightBookingService
HotelBookingService
CarRentalService
Subsystems:
package facadedesignpattern;
public class FlightBookingService {
public
void bookFlight(String from,String to) {
System.out.println("Flight
booked from " + from + " to " + to);
}
}
package facadedesignpattern;
public class HotelBookingService {
public
void hotelBook(String location)
{
System.out.println("Hotel
Booked In"+location);
}
}
package facadedesignpattern;
public class CarRentalService {
public void rentCar(String location) {
System.out.println("Car
rented at " + location);
}
}
TravelFacde
package facadedesignpattern;
public class TravelFacde {
private
FlightBookingService flightBookingService;
private
HotelBookingService hotelBookingService;
private
CarRentalService carRentalService;
public
TravelFacde() {
flightBookingService=new
FlightBookingService();
hotelBookingService=new
HotelBookingService();
carRentalService=new
CarRentalService();
}
public
void bookTrip(String from,String to)
{
System.out.println("=Booking
Travel Package=");
flightBookingService.bookFlight(from,
to);
hotelBookingService.hotelBook(to);
carRentalService.rentCar(to);
System.out.println("=Travel
Package Booked Successfully=");
}
}
Client
package facadedesignpattern;
public class Main {
public
static void main(String[] args) {
TravelFacde
tf = new TravelFacde();
tf.bookTrip("Bengaluru",
"Patna");
tf.bookTrip("India", "Lonndon");
}
}
Common Use Cases of Facade Pattern in Java
1. Java Standard Library
- Java
IO API
The java.io package is quite complex, with many classes like InputStream, OutputStream, Reader, Writer, and their subclasses.
Classes like BufferedReader or PrintWriter act as facades providing easier ways to read/write data without dealing directly with low-level streams. - JDBC
(Java Database Connectivity)
The DriverManager class acts as a facade for managing database drivers and connections, hiding complex details of driver management from clients.
2. Java EE / Jakarta EE Frameworks
- EJB
(Enterprise Java Beans)
Facade pattern is used to expose simpler business interfaces while hiding complex business logic and persistence operations behind the scenes. - JPA
(Java Persistence API)
The EntityManager provides a facade interface for complex operations related to database transactions, queries, and entity management.
3. Spring Framework
- Spring’s
JdbcTemplate
It acts as a facade over the core JDBC API, simplifying database access by handling connection management, exception handling, and statement creation. - RestTemplate
/ WebClient
These classes are facades simplifying HTTP client operations, hiding the complexity of REST calls.
4. Logging Frameworks
- SLF4J
(Simple Logging Facade for Java)
SLF4J is literally a logging facade that provides a common API for various logging frameworks (Log4j, java.util.logging, Logback). This hides the implementation details and allows you to switch logging frameworks without changing client code.
5. Complex Subsystem Integration
- When
integrating multiple third-party libraries or microservices, you often
create a facade layer to provide a unified interface to the rest of
your application.
🧩 Summary Table
Use Case |
Example |
Facade Role |
Java IO |
BufferedReader |
Simplifies stream reading |
JDBC |
DriverManager |
Manages DB connections |
Java EE |
EntityManager |
Simplifies persistence operations |
Spring Framework |
JdbcTemplate |
Simplifies JDBC operations |
Logging |
SLF4J |
Unified logging API over multiple logging libs |
System Integration |
Custom facade classes |
Unify multiple APIs or services |
No comments:
Post a Comment