In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares to you is about how to understand the appearance pattern in the example of NET design pattern, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
1. Introduction to appearance mode (Brief Introduction)
The appearance pattern, which provides a unified interface for a set of interfaces of the subsystem, defines a high-level interface that makes the subsystem easier to use.
2. Problem solved (What To Solve)
1. Separate two different layers
A typical example of layering is the three-tier architecture of Net, where the interface layer is separated from the business logic layer, and the business logic layer is classified from the data access layer. This can provide a unified interface and interface for the subsystem and reduce the coupling of the system.
2. Reduce dependence
With the increase of functions and the refactoring of the program, the system will become more and more complex, so adding an appearance can provide a simple interface and reduce the dependence between them.
3. Provide an interface for the interaction between new and old systems
Sometimes, the new system needs the core functions of the old system, but the old system has been difficult to maintain and expand, so you can add a Fa ç ade class to the new system, yes, the new system interacts with the Fa ç ade class, and the Fa ç ade class interacts with the old system.
3. Appearance pattern analysis (Analysis)
1. Appearance pattern structure
2. Source code
1. Subsystem class SubSystemOne
Public class SubSystemOne {public void MethodOne () {Console.WriteLine ("execute method One in subsystem One");}}
2. Subsystem class SubSystemTwo
Public class SubSystemTwo {public void MethodTwo () {Console.WriteLine ("execute method Two in subsystem Two");}}
3. Subsystem class SubSystemThree
Public class SubSystemThree {public void MethodThree () {Console.WriteLine ("execute method Three in subsystem Three");}}
4. Facade appearance class, which provides higher-level interface and consistent interface for the collection of subsystem classes.
Public class Facade {SubSystemOne one; SubSystemTwo two; SubSystemThree three; public Facade () {one = new SubSystemOne (); two = new SubSystemTwo (); three = new SubSystemThree ();} public void MethodA () {Console.WriteLine ("start executing method An in appearance mode"); one.MethodOne () Two.MethodTwo (); Console.WriteLine ("method An execution in appearance mode ends"); Console.WriteLine ("- -");} public void MethodB () {Console.WriteLine ("start method B in appearance mode"); two.MethodTwo () Three.MethodThree (); Console.WriteLine ("method B execution in appearance mode ends");}}
5. Client code
Static void Main (string [] args) {Facade facade = new Facade (); facade.MethodA (); facade.MethodB (); Console.Read ();}
3. The result of running the program
IV. Case study (Example)
1. Scene
Suppose the distance network education system-the user registration module includes functions such as
1. Verify that the course is full.
2. Collect customer fees
3. Notify users that the course selection is successful.
As shown in the following figure
The collection of subsystem classes includes: PaymentGateway class, RegisterCourse class, NotifyUser class.
PaymentGateway category: users pay for the course
RegisterCourse class: verify that the selected course is full and calculate the cost of the course
NotifyUser class: notify the user whether the course selected by the user is successful or not
RegistrationFacade class: appearance class, which provides a unified interface and interface to complete the functions of course verification, online payment and user notification.
2. Code
1. Collection of subsystem classes
Namespace FacadePattern {/ Subsystem for making financial transactions / public class PaymentGateway {public bool ChargeStudent (string studentName, int costTuition) {/ / Charge the student Console.WriteLine (String.Format ("Charging student {0} for ${1}", studentName, costTuition.ToString ()); return true }} / Subsystem for registration of courses / public class RegisterCourse {public bool CheckAvailability (string courseCode) {/ / Verify if the course is available.. Console.WriteLine (String.Format ("Verifying availability of seats for the course: {0}", courseCode)); return true;} public int GetTuitionCost (string courseCode) {/ / Get the cost of tuition return 1000 }} / Subsystem for Notifying users / public class NotifyUser {public bool Notify (string studentName) {/ / Get the name of the instructor based on Course Code / / Notify the instructor Console.WriteLine ("Notifying Instructor about new enrollment") Return true;}
2. Appearance class Fa ç ade Class
/ The Facade class that simplifies executing methods in the subsystems and hides implementation for the client / public class RegistrationFacade {private PaymentGateway _ paymentGateWay; private RegisterCourse _ registerCourse; private NotifyUser _ notifyUser; public RegistrationFacade () {_ paymentGateWay = new PaymentGateway (); _ registerCourse = new RegisterCourse (); _ notifyUser = new NotifyUser () } public bool RegisterStudent (string courseCode, string studentName) {/ / Step 1: Verify if there are available seats if (! _ registerCourse.CheckAvailability (courseCode)) return false; / / Step 2: Charge the student for tuition if (! _ paymentGateWay.ChargeStudent (studentName, _ registerCourse.GetTuitionCost (courseCode)) return false; / / Step 3: If everything's successful so far, notify the instructor of the new registration return _ notifyUser.Notify (studentName)
3. Client code
Namespace FacadePattern {class Program {static void Main (string [] args) {RegistrationFacade registrationFacade = new RegistrationFacade (); if (registrationFacade.RegisterStudent ("DesignPatterns101", "Jane Doe") Console.WriteLine ("Student Registration SUCCESSFUL!"); else Console.WriteLine ("Student Registration Unsuccessful");}
5. Summary (Summary)
The appearance pattern, which provides a unified interface for a set of interfaces of the subsystem, defines a high-level interface that makes the subsystem easier to use.
The appearance mode can solve the separation of layer structure, reduce the degree of system coupling and provide interface functions for the interaction between new and old systems.
The above is how to understand the appearance pattern in the example of NET design pattern. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.