Appearance mode in Java design mode (façade mode)

Source: Internet
Author: User

First, Concept introduction

Appearance mode (facade), he hides the complexity of the system and provides the client with an interface that can access the system. This type of design pattern belongs to the structural model. Provides a unified access interface for a set of interfaces in a subsystem that makes it easier to access or use the subsystem.

Ii. Roles and usage scenarios

In short, the pattern is to encapsulate some complex processes into an interface for easier use by external users. In this mode, design to 3 characters.

1). Façade role: The core of the appearance mode. It is called by the client role and is familiar with the functions of the subsystem. A combination of several functions is scheduled internally based on the needs of the customer role.

2). Subsystem role: realizes the function of subsystem. It is unknown to the customer role and facade. It can have internal interaction within the system or an interface that can be called by the outside world.

3). Customer role: Complete the functionality to be implemented by calling Facede.

Usage scenarios:

1-modules that provide external access to complex modules or subsystems;

2-subsystems are independent of each other;

3-In a chromatography structure, you can use the appearance pattern to define the entry for each layer of the system.

Third, examples

Below, we will implement this pattern with a simple example.

Each computer has CPU, Memory, Disk. When the computer is turned on and off, the corresponding parts are also turned on and off, so that the appearance mode is used to decouple the user from the part. Such as:

The creation of the package body:

Code implementation

The first is the Subsystem class:

"Code Listing--1"

1 package Com.huawei.facadeDesign.children; 2  3 import Org.apache.log4j.Logger; 4  5/** 6  * CPU Subsystem Class 7  * @author Administrator 8  * 9  */10 Publi C class CPU Each {public     static final Logger Logger = Logger.getlogger (cpu.class), public     void Start ()         Logger.info ("CPU is start ..."),     }17 public     void ShutDown (),         Logger.info ("CPU is ShutDown ...");     }22}

"Code Listing--2"

1 package Com.huawei.facadeDesign.children; 2  3 import Org.apache.log4j.Logger; 4  5/** 6  * Disk Subsystem Class 7  * @author Administrator 8  * 9  */10 Publ IC class Disk11 {public     static final Logger Logger = Logger.getlogger (disk.class), public     void Start () 14
   
    {15         logger.info ("Disk is start ..."),     }17 public     void ShutDown ()         Logger.info ("Disk is ShutDown ...");     }22}
   

"Code Listing--3"

1 package Com.huawei.facadeDesign.children; 2  3 import Org.apache.log4j.Logger; 4  5/** 6  * Memory Subsystem Class 7  * @author Administrator 8  * 9  */10 PU Blic class Memory11 {public     static final Logger Logger = Logger.getlogger (memory.class), public     void Start ()         logger.info ("Memory is start ..."),     }17 public     void ShutDown ()         Logger.info ("Memory is ShutDown ...");     }22}

And then, the façade class facade

"Code Listing--4"

 1 package Com.huawei.facadeDesign.facade; 2 3 Import Org.apache.log4j.Logger; 4 5 Import Com.huawei.facadeDesign.children.CPU; 6 Import Com.huawei.facadeDesign.children.Disk; 7 Import Com.huawei.facadeDesign.children.Memory; 8 9 10/**11 * Façade Class (CORE) * @author Administrator13 *14 */15 public class Computer16 {+ public static final Logge  R LOGGER = Logger.getlogger (computer.class); private CPU cpu;20 private Memory memory;21 private Disk disk;22 Public computer (), CPU = new CPU (), memory = new Memory (), and disk = new Di         SK ();}28 public void Start () {Logger.info ("computer start Begin"); Cpu.start (); 32 Disk.start (); Memory.start (); Logger.info ("Computer start End");}36 Notoginseng public Voi D ShutDown () logger.info ("Computer ShutDown begin"); Cpu.shutdown (); Disk.shutdown ()        ; Memory.shutdown (); 43 Logger.info ("Computer ShutDown end ..."); 44}45} 

Finally, the customer role.

"Code Listing--5"

1 package com.huawei.facadeDesign; 2  3 import Org.apache.log4j.Logger; 4  5 import com.huawei.facadeDesign.facade.Computer; 6  7/** 8  * Client Class 9  * @author Administrator10  *11  */12 public class Cilent {public     static final Logger Logger = Logg Er.getlogger (Cilent.class); public     static void Main (string[] args)     {         computer computer = new Computer ();         Computer.start ();         logger.info ("=================");         Computer.shutdown (); 20     }21 22}

"Code Listing--6" Run results

From the above example, with this facade class, that is, the computer class, the user does not have to call the subsystem in person Disk,memory, CPU class, do not need to know the implementation details of the system, even do not have to know the structure of the internal system. The client only needs to interact with facade.

Iv. Advantages-Loose coupling

The decoupling between client and subsystem makes it easier to extend and maintain the module function inside the subsystem;

-Easy to use

The client does not need to know the implementation within the subsystem at all, or does not need to know the internal structure of the subsystem, it only needs to interact with the facade class.

-Better partitioning of access levels

Some methods are external to the system, and some methods are used within the system to interact with each other. Subsystems put those exposed to the external functions into the façade, so that the client can be used, it is good to hide the internal details of the subsystem.

Appearance mode in Java design mode (façade 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.