Java design mode-appearance mode (facade)

Source: Internet
Author: User

The appearance pattern is to solve the dependency of class and class, like spring, the relationship between classes and classes can be configured into the 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)

Let's look at the implementation class first:

[Java]View PlainCopy
    1. public class  cpu {  
    2.       
    3.      public void startup () {  
    4.         system.out.println ( "cpu startup!");   
    5.     }  
    6.        
    7.     public  void shutdown () {  
    8.         system.out.println ( "Cpu shutdown !");   
    9.     }  
    10. }  
[Java]View PlainCopy
    1. public class  memory {  
    2.       
    3.     public void startup () {   
    4.         system.out.println ( "memory startup!");   
    5.     }  
    6.        
    7.     public  void shutdown () {  
    8.         system.out.println ( "memory  Shutdown! ");   
    9.     }  
    10. }  
[Java]View PlainCopy
    1. public class  disk {  
    2.       
    3.      public void startup () {  
    4.         system.out.println ( "disk startup!");   
    5.     }  
    6.        
    7.     public  void shutdown () {  
    8.         system.out.println ( "disk  Shutdown! ");   
    9.     }  
    10. }  
[Java]View PlainCopy
  1. Public class Computer {
  2. private CPU CPU;
  3. private memory memory;
  4. private disk disk;
  5. Public computer () {
  6. CPU = new CPU ();
  7. Memory = new Memory ();
  8. Disk = new disk ();
  9. }
  10. public void Startup () {
  11. System.out.println ("Start the computer!");
  12. Cpu.startup ();
  13. Memory.startup ();
  14. Disk.startup ();
  15. System.out.println ("Start computer finished!");
  16. }
  17. public void shutdown () {
  18. SYSTEM.OUT.PRINTLN ("begin to close the computer!");
  19. Cpu.shutdown ();
  20. Memory.shutdown ();
  21. Disk.shutdown ();
  22. SYSTEM.OUT.PRINTLN ("Computer closed!");
  23. }
  24. }

The user class is as follows:

[Java]View PlainCopy
    1. Public class User {
    2. public static void Main (string[] args) {
    3. Computer computer = new computer ();
    4. Computer.startup ();
    5. Computer.shutdown ();
    6. }
    7. }

Output:

Start the computer!
CPU startup!
Memory startup!
Disk startup!
Start Computer finished!
Begin to close the computer!
CPU shutdown!
Memory shutdown!
Disk shutdown!
Computer closed!

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!

Java design mode-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.