Facade Mode (Facade Mode)

Provides a consistent interface for a set of interfaces in a subsystem.

To a certain extent, it also achieves a "decoupling" effect - any changes in internal subsystems will not affect changes in the Facade interface.

 

Intent: To provide a consistent interface for a set of interfaces in a subsystem , the Facade pattern defines a high-level interface that makes this subsystem easier to use.

The main solution: reduce the complexity of accessing the internal subsystems of complex systems and simplify the interface between the client and it.

When to use:

1. The client does not need to know the complex connections within the system, and the entire system only needs to provide a "receptionist" .

2. Define the entrance of the system.

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

Key code: Add another layer between the client and the complex system, which handles the order of calls, dependencies, etc.

Application examples:  1. Going to the hospital to see a doctor may involve registration, outpatient treatment, price pricing, and medicine collection, which makes the patient or the patient's family feel very complicated. If there is a receptionist , only the receptionist can handle it, which is very convenient. 2. The three-tier development model of JAVA.

Advantages:  1. Reduce system interdependence. 2. Improve flexibility. 3. Improve the security.

 

Disadvantages: It does not conform to the principle of opening and closing. If it is very troublesome to change things, inheritance and rewriting are not suitable.

http://www.runoob.com/design-pattern/facade-pattern.html

http://blog.csdn.net/hguisu/article/details/7533759



  
 Example: A main power switch can control four lights, a fan, an air conditioner and a TV to turn on and off. The main power switch can control all the above electrical equipment at the same time, and the main power switch is the design of the appearance mode of the system.

 

Facade mode has the following advantages :

1) Shielding subsystem components from clients reduces the number of objects handled by clients and makes the subsystem easier to use. By introducing the Facade pattern, the client code will be simple with few objects associated with it.

2) The loose coupling relationship between the subsystem and the client is realized, which makes the component change of the subsystem not affect the client class that calls it, and only needs to adjust the appearance class.

3) It reduces compilation dependencies in large-scale software systems and simplifies the porting process of systems between different platforms, because compiling one subsystem generally does not require compiling all other subsystems. Modifications to one subsystem have no effect on other subsystems, and changes within subsystems do not affect appearance objects.

4) It only provides a unified entry for accessing the subsystem, and does not affect the user's direct use of the subsystem class.

Disadvantages of Facade Mode

1) Clients cannot be well restricted from using subsystem classes, and if too many restrictions are imposed on client access to subsystem classes, variability and flexibility will be reduced.

 

2) In the case of not introducing an abstract facade class, adding new subsystems may require modifying the facade class or the source code of the client, which violates the "open-closed principle".



 

step 1

Create an interface.

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 skin 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 this appearance 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();		
   }
}

 

 

...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326252385&siteId=291194637