Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39805735
appearance mode (facade): is to solve the dependency between classes and classes, like spring. The ability to configure the relationship between a class and a class into a configuration file, and the appearance pattern is to set their
Relationships are placed in a facade class, reducing the coupling between classes and classes, which does not involve interfaces.
First, UML modeling:
Second, the code implementation:
/** * Demo Sample: Appearance mode, also known as façade mode * Strengths: To resolve dependencies between classes and classes. Reduced coupling between classes and classes * * This mode does not refer to the interface */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. Hold memory, CPU instances * * Task let computer help us deal with, we do not have to deal with memory, CPU directly * * Here is a bit like going to the store to buy things: we buy things only need to go to the store to buy, and do not need to go to the manufacturer to buy. * The store can be called a facade appearance (façade) mode. -Products are in the store */class computer {Private Memory memory;private CPU 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 ...");} /** * ClientTest class * @author Leo */public class Test {public static void main (string[] args) {Computer computer = new computer (); Compu Ter.startup (); System.out.println ("\ n"); Computer.shutdown ();}}
Iii. Summary
Suppose we don't have the computer class, then. CPU, memory they will hold instances of each other, resulting in a relationship, which can cause serious dependence, change a class, may bring other classes of changes. This is not what we want to see, with the computer class. The relationship between them has been placed in the computer class, so that the understanding of the role of decoupling, which is the appearance of facade mode.
Java Design Pattern Rookie series (ix) modeling and realization of appearance pattern