Java Design pattern Learning record-appearance mode

Source: Internet
Author: User

Objective

This is to introduce the appearance of the model (also known as the façade mode), the appearance of the pattern is also a structural model, in fact, the appearance of the pattern is very good understanding, the simple is to be a number of complex business package into a method, call this method can not be related to the specific execution of what business, and only care about the results. This scene in fact in the daily development of the use of the frequency is still very high, below to briefly understand it.

Introduction to the concept of appearance mode

appearance mode hides the complexity of the system and provides a unified interface to a set of interfaces in a subsystem. the customer does not have to deal with the subsystem when using the system, which reduces the coupling between the customer and the subsystem.

Example

Tea problem, when the Ji Smoke bag with and two want to drink tea, this time they will do their own, take tea, water, tea and then the tea bubble. Process such as.

So two people have to operate their own tea sets, boiling water, tea, and so on, but in fact, they want to drink tea, just don't care how the tea bubble out. So this time they both went to the teahouse, so also do not have to do their own tea, directly to the Teahouse's bartender son on the line. At this point the process becomes the following.

The following code is used to implement this process:

Get drinking water first

 Public class Drinkablewater {    public  drinkablewater () {        System.out.println ("Potable water is ready");    }     // boiled water     Public void Facadewater () {        System.out.println ("Drinking water boils down");}    }

and get the tea.

 Public class Tea {    public  tea () {        System.out.println ("The teas are ready. ");    }     // Take tea     Public void Facadetea () {        System.out.println ("tea can be made.") ");    }}

Then get a cup and you can make tea.

 Public class Teacup {    public  teacup () {        System.out.println ("Cup Ready");    }     // Tea     Public void Facadeteacup (Tea tea) {        tea.facadetea ();        System.out.println ("The tea is soaked into the teacup.") ");        System.out.println ("Waiting for a while, a cup of fragrant and strong tea rushed.") ");    }}

Bartender Tea

 Public class Waiter {    privatenew  drinkablewater ();     Private New teacup ();     Private New Tea ();     // get a cup of tea     Public void Gettea () {        drinkablewater.facadewater ();        Teacup.facadeteacup (tea);}    }

Customers are coming for tea.

 Public class Customer {    publicstaticvoid  main (string[] args) {        // call Bartender        . New waiter ();         // get a cup of tea from bartender         Waiter.gettea ();    }}

Run results

The drinking water is ready, the cup is ready, the tea is ready. But the drinking water boiling can make tea. The tea was soaked into the teacup. After a while, a cup of fragrant and strong tea rushed.
Structure of the appearance pattern

The abstract structure of the appearance pattern is shown below:

The following roles are mainly included in the appearance mode.

1, the façade role (facade): This is the core of the appearance mode. It is called by the client role, so it is familiar with the functions of the subsystem. It has several functional combinations that are scheduled internally based on the needs of the customer's role.

2, subsystem role (SystemA, Systemb, SystemC): Realize the function of subsystem. For a subsystem role, the facade role, like the client role, is unknown and does not have any information or links to the facade role.

3, customer role (client): Call the facade role to complete the functions to be obtained.

In the above example of tea, and the two and Ji Opium bag is the customer role, teahouse bartender son is the façade role, tea sets, drinking water, tea is the subsystem role.

Advantages of the Appearance mode

1, the client block the subsystem components, reduce the number of client processing objects, but also reduce the amount of code for the client.

2, the client and subsystem of the loose coupling, so that the subsystem changes will not affect the client to call it, only need to change the appearance of the class.

3, one subsystem changes will not affect another subsystem, the internal changes in the subsystem will not affect the appearance of the object.

Disadvantages of Appearance mode

1, the client can not be very well restricted to use the subsystem class, if the client Access subsystem classes to do too many restrictions reduce variability and flexibility.

2, if the design is inappropriate, adding new subsystems may need to modify the appearance of the source code of the class, contrary to the open and closed principle.

Applicable scenarios

You can use appearance mode when you want to provide a simple entry for accessing a complex set of subsystems.

There is a large dependency between the client program and several subsystems. The introduction of the appearance class can decouple the subsystem from the client, thus improving the independence and portability of the subsystem.

In a hierarchical structure, you can use the appearance pattern to define the entry of each layer in the system, and the layers are not directly related to the layers, but the contact is established through the appearance class to reduce the coupling between the layers.

Extended

In the above example, we define the façade is a specific class, but when the need to add new features, you need to modify the façade class, so the best way is to make an abstract façade, that is, the function of the façade class is abstracted out, and then different requirements, you can do two concrete façade class. For example: Ji Opium bag heel and two go teahouse tea is a façade class, go to the street to buy something is another façade class.

To learn more about design patterns, see the Java Design Patterns Learning record-gof design pattern Overview.

If I don't panic, the world will not panic. Come on!

Java Design pattern Learning record-appearance 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.