Design Patterns - skin mode

First, the basic introduction

Facade pattern (structural): Foreign there is a unified interface, external applications do not care about the specific details of the internal subsystems, it would greatly reduce the complexity of the application, improve the maintainability of the program.

Second, a role that includes

1. Appearance role: to provide a common interface subsystem outside.

2. Subsystem role: to achieve part of the system functions, customers can access it through the appearance of the characters.

Third, cases and UML class diagrams

Case Description:

           Animal interface defines eat sleep run three features that make the different animals to achieve its methods, different types of operations.

UML 类图:

Class Animal:

public interface Animal {

    void eat();

    void sleep();

    void run();
}

Description: Animal Interface, the appearance of roles, subsystems defined public interfaces.

Class Dog:

public class Dog implements Animal {
    @Override
    public void eat() {
        System.out.println("用舌头舔着吃");
    }

    @Override
    public void sleep() {
        System.out.println("趴着睡");
    }

    @Override
    public void run() {
        System.out.println("四只脚跑");
    }
}

Description: dogs, subsystems role to achieve the dog's function.

Class Person:

public class Person implements Animal {

    @Override
    public void eat() {
        System.out.println("用手把食物放入嘴巴吃");
    }

    @Override
    public void sleep() {
        System.out.println("躺着睡");
    }

    @Override
    public void run() {
        System.out.println("两只脚跑");
    }
}

Description: Human, subsystems role, the realization of human functions.

Class FacadeTest:

public class FacadeTest {

    public static void main(String[] args) {
        Animal animal = new Person();
        animal.eat();
        animal.run();
        animal.sleep();
        System.out.println("——————————");
        animal = new Dog();
        animal.eat();
        animal.run();
        animal.sleep();
    }
}

Description: Test and client classes.

Fourth, application scenarios

1. Apply to the development of a hierarchical structure, such as controller, service, dao in the development of three-tier structure, is to call through the interface.

2. The need for shielding visitor details of each subsystem, and provides an interface required for its demand.

Published 35 original articles · won praise 61 · views 50000 +

Guess you like

Origin blog.csdn.net/m0_37914588/article/details/103906082