Appearance mode (facade)

Source: Internet
Author: User

The appearance pattern is to solve the dependency between classes and classes, like spring, the relationship between classes and classes can be configured into a configuration file, and the appearance pattern is to put their relationship in a facade class, reducing the coupling between class classes, which does not involve interfaces, Look at the class diagram: (We take the startup process of a computer as an example)

public class CPU {public void startup () {System.out.println ("CPU startup!");} public void shutdown () {System.out.println ("CPU shutdown!");}}
public class Disk {public void startup () {System.out.println ("Disk startup!");} public void shutdown () {System.out.println ("Disk shutdown!");}}
public class memory {public void  startup () {System.out.println ("memory startup!");} Public void shutdown () {System.out.println ("memory shutdown!");}} 
public class computer {private cpu cpu;private memory memory;private  Disk disk;    public computer () {    this.cpu=new cpu ();     this.disk=new disk ();     this.memory=new memory ();     }    public void startup ()     {     system.out.println ("computer startup!");     cpu.startup ();     memory.startup ();     Disk.startup ();     system.out.println ("start computer finish!");     }        public void shutdown ()     {    system.out.println ("begin to close the  Computer! ");     cpu.shutdown ();     memory.shutdown ();    disk.shutdown ();     system.out.println ("computer close!");     }}
public class User {public static void main (string[] args) {computer computer =new computer (); Computer.startup (); computer. Shutdown ();}}

Test results:

Computer startup!

CPU startup!

Memory startup!

Disk startup!

Start Computer finish!

Begin to close the computer!

CPU shutdown!

Memory shutdown!

Disk shutdown!

Computer close!

If we do not have computer class, then, CPU, Memory, disk 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 is placed in the computer class, so that the understanding of the role of decoupling , which is the appearance of the pattern!

Appearance mode (facade)

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.