Java Design Pattern modeling and implementation of cainiao series (9) Appearance Pattern

Source: Internet
Author: User

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.


 

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.