Facade

1. Definitions

Providing a consistent interface for a set of interfaces in a subsystem, the Facade
pattern defines a high-level interface that makes the subsystem easier to use.

2. Structure of Appearance Mode

structure
- Facade: Define the external high-level interface of multiple modules of the subsystem, usually need to call multiple internal modules, so as to proxy the client's request to the appropriate subsystem object.
- Module: Accept the delegation of the Facade object to actually implement the function, and there may be interactions between the various modules.
- Client: Complete the function to be implemented by calling Facede.

3. Examples

The following takes a mobile phone as an example to implement this mode.
A Phone has CPU, Memory (memory), ROM (memory). When the phone is turned on and off, the corresponding parts will also turn on and off. Phone is equivalent to a facade. Decoupling between users and components is achieved.

CPU

/**
 * CPU
 * @author lijun
 * @since 2018-03-27 16:55
 */
public class CPU {
    /**
     * 启动
     */
    public void start(){
        System.out.println("CPU 启动成功!");
    }

    /**
     * 关闭
     */
    public void shutDown(){
        System.out.println("CPU 关闭成功!");
    }
}

Memory

/**
 * Memory
 * @author lijun
 * @since 2018-03-27 16:55
 */
public class Memory {
    /**
     * 启动
     */
    public void start(){
        System.out.println("Memory 启动成功!");
    }

    /**
     * 关闭
     */
    public void shutDown(){
        System.out.println("Memory 关闭成功!");
    }
}

ROM

/**
 * ROM
 * @author lijun
 * @since 2018-03-27 16:55
 */
public class ROM {
    /**
     * 启动
     */
    public void start(){
        System.out.println("ROM 启动成功!");
    }

    /**
     * 关闭
     */
    public void shutDown(){
        System.out.println("ROM 关闭成功!");
    }
}

Phone

/**
 * @author lijun
 * @since 2018-03-27 17:04
 */
public class Phone {
    /**
     * cpu
     */
    private CPU cpu = null;

    /**
     * memory
     */
    private Memory memory = null;

    /**
     * rom
     */
    private ROM rom = null;

    public Phone() {
        cpu = new CPU();
        memory = new Memory();
        rom = new ROM();
    }

    /**
     * start
     */
    public void  start(){
        cpu.start();
        memory.start();
        rom.start();
    }

    /**
     * 关闭
     */
    public void  shutDwon(){
        cpu.shutDown();
        memory.shutDown();
        rom.shutDown();
    }

}

Client


/**
 * @author lijun
 * @since 2018-03-27 17:12
 */
public class Client {
    public static void main(String[] args) {
        Phone phone = new Phone();
        phone.start();
        phone.shutDwon();

    }
}

output:

CPU 启动成功!
Memory 启动成功!
ROM 启动成功!
CPU 关闭成功!
Memory 关闭成功!
ROM 关闭成功!

4. Essence

Encapsulate interactions and simplify calls.

5. Summary

1. Advantages

  • Loose coupling decouples
    the client and the subsystem, making the module functions within the subsystem easier to expand and maintain.
  • Simple and easy to use The
    client does not need to know the internal implementation of the subsystem at all, or does not need to know the internal composition of the subsystem at all, it only needs to interact with the Facade class.
  • Better division of access levels
    Some methods are external to the system, and some methods are used for interaction within the system. The subsystem concentrates the functions exposed to the outside into the facade, so that the use of the client can be realized, and the internal details of the subsystem are well hidden.

2. Disadvantages
Too many or unreasonable Facades can be confusing. In the end it is good to call Facade! Or call the module directly.

Source address

Guess you like

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