Design Mode (13) --- appearance mode, design mode 13 --- appearance

Source: Internet
Author: User

Design Mode (13) --- appearance mode, design mode 13 --- appearance

I. Definition

Appearance mode: 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.

Explanation: In simple terms, the client needs to call multiple interfaces in a particularly complex subsystem. It is very complicated to call the logic directly, and it is not easy to expand the system. The appearance mode unifies this complex subsystem and provides several high-level interfaces for the client to call. In general, the subsystem is a black box that provides several transparent interfaces for calling.

 

Ii. UML class diagram and basic code

Basic code:

Class Program {static void Main (string [] args) {Facade facade = new Facade (); facade. methodA (); facade. methodB (); Console. read () ;}} class Facade {SubSystemOne systemOne; SubSystemTwo systemTwo; SubSystemThree systemThree; public Facade () {systemOne = new SubSystemOne (); systemTwo = new SubSystemTwo (); systemThree = new SubSystemThree ();} public void MethodA () {Console. writeLine ("\ n method group A () ---"); systemOne. methodOne (); systemThree. methodThree ();} public void MethodB () {Console. writeLine ("\ n method group B () ---"); systemOne. methodOne (); systemTwo. methodTwo () ;}} class SubSystemOne {public void MethodOne () {Console. writeLine ("Subsystem Method 1") ;}} class SubSystemTwo {public void MethodTwo () {Console. writeLine ("Subsystem Method 2");} class SubSystemThree {public void MethodThree () {Console. writeLine ("Subsystem Method 3 ");}}View Code

 

Iii. Examples

A school course selection system includes a registration course subsystem and a notification system. Under normal circumstances, you need to call the registration course system and notification system one by one. If the appearance mode is used, the registration course system and notification system are packaged to provide a unified interface for students to call. The Code is as follows:

Class Program {private static RegistrationFacade facade = new RegistrationFacade (); static void Main (string [] args) {if (facade. registerCourse ("design mode", "studentA") {Console. writeLine ("course selected");} else {Console. writeLine ("Course Selection failed");} Console. read () ;}} public class RegistrationFacade {private RegisterCourse registerCourse; private policystudent; public RegistrationFacade () {register Course = new RegisterCourse (); policystudent = new notifyStudent ();} public bool RegisterCourse (string courseName, string studentName) {if (! RegisterCourse. checkAvailable (courseName) {return false;} return policystudent. required y (studentName) ;}} public class RegisterCourse {public bool CheckAvailable (string courseName) {Console. writeLine ("verifying that the course {0} is full", courseName); return true ;}} public class policystudent {public bool Policy (string studentName) {Console. writeLine ("sending notification to {0}", studentName); return true ;}}

 

Iv. Advantages and Disadvantages and Use Cases

Advantages:

1) The appearance mode shields sub-system components from the client, simplifies the interface, reduces the number of objects processed by the client, and simplifies the use of sub-systems.

2) The appearance mode achieves loose coupling between the subsystem and the customer, while the functional components inside the subsystem are tightly coupled. Loose coupling makes the Component Changes of the subsystem do not affect its clients.

Disadvantages:

To add a new subsystem, you may need to modify the appearance class or client, which violates the "Open and Close principle ".

 

Use Cases:

1) provides a simple interface for a complex subsystem.

2) in a hierarchical structure, you can use the appearance mode to define the entries for each layer of the system.

3) subsystems must be independent.

Related Article

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.