Appearance mode: also known as "facade mode", it belongs to structural design mode. It is one of the relatively simple, common and widely used design modes. The main purpose is to simplify client requests by providing delegate calls to methods in existing systems
definition
If the external wants to communicate with the internal, it must be through an object with a high-level interface provided internally. In vernacular, that is to say, external communication with internal communication can only be through the only communication window provided internally. Its purpose is to hide the complexity of the system and simplify the interface between the client and the system.
example
It's time to lift chestnuts again. In modern society, we all live a fast-paced life, and the quality of life has gradually improved. Many times, we need some special things to meet ourselves. At this time, there is a kind of business private customization
For example, now you want to install a new computer host. On this day, you go to the computer city
Upper Code:
Common writing
If it is assembled by itself (no mode is used)
/** * Steps for assembling a computer host */ public interface IBuildUp { public void getCpu(); public void getMotherBoard(); public void getMemory(); public void getPower(); public void getStorage(); public void getFans(); public void getChassis(); public void build(); } public class BuildUpImpl implements IBuildUp{ @Override public void getCpu() { System.out.println("Took a piece CPU"); } @Override public void getMotherBoard() { System.out.println("Took a motherboard"); } @Override public void getMemory() { System.out.println("Took two 8 G Memory"); } @Override public void getPower() { System.out.println("Took a power supply"); } @Override public void getStorage() { System.out.println("Took a solid state drive"); } @Override public void getFans() { System.out.println("Got some radiators"); } @Override public void getChassis() { System.out.println("Took a chassis"); } @Override public void build() { System.out.println("Assemble the hardware and install the system => Computer host"); } } public class Client { public static void main(String[] args) { IBuildUp computer = new BuildUpImpl(); computer.getCpu(); computer.getMemory(); computer.getMotherBoard(); computer.getPower(); computer.getStorage(); computer.getFans(); computer.getChassis(); computer.build(); } }
Operation results:
Skin mode version
But isn't it tired to work all day to get the host you want? There may also be various rollovers in the process. But fortunately, isn't there a variety of stores in the computer city? Isn't there someone who can help you install the machine? Don't you just leave it to them? You just need to tell him that if you want to install a computer host, won't he do it for you? Yeah! Continue Code:
We need a computer store at this time
/** * Computer store */ public class Store { IBuildUp computer = new BuildUpImpl(); public Store() { System.out.println("Computer store"); } /** * Installation service */ public void getComputer(){ computer.getCpu(); computer.getMemory(); computer.getMotherBoard(); computer.getPower(); computer.getStorage(); computer.getFans(); computer.getChassis(); computer.build(); } }
You only need to use their installation service in this store to get a computer host you want
public class Client { public static void main(String[] args) { Store store = new Store(); store.getComputer(); } }
Operation results:
Store is the key, which is the interface for external and internal communication. It processes the internal complex operations and the data acquisition of each subsystem, leaving only one external access interface to simplify the access mode. External only need to communicate with it, can indirectly complete many complex operations.
In addition, if the merchant is in a good mood and sends you some LED light strips, you only need to modify them in the Store, and the calling place does not need to be changed
public class LED { public void setLED(IBuildUp build){ System.out.println("Add to the host LED Light belt"); } } /** * Computer store */ public class Store { IBuildUp computer = new BuildUpImpl(); LED led = new LED(); public Store() { System.out.println("Computer store"); } /** * Installation service */ public void getComputer(){ computer.getCpu(); computer.getMemory(); computer.getMotherBoard(); computer.getPower(); computer.getStorage(); computer.getFans(); computer.getChassis(); led.setLED(computer); computer.build(); } }
Operation results:
Advantages and disadvantages
- Advantages: decoupling, reducing system interdependence, improving flexibility and security
- Disadvantages: it does not comply with the opening and closing principle. When it needs to be modified, you can only modify the code of the facade role.