Facade pattern

Facade Pattern hides the complexity of the system and provides an interface through which clients can access the system. This type of design pattern belongs to structural patterns, and 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 delegates calls to 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 internal complexity of the subsystem.

Main problems solved

  • Reduce the coupling between clients and complex subsystems.
  • Simplify client operations on complex systems and hide internal implementation details.

Use Case

  • When clients do not need to understand the complex internal logic and component interactions of the system.
  • When a clear entry point needs to be defined for the entire system.

Implementation Approach

  • Create facade classDefine a class (Facade) as an intermediary between clients and subsystems.
  • Encapsulate subsystem operationsThe Facade class encapsulates complex subsystem operations into simple methods.

Key code

  • Facade classProvide a high-level interface to simplify interaction between clients and subsystems.
  • Subsystem ClassImplement specific business logic, which is called by the Facade class.

Application example

  1. Hospital ReceptionThe hospital receptionist simplifies complex processes such as registration, outpatient consultation, pricing, and medication dispensing.
  2. Java Three-Tier ArchitectureThrough the Facade pattern, access to the presentation layer, business logic layer, and data access layer can be simplified.

Advantages

  1. Reduce DependenciesDependencies between clients and subsystems are reduced.
  2. Improve flexibilityInternal changes in the subsystem do not affect clients.
  3. Enhance securityHides the internal implementation of the subsystem and only exposes necessary operations.

Disadvantages

  • Violates the open-closed principleModifications to the subsystem may require corresponding changes to the Facade class.

Usage suggestions

  • 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 clients to use.

Notes

  • The Facade pattern is suitable for layered structures and can provide a clear entry point for each layer.
  • Avoid overusing the Facade pattern to prevent hiding too many details, which may lead to maintenance difficulties.

Structure

The Facade Pattern involves the following core roles:

  • Facade:

    • Provide a simplified interface that encapsulates the complexity of the system. Clients of the Facade pattern interact with the facade object without needing to deal directly with the various components of the system.
  • Subsystem:

    • Composed of multiple interrelated classes, responsible for the specific functions of the system. The facade object completes client requests by calling these subsystems.
  • Client:

    • Use the facade object to interact with the system without needing to understand the specific internal implementation of the system.

Implementation

We will create aShapeInterface and implementationShapeThe entity class of the interface. The next step is to define a facade class.ShapeMaker。

ShapeMakerThe class uses entity classes to represent user calls to these classes.FacadePatternDemoClass usageShapeMakerclass to display the result.

外观模式的 UML 图

Step 1

Create an interface.

Shape.java

public interface Shape { void draw(); }

Step 2

Create an entity class that implements 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 the result:

Circle::draw()
Rectangle::draw()
Square::draw()
other extensions