The appearance model

Appearance mode: also called “appearance mode” belongs to the structural design mode, which is relatively simple, common and widely used one of the design modes. The main goal is to simplify client requests by providing delegate calls to methods in the existing system

define

If an external wants to communicate with the interior, it must do so through an object with a high-level interface provided by the interior. In plain English, the outside can communicate with the inside only through the only communication window provided by the inside. The purpose is to hide the complexity of the system and simplify the interface between the client and the system.

example

It’s time for chestnuts again. In modern society, everyone is living a fast-paced life, and the quality of life is gradually improving. Many times, we need something special to satisfy ourselves. At this time, there is a kind of business personal customization, for example, now you want to assemble a new computer host, this day you go to visit the computer city code:

longhand

If you do it yourself (not in a pattern)

/** * The steps to assemble a computer host */
public interface IBuildUp {
    public void getCpu(a);
    public void getMotherBoard(a);
    public void getMemory(a);
    public void getPower(a);
    public void getStorage(a);
    public void getFans(a);
    public void getChassis(a);
    public void build(a);
}

public class BuildUpImpl implements IBuildUp{
    @Override
    public void getCpu(a) {
        System.out.println("Took a CPU.");
    }

    @Override
    public void getMotherBoard(a) {
        System.out.println("Took a motherboard.");
    }

    @Override
    public void getMemory(a) {
        System.out.println("Took two 8gs of ram.");
    }

    @Override
    public void getPower(a) {
        System.out.println("Took a power supply.");
    }

    @Override
    public void getStorage(a) {
        System.out.println("Took a solid-state drive.");
    }

    @Override
    public void getFans(a) {
        System.out.println("Got some radiators.");
    }

    @Override
    public void getChassis(a) {
        System.out.println("Took a case.");
    }

    @Override
    public void build(a) {
        System.out.println("Put the hardware together and install the system => computer host"); }}public class Client {
    public static void main(String[] args) {
        IBuildUp computer = newBuildUpImpl(); computer.getCpu(); computer.getMemory(); computer.getMotherBoard(); computer.getPower(); computer.getStorage(); computer.getFans(); computer.getChassis(); computer.build(); }}Copy the code

Running results:

Appearance mode version

But isn’t it tiring to work all day to get the mainframe you want? All sorts of rollovers are possible in the process. But fortunately computer city is not a variety of stores? Isn’t there someone who can install it for you? Why don’t you just leave it to them? All you have to do is tell him you’re going to install a mainframe computer, and he’ll do it for you. Isn’t it! Continue coding: a computer store is needed

/** * computer store */
public class Store {
    IBuildUp computer = new BuildUpImpl();

    public Store(a) {
        System.out.println("Computer Store");
    }

    /** ** installation services */
    public void getComputer(a){ computer.getCpu(); computer.getMemory(); computer.getMotherBoard(); computer.getPower(); computer.getStorage(); computer.getFans(); computer.getChassis(); computer.build(); }}Copy the code

All you have to do is use their installation service at the store and get the mainframe you want

public class Client {
    public static void main(String[] args) {
        Store store = newStore(); store.getComputer(); }}Copy the code

Running results:

Among themStoreIt’s the key. It’s the interface between the external and the internal. It has dealt with the internal complex operation and the data acquisition of each subsystem, leaving only one access interface to the outside, simplifying the access mode. The outside world just needs to talk to it, and it can do a lot of complicated operations indirectly.

In addition, if the merchant is in a good mood and gives you some LED light strips, you only need to modify them in the Store, and the place of calling does not need to be changed

public class LED {

    public void setLED(IBuildUp build){
        System.out.println("Add LED light belt to the main engine."); }}/** * computer store */
public class Store {
    IBuildUp computer = new BuildUpImpl();
    LED led = new LED();
    public Store(a) {
        System.out.println("Computer Store");
    }

    /** ** installation services */
    public void getComputer(a){ computer.getCpu(); computer.getMemory(); computer.getMotherBoard(); computer.getPower(); computer.getStorage(); computer.getFans(); computer.getChassis(); led.setLED(computer); computer.build(); }}Copy the code

Running results:

The advantages and disadvantages

  • Advantages: Decoupling, reduce system interdependence, improve flexibility and security
  • Disadvantages: inconsistent with the open and close principle, meet the need to modify when only to modify the facade role of the code.