Design Mode-Facade)

Source: Internet
Author: User

Design Mode-Facade)

What is the appearance mode?

Facade provides a consistent interface for a group of interfaces in the subsystem and defines a high-level interface, which makes the subsystem easier to use.

To put it simply, the appearance mode is a very frequently used structural design mode. It introduces an appearance role to simplify the interaction between the client and the subsystem, provides a unified portal for complex subsystem calls, reduces the coupling between subsystems and clients, and facilitates client calls.

Overview:

In a real application system, a sub-system may consist of many classes. Subsystem customers need to interact with some classes in the subsystem for their needs. Direct interaction between customer and subsystem classes will result in high coupling between client objects and subsystems. Any modification similar to the class interface in the subsystem will affect all customer classes dependent on it.

From the definition of the appearance mode above, we can see that the appearance mode can effectively solve the above problems and provide a higher level and simpler interface for the subsystem, this reduces the complexity and dependency of subsystems. This makes subsystems easier to use and manage.

Appearance is a class that provides simple interfaces for subsystems and customers. When the correct application looks, the customer no longer interacts directly with the class in the subsystem, but with the appearance. The appearance is responsible for Class Interaction with the subsystem. In fact, the appearance is the interface between the subsystem and the customer, so that the appearance mode reduces the coupling between the subsystem and the customer.

Instance:

The following uses a simple example to describe the appearance mode:

Structure:

The sub-system class is usually a business class that implements some specific and independent business functions. The typical code is as follows:

 

class One{    public void MethodA(){        System.out.println( methodA--> is runing );    }}class Two{    public void MethodB(){        System.out.println( methodB--> is runing );     }}class Three{    public void MethodC(){        System.out.println( methodC--> is runing );    }}class Four{    public void MethodD(){       System.out.println( methodD--> is runing );    }}

 

After the appearance class is introduced, the interaction with the Subsystem Service class is completed by the appearance class. The following code usually exists in the appearance class:

 

Class Facade {private One obj1 = new One (); private Two obj2 = new Two (); private Three obj3 = new Three (); private Four obj4 = new Four (); public void Method1 () {System. out. println (method group 1); obj1.MethodA (); obj2.MethodB (); obj3.MethodC ();} public void Method2 () {System. out. println (method group 2); obj1.MethodA (); obj2.MethodB (); obj4.MethodD ();}}

 

Because the reference to the subsystem object is maintained in the appearance class, the client can indirectly call the Business Method of the subsystem object through the appearance class, without directly interacting with the subsystem object. After the appearance class is introduced, the client code becomes very simple. The typical code is as follows:

 

class Program{    static void Main(string[] args){        Facade facade = new Facade();        facade.Method1();facade.Method2();    }}

 

When do I use the appearance mode?

First, in the initial stage of design, we consciously separate two different layers and establish an external Facade between the layer and the layer. In this way, we can provide a simple interface for complicated subsystems and reduce coupling.

Second: in the development phase, subsystems tend to become more and more complex due to constant reconstruction and evolution. Increasing the appearance of Facade can provide a simple interface to reduce dependencies between them.

Third: When maintaining a large system, it may be very difficult to maintain and expand the system. At this time, we can develop a new Facade class for the system, to provide clear and simple interfaces for the design of rough or highly complex legacy code, so that the new system can interact with the Facade object, and the Facade and legacy code can interact with all the complicated work.

 

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.