Facade Pattern)

Source: Internet
Author: User

Facade Pattern) 

Facade mode definition:

The facade mode is also called the appearance mode, which is a common encapsulation mode. It defines that the external communication between a sub-system and its internal communication must be performed through a unified object. The facade mode provides a high-level interface to make subsystems easier to use.

 

Advantages of Facade Mode:

1. Reduce system dependencies. The caller accesses the subsystem through the interface provided by the facade role, so that as long as the interface provided by the facade role does not change, how the subsystem changes will not affect the high-levelCodeBecause the facade role is the same facade for callers.

2. high flexibility. Reduced dependencies and increased flexibility.

3. Improved security. A facade role can restrict external sub-system functions that can be accessed. You only need to provide an interface for the required functions. If no interface is provided, the sub-system functions cannot be accessed.

 

Application scenarios of the facade mode:

1. An external access interface must be provided for a complex module or subsystem. For example, if you want to use a system function, you need to call 50 functions, and 50 functions need to be in a certain order. You cannot directly call these 50 functions in order, what if it is more complicated (there are 5000 functions -_-!!), In this case, once a function fails, you will see it again. In this way, the write high-level module is strongly coupled with the subsystem module. Once the subsystem is modified, the high-level module also needs to be changed. To solve this kind of strong coupling, we can use the facade role to encapsulate the subsystem and then provide the business interface for the high-level module, so that the high-level module only depends on the facade role, the modification of the subsystem does not affect the high-level module.

2. subsystems are relatively independent-only black box operations are required.

 

General class diagram of the facade mode:

Facade: this role encapsulates the sub-system and knows its functions and responsibilities. This role delegates all requests sent from the client to the corresponding subsystem. It is actually only a delegate class with no actual business logic. The high-level module uses the interfaces provided by this role to access sub-interfaces.

Subsystem (subsystem role): either one or more subsystems. Each system is a collection of classes. The sub-system does not know the existence of the facade role. In terms of this, the facade role is actually a client.

 

Common Code for facade mode:

Subsystem role:

 /* Subsystem  */ 
Public Class Sub‑ema
{
Public Void Logica ()
{
System. Out . Println ( " Logica " );
}
}

/* Subsystem B */
Public Class Subsystemb
{
Public Void Logicb ()
{
System. Out . Println ( " Logicb " );
}
}

/* Subsystem C */
Public Class Subscribe EMC
{
Public Void Logicc ()
{
System. Out . Println ( " Logicc " );
}
}

Facade role:

Public   Class Facaderole
{
/* Encapsulated subsystem set */
Private Sub‑ema extends EMA = New Sub‑ema ();
Private Subsystemb systemb = New Subsystemb ();
Private Subscribe EMC = New Subscribe EMC ();

// Interfaces that call the function of subsystem
Public Void Logica ()
{
/* Delegate to system */
Systema. Logica ();
}

// Interfaces that call the function of subsystem B
Public Void Logicb ()
{
/* Delegate to system B */
Systemb. logicb ();
}

// Interfaces that call the function of subsystem B
Public Void Logicc ()
{
/* Delegate to system c */
SystemC. logicc ();
}
}

scenario:

  Public   class  client 
{< br> Public static void main (string [] ARGs)
{< br> /* Create a facade role */
facaderole = New facaderole ();
/* function of Access Subsystem A */
facaderole. logica ();
/* Access Subsystem B Functions */
facaderole. logicb ();
/* Access Subsystem C functions */
facaderole. logicc ();
}< BR >}


Supplement of Facade Mode

Supplement 1: The subsystem can have multiple facades

Generally, a sub-system only needs one facade role, but you need to consider multiple facade roles in the following situations:

1. When a facade role is relatively large, it can be divided into multiple smaller facade roles by function category.

2. Different access paths must be provided for the subsystem. For example, the subsystem provides full-function access to the trust module and restricted function access to the untrusted module. In this case, multiple facade modes can be used to restrict the accessible functions of different modules.
 

Supplement 2: The facade role should not be involved in the business logic of the subsystem.

Public   Class Facaderole
{
/* Encapsulated subsystem set */
Private Sub‑ema extends EMA = New Sub‑ema ();
Private Subsystemb systemb = New Subsystemb ();
Private Subscribe EMC = New Subscribe EMC ();

// Interfaces that call the function of subsystem
Public Void Logica ()
{
/* Delegate to system */
Systema. Logica ();
}

// Interfaces that call the function of subsystem B
Public Void Logicb ()
{
/* Delegate to system B */
Systemb. logicb ();
}

// Interfaces that call the function of subsystem B
Public Void Logicc ()
{
/* Delegate to system */
Systema. Logica ();
/* Delegate to system c */
SystemC. logicc ();
}
}

The logicc () method is modified as above. It seems that there are no errors, but it is actually incorrect. The definition of the facade Mode means that the facade role is a unified interface for accessing the subsystem. It is just a delegate class and provides the path for accessing the subsystem, do not participate in the specific business logic (this modification has already been involved in the subsystem logic, you must first execute the Logica of system A and then execute the logicc of system C ). The above modification will lead to a very serious dependency: The subsystem must rely on the facade to be correctly accessed. When multiple subsystems are dependent, encapsulate the dependent subsystems.

Public ClassContextac
{
PrivateSub‑ema extends EMA =NewSub‑ema ();
PrivateSubscribe EMC =NewSubscribe EMC ();

Public VoidExecute ()
{
Systema. Logica ();
SystemC. logicc ();
}
}

Facade role:

 Public   Class Facaderole
{
/* Encapsulated subsystem set */
Private Sub‑ema extends EMA = New Sub‑ema ();
Private Subsystemb systemb = New Subsystemb ();
Private Contextac = New Contextac ();

// Interfaces that call the function of subsystem
Public Void Logica ()
{
/* Delegate to system */
Systema. Logica ();
}

// Interfaces that call the function of subsystem B
Public Void Logicb ()
{
/* Delegate to system B */
Systemb. logicb ();
}

// Interfaces that call the function of subsystem B
Public Void Logicc ()
{
Contextac.exe cute ();
}
}

After this encapsulation, the facade role has nothing to do with the specific business. The facade mode remains unchanged as the subsystem changes.

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.