Design Pattern appearance

Source: Internet
Author: User
Facade)
Provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes the subsystem easier to use.




During the summer vacation, many of our students chose to cook their own meals. Today we will introduce two typical traditional dishes that are popular among the masses: braised pork ribs and braised fish.

The preparation steps of braised pork ribs and braised fish are roughly divided into four steps: preparation materials, pickling materials, frying, and juice adjustment. Then you can make a hearty braised pork ribs and braised fish! Then, we must remember every step of creation, and the sequence cannot be reversed. Otherwise, the effect will be different. How can we achieve this easily? This is what we will look like today.

In simple terms, the external interface is provided to serve as a bridge between external applications and internal complex systems. External applications do not need to know the implementation details of the system. The appearance mode is like a cook in a restaurant. He knows how to cook braised pork ribs. We just need to check the recipes and order to eat at the restaurant!



Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace appearance {class program {// material class public class material required for cooking {// material required for braised pork ribs public void spareribs () {console. writeline ("prepare 500 grams of pork ribs, chopped green onion, ginger, soy sauce, peanut oil, sugar, vinegar, cooking wine, salt each amount... ");} // material required for braised fish public void fish () {console. writeline ("prepare 500 grams of fish, chopped green onion, ginger, soy sauce, peanut oil, sugar, vinegar, cooking wine, salt each amount .. ") ;}}// public class cure of pickled materials {// Pickled pork ribs public void spareribs () {console. writeline ("wash the ribs into a long segment of 3 cm, boil it with boiling water, pull it out and put it in the basin, and add salt and soy sauce to marinate it... ");} // pickled fish public void fish () {console. writeline ("Fish clean up, dry the skin, the surface of the flower knife, with salt and ginger marinate for 2-3 hours... ") ;}/// public class fry for frying materials {// Add public void spareribs () {console. writeline... ");} // public void fish () {console. writeline... ");}}// The final juice adjustment class public class sauce {// braised pork ribs juice adjustment public void spareribs () {console. writeline, boil it and cook it with a slow fire until the broth is thick and the ribs are ripe. Just go out of the pot! ");} // Public void fish () {console. writeline, after the big goods are burned, close the fire for 15-20 minutes, just go out of the pot! ") ;}}// Braised pork ribs appearance class public class spareribsfacade {private material = new material (); Private cure = new cure (); private fry = new fry (); Private Sauce sauce = new sauce (); Public void cookspareribs () {console. write ("Step 1:"); material. spareribs (); console. write ("Step 2:"); cure. spareribs (); console. write ("Step 3:"); Fry. spareribs (); console. write ("Step 4:"); sauce. spareribs () ;}/// braised fish appearance pub LIC class fishfacade {private material = new material (); Private cure = new cure (); Private fry = new fry (); Private sauce = new sauce (); public void cookfish () {console. write ("Step 1:"); material. fish (); console. write ("Step 2:"); cure. fish (); console. write ("Step 3:"); Fry. fish (); console. write ("Step 4:"); sauce. fish () ;}} static void main (string [] ARGs) {// The console is started for braised pork ribs. writeline ("---- Start braised pork ribs..."); spareribsfacade = new spareribsfacade (); spareribsfacade. cookspareribs (); console. writeline ("---- braised pork ribs is finished! "); Console. writeline (); // started to cook the braised fish console. writeline ("---- start to cook braised fish... "); fishfacade = new fishfacade (); fishfacade. cookfish (); console. writeline ("---- braised fish! "); Console. Read ();}}}







Appearance mode class diagram:


The class diagram contains the following roles:
Facade): A single window that forms a complex subsystem inside the system and provides a higher level of interface APIs to the outside of the system.
Subsystem (subsystem role): Various subsystems in the system perform their respective duties and do their jobs well ". Appearance roles do not have any impact on them. They follow the appearance role call, but do not call the appearance role in turn.
Client (client role): As the client role of an external application, it is not related to the operation of complex subsystems in the system, but only to the communication between appearance roles to obtain data and results.




Main advantages:
1. It shields sub-system components from the client, reduces the number of objects to be processed by the client, and makes the sub-system easier to use. By introducing the appearance mode, the client code becomes very simple and there are few objects associated with it.
2. It implements loose coupling between the subsystem and the client, so that the subsystem changes will not affect the client that calls it. You only need to adjust the appearance class.
3. Modifications to a sub-system have no impact on other sub-systems, and internal changes in the sub-system do not affect the appearance objects.

 


Main disadvantages:
1. The client's direct use of the sub-system class cannot be well limited. If too many restrictions are imposed on the client's access to the sub-system class, the variability and flexibility are reduced.
2. If the design is improper, adding a new subsystem may need to modify the source code of the appearance class, which violates the open and closed principle.

 


Applicable scenarios:
1. The appearance mode can be used to provide a simple entry for accessing a series of complex subsystems.
2. There is a large dependency between the client program and multiple subsystems. The introduction of appearance class can decouple the subsystem from the client, thus improving the independence and portability of the subsystem.
3. In a hierarchical structure, you can use the appearance mode to define the entrance of each layer in the system. There is no direct connection between the layer and the layer. Instead, you can establish a connection through the appearance class to reduce the Coupling Degree between the layers.



Related models:
1. Abstract Factory: abstract factory can be understood as the appearance mode related to the generation of complex objects, because a higher level of Calling Interface API is provided in the abstract factory for external applications to call to obtain object instances.
2. Singleton: The appearance is closely related to the singleton. Because of the general situation, the appearance is often designed to run as a singleton for external applications to call.
3. intermediary: The intermediary acts as the intermediary for each participant in the system and encapsulates the interaction between the participants. The intermediary makes it unnecessary for objects in the system to call each other explicitly, this decouples the participants of the system.









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.