Facade Pattern
The Facade Pattern hides the complexity of a system and provides an interface through which the client can access the system. This type of design pattern belongs to the structural patterns; it adds an interface to an existing system to hide its complexity.
This pattern involves a single class that provides simplified methods for client requests and delegated calls to the methods of existing system classes.
Introduction
Intent
Provide a consistent high-level interface for a complex subsystem. In this way, client code can interact with the subsystem through this simplified interface without needing to understand the complexity inside the subsystem.
Main problem solved
- Reduce the coupling between the client and a complex subsystem.
- Simplify the client's operations on a complex system and hide internal implementation details.
Use Cases
- When the client does not need to understand the complex logic and component interactions inside the system.
- When a clear entry point needs to be defined for the whole system.
Implementation
- Create the Facade class: define a class (the Facade) that acts as an intermediary between the client and the subsystem.
- Encapsulate subsystem operations: the Facade class encapsulates complex subsystem operations into simple methods.
Key code
- Facade class: provides a high-level interface that simplifies the interaction between the client and the subsystem.
- Subsystem classes: implements the specific business logic and is called by the Facade class.
Application Example
- Hospital reception: the reception staff at a hospital simplify the complex processes of registration, outpatient service, price calculation, and medicine collection.
- Java three-tier architecture: through the Facade Pattern, access to the presentation layer, business logic layer, and data access layer can be simplified.
Advantages
- Reduce dependencies: the dependency between the client and the subsystem is reduced.
- Improved flexibility: internal changes in the subsystem do not affect the client.
- Enhanced security: the internal implementation of the subsystem is hidden, exposing only the necessary operations.
Disadvantages
- Violates the Open-Closed Principle: changes to the subsystem may require corresponding changes to the Facade class.
Usage recommendation
- Use the Facade Pattern when you need to simplify access to a complex system.
- Ensure that the methods provided by the Facade class are simple enough for the client to use.
Notes
- The Facade Pattern is suitable for hierarchical structures and can provide a clear entry point for each layer.
- Avoid overusing the Facade Pattern so that too many details are hidden, making maintenance difficult.
structures
The Facade Pattern involves the following core roles:
-
Facade:
- Provides a simplified interface that encapsulates the complexity of the system. Clients of the Facade Pattern interact with the Facade object without dealing directly with the individual components of the system.
-
Subsystem:
- Composed of multiple interrelated classes, responsible for the specific functions of the system. The Facade object fulfills the client's requests by calling these subsystems.
-
Client (Client):
- Use the Facade object to interact with the system without needing to know the specific internal implementation.
Create a class that implements
We will create a Shape interface as the bridge implementation and concrete classes that implement the Shape entity classes of the interface. The next step is to define a Facade class ShapeMaker。
ShapeMaker class that uses the entity classes to represent the user's calls to these classes.FacadePatternDemo Class uses ShapeMaker class to display the results.
Step 1
Create an interface.
Shape.java
public
interface
Shape
{
void
draw
(
)
;
}
Step 2
Create concrete classes that implement the interface.
Rectangle.java
public
class
Rectangle
implements
Shape
{
@
Override
public
void
draw
(
)
{
System
.
out
.
println
(
"
Rectangle::draw()
"
)
;
}
}
Square.java
public
class
Square
implements
Shape
{
@
Override
public
void
draw
(
)
{
System
.
out
.
println
(
"
Square::draw()
"
)
;
}
}
Circle.java
public
class
Circle
implements
Shape
{
@
Override
public
void
draw
(
)
{
System
.
out
.
println
(
"
Circle::draw()
"
)
;
}
}
Step 3
Create a Facade class.
ShapeMaker.java
public
class
ShapeMaker
{
private
Shape
circle
;
private
Shape
rectangle
;
private
Shape
square
;
public
ShapeMaker
(
)
{
circle
=
new
Circle
(
)
;
rectangle
=
new
Rectangle
(
)
;
square
=
new
Square
(
)
;
}
public
void
drawCircle
(
)
{
circle
.
draw
(
)
;
}
public
void
drawRectangle
(
)
{
rectangle
.
draw
(
)
;
}
public
void
drawSquare
(
)
{
square
.
draw
(
)
;
}
}
Step 4
Use the Facade class to draw various types of shapes.
FacadePatternDemo.java
public
class
FacadePatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
ShapeMaker
shapeMaker
=
new
ShapeMaker
(
)
;
shapeMaker
.
drawCircle
(
)
;
shapeMaker
.
drawRectangle
(
)
;
shapeMaker
.
drawSquare
(
)
;
}
}
Step 5
Run the program, output result:
Circle::draw()
Rectangle::draw()
Square::draw()