Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Facade Pattern

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

Use Cases

Implementation

Key code

Application Example

  1. Hospital reception: the reception staff at a hospital simplify the complex processes of registration, outpatient service, price calculation, and medicine collection.
  2. Java three-tier architecture: through the Facade Pattern, access to the presentation layer, business logic layer, and data access layer can be simplified.

Advantages

  1. Reduce dependencies: the dependency between the client and the subsystem is reduced.
  2. Improved flexibility: internal changes in the subsystem do not affect the client.
  3. Enhanced security: the internal implementation of the subsystem is hidden, exposing only the necessary operations.

Disadvantages

Usage recommendation

Notes

structures

The Facade Pattern involves the following core roles:

- 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. - 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. - 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()