Latest web development tutorials

Facade pattern

Facade pattern (Facade Pattern) hides the complexity of the system to the client provides a client can access the system interface. This type of design patterns belong structural model, it adds an interface to an existing system, to hide the complexity of the system.

This model involves a single class, which provides a simplified approach to client requests and delegate calls to existing systems class methods.

Introduction

Intent: The subsystem provides a set of interfaces in a consistent interface, schema defines the appearance of a high-level interface that makes the subsystem easier to use.

The main solution: reduce the complexity of accessing internal subsystems when complex systems, simplify client with the interface.

When to use: 1, the client does not need to know the system inside the complex links, the entire system need only provide a "receptionist" can be.2, the inlet define the system.

How to fix: the client is not coupled with the system, the system coupled with the appearance of class.

The key code: between the client system and add a layer of complexity, this time will be calling sequence, dependencies and other handling.

Application examples: 1, go to the hospital, you may go to registration, outpatient, designated price, dispensary, the patient or family members of patients find it very complicated, if there are provided reception staff, just let the reception staff to deal with, very convenient.2, JAVA three-tier development model.

Advantages: 1, to reduce the system are interdependent.2, increase flexibility. 3, to improve security.

Disadvantages: does not comply with the principle of opening and closing, if you want to change things too much trouble, inheritance rewrite are inappropriate.

Usage scenarios: 1, to provide access to external modules are complex modules or subsystems.2, relatively independent subsystems. 3, the prevention of low-level personnel risks.

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

achieve

We will create aShapeinterface and implementation of the entity classesShapeinterface. The next step is to define a skin classShapeMaker.

ShapeMakerclass uses an entity class to represent a user calls to these classes.FacadePatternDemo,our demonstration classes usingShapeMakerclass to display the results.

Facade pattern UML diagram

step 1

Create an interface.

Shape.java

public interface Shape {
   void draw ();
}

Step 2

Create 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 class appearance.

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

The use of various types of skin class to draw the shape.

FacadePatternDemo.java

public class FacadePatternDemo {
   public static void main (String [] args) {
      ShapeMaker shapeMaker = new ShapeMaker ();

      shapeMaker.drawCircle ();
      shapeMaker.drawRectangle ();
      shapeMaker.drawSquare ();		
   }
}

Step 5

Verify output.

Circle :: draw ()
Rectangle :: draw ()
Square :: draw ()