Facade Pattern
# Facade Pattern
The Facade Pattern hides the complexity of a system and provides the client with an interface that the client can use to access the system. This type of design pattern is a structural pattern. It adds an interface to an existing system to hide the complexity of the system.
This pattern involves a single class that provides simplified methods required by the client and delegates calls to methods of existing system classes.
## Introduction
### Intent
Provide a unified high-level interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
### Problem It Solves
* Reduces coupling between the client and the complex subsystem.
* Simplifies the client's operation of the complex system, hiding internal implementation details.
### Use Cases
* When the client does not need to understand the complex logic and component interactions within the system.
* When a clear entry point for the entire system needs to be defined.
### Implementation Approach
* **Create a Facade Class**: Define a class (the facade) that acts as a mediator 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, simplifying the interaction between the client and the subsystem.
* **Subsystem Classes**: Implement the specific business logic, called by the Facade class.
### Application Examples
1. **Hospital Reception**: The hospital receptionist simplifies complex processes like registration, consultation, pricing, and medication collection.
2. **Java Three-Tier Architecture**: Using the facade pattern can simplify access to the presentation layer, business logic layer, and data access layer.
### Advantages
1. **Reduces Dependencies**: Reduces dependencies between the client and the subsystem.
2. **Improves Flexibility**: Internal changes to the subsystem do not affect the client.
3. **Enhances Security**: Hides the internal implementation of the subsystem, exposing only necessary operations.
### Disadvantages
* **Violates the Open/Closed Principle**: Modifications to the subsystem may require corresponding modifications to the facade class.
### Usage Recommendations
* Use the facade pattern when you need to simplify access to a complex system.
* Ensure the methods provided by the facade class are simple enough for the client to use.
### Considerations
* The facade pattern is suitable for hierarchical structures and can provide a clear entry point for each layer.
* Avoid overusing the facade pattern to prevent hiding too many details, which can lead to maintenance difficulties.
### Structure
**The Facade Pattern involves the following core roles:**
* **Facade:**
* Provides a simplified interface, encapsulating the complexity of the system. The client of the facade pattern interacts with the facade object without having to deal directly with the various components of the system.
* **Subsystem:**
* Consists of multiple interconnected classes responsible for the system's specific functionality. The facade object fulfills client requests by calling these subsystems.
* **Client:**
* Uses the facade object to interact with the system without needing to understand the specific implementation details within the system.
## Implementation
We will create a _Shape_ interface and concrete classes implementing the _Shape_ interface. The next step is to define a facade class _ShapeMaker_.
The _ShapeMaker_ class uses the concrete classes to represent user calls to these classes. The _FacadePatternDemo_ class uses the _ShapeMaker_ class to display results.

### Step 1
Create an interface.
## Shape.java
public interface Shape{void draw(); }
### Step 2
Create concrete classes implementing 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
Execute the program, output the result:
Circle::draw()Rectangle::draw()Square::draw()
YouTip