Appearance mode

Facade Pattern hides the complexity of the system and provides the client with an interface that the client can access the system. This type of design pattern is a structured pattern that adds an interface to an existing system to hide the complexity of the system.

This pattern involves a single class that provides a simplified method of client request and a delegate call to an existing system class method.

Introduction

Intent: provides a consistent interface to a set of interfaces in the subsystem. The facade mode defines a high-level interface that makes this subsystem easier to use.

Main solution: Reduces the complexity of accessing internal subsystems of complex systems and simplifies the interface with clients.

When to use: 1. The client does not need to know the complicated connection inside the system. The whole system only needs to provide a "receptionist". 2. Define the entrance to the system.

How to solve: The client is not coupled to the system, and the appearance class is coupled to the system.

Key Code: Add another layer between the client and the complex system. This layer handles the calling order, dependencies, and so on.

Application example: 1, go to the hospital to see a doctor, may have to go to the registration, outpatient, price, take medicine, so that patients or family members feel very complicated, if there is a reception staff, only the reception staff to deal with, it is very convenient. 2. JAVA's three-tier development model.

Advantages: 1. Reduce system interdependence. 2. Increase flexibility. 3. Improve safety.

Disadvantages: does not meet the principle of opening and closing. If it is troublesome to change things, inheritance and rewriting are not appropriate.

Usage scenarios: 1. Modules that provide external access to complex modules or subsystems. 2. The subsystems are relatively independent. 3. Prevent the risks brought by low-level personnel.

Note: In a hierarchical structure, you can use the Appearance mode to define the entry for each layer in the system.

Implementation

We will create an Shape interface and an entity class that implements the Shape interface. The next step is to define a skin class ShapeMaker.

The

ShapeMaker class uses entity classes to represent user calls to these classes. FacadePatternDemo, our demo class uses the ShapeMaker class to display the results.

j facade_pattern_uml_diagram

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< Span class="hl-code">.println("Rectangle::draw()"); } }

Square.java

public class Square implements Shape { @Override public void draw() { System.out< Span class="hl-code">.println("Square::draw()"); } }

Circle.java

public class Circle implements Shape { @Override public void draw() { System.out< Span class="hl-code">.println("Circle::draw()"); } }

Step 3

Create a skin class.

ShapeMaker.java

public class ShapeMaker { private Shape circle; private Shape rectangle; private Shape square; public ShapeMaker() { circle= new< Span class="hl-code"> Circle(); rectangle = new< Span class="hl-code"> Rectangle(); square= new< Span class="hl-code"> Square(); } public void drawCircle(){ circle.draw< Span class="hl-brackets">(); } public void drawRectangle(){ rectangle.draw< Span class="hl-brackets">(); } public void drawSquare(){ square.draw< Span class="hl-brackets">(); } }

Step 4

Use this skin class to draw various types of shapes.

FacadePatternDemo.java

public class FacadePatternDemo { public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); shapeMaker.drawCircle< Span class="hl-brackets">(); shapeMaker.drawRectangle< Span class="hl-brackets">(); shapeMaker.drawSquare< Span class="hl-brackets">(); } }

Step 5

Executing the program, outputting results:

Circle::draw< /span>()
Rectangle::draw()
Square::draw()





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition