Java Design Pattern modeling and implementation of cainiao series (9) Appearance Pattern
Facade: Solves the dependency between classes. Like spring, you can configure the relationship between classes to the configuration file, the appearance mode is
The relationship is placed in a Facade class, which reduces the coupling between the class and the class. This mode does not involve interfaces.
I. uml modeling:
Ii. Code implementation:
/*** Example: appearance mode, also called facade mode ** advantages: To solve the dependency between classes, reduced coupling between classes ** this mode does not involve interfaces */class Memory {public void startup () {System. out. println (this is memory startup ...);} public void shutdown () {System. out. println (this is memory shutdown ...);}} class CPU {public void startup () {System. out. println (this is CPU startup ...);} public void shutdown () {System. out. println (this is CPU shutdown ...);}} /*** as facade, holding Memor Y, CPU instance ** task allows the Computer to help us deal with, we do not need to directly deal with Memory, CPU ** here is a bit like shopping in the store: we only need to go to the store to buy things, rather than from the manufacturer. ** A store is called a facade appearance (facade) mode. --> All items are in the store */class Computer {private Memory memory; private CPU; public Computer () {memory = new Memory (); cpu = new cpu ();} public void startup () {System. out. println (begin to start the computer ...); memory. startup (); cpu. startup (); System. out. println (computer start finished ...);} public void shutdown () {System. out. println (begin to close the computer ...); memory. shutdown (); cpu. shutdown (); System. out. println (computer close finished ...);}} /*** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {Computer computer = new Computer (); computer. startup (); System. out. println (); computer. shutdown ();}}
Iii. Summary
If we do not have a Computer class, the CPU and Memory instances will hold each other and have a relationship. This will cause serious dependencies and change a class, other classes may be modified. This is not what we want to see. With the Computer class, the relationship between them is placed in the Computer class, which plays a decoupling role, this is the Facade mode.