Java Design Pattern Rookie series (ix) modeling and realization of appearance pattern

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39805735


Appearance mode (facade): To resolve dependencies between classes and classes, like spring, you can configure the relationship between classes and classes into a configuration file, and the appearance pattern is to add their

Relationships are placed in a facade class, reducing the degree of coupling between classes and classes, which does not involve interfaces.

First, UML modeling:



Second, the code implementation:

/** * Example: Appearance mode, also called façade mode * Advantages: To solve the dependency between class and class, reduce the degree of coupling between class and class * * This mode does not involve interface */class Memory {public void startup () {System.ou T.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 memory, CPU instances * * Task let computer help us deal with, we do not have to deal with memory, CPU * * Here is a bit like going to the store to buy things: we buy things only 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 ...");} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) {Computer computer = new computer (); computer.st Artup (); System.out.println ("\ n"); Computer.shutdown ();}}

Iii. Summary

If we do not have the computer class, then, the CPU, memory they will hold each other instance, the relationship, which will cause serious dependence, modify a class, may bring other classes of modification, 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.