1. Overview
Provides a consistent interface for a set of interfaces in a subsystem that defines a high-level interface that makes this subsystem easier to use.
2. Role in the pattern
2.1 Appearance Class (façade): The Appearance class knows which subsystem classes are responsible for processing requests and proxies the customer's request to the appropriate subsystem object.
2.2 Subsystem class set (Subsystem Classes): The subsystem class set implements the subsystem function, handles the task which the appearance class object assigns.
3. Pattern Interpretation
3.1 class diagram of the appearance pattern
3.2 Code implementation of the appearance pattern
<summary>///subsystem A class///</summary> public class Subsystemone {public void Methodeone () {
Console.WriteLine ("Sub System A.");
}}///<summary>///subsystem A class///</summary> public class Subsystemtwo {public void Methodtwo () {
Console.WriteLine ("Sub System second method.");
}}///<summary>///subsystem A class///</summary> public class Subsystemthree {public void Methodthree ()
{Console.WriteLine ("Sub System third method.");
}}///<summary>///subsystem A class///</summary> public class Subsystemfour {public void Methodfour ()
{Console.WriteLine ("Sub System Fourth method.");
}///<summary>///appearance class///</summary> public class Façade {private Subsystemone one;
Private Subsystemtwo two;
Private Subsystemthree three;
Private Subsystemfour four;
Public façade () {one = new Subsystemone ();
two = new Subsystemtwo (); three = new SubSystemthree ();
four = new Subsystemfour ();
public void MethodA () {Console.WriteLine ("\nmethod Group A----"); One.
Methodeone (); Two.
Methodtwo (); Four.
Methodfour ();
public void MethodB () {Console.WriteLine ("\nmethod Group B----"); Two.
Methodtwo (); Three.
Methodthree ();
}
}
3.3 Client Code
Class program
{
static void Main (string[] args)
{
//Because of the façade, the client may not know the existence of the subsystem class at all.
facade façade = new façade ();
Façade. MethodA ();
Façade. MethodB ();
Console.read ();
}
Run results
4. Model Summary
4.1 Advantages
The 4.1.1 facade pattern reduces the complexity of the client's use of the subsystem.
The 4.1.2 appearance mode is loosely coupled with the subsystem, allowing the modules within the subsystem to be more easily extended and maintained.
4.1.3 can help us to better classify the level of access by using a façade properly.
4.2 Disadvantages
Too much or less reasonable façade is also easy to confuse, in the end is to call the façade good, or directly call the module good.
4.3 Applicable scenarios
4.3.1 need to consider the façade pattern when layering the design.
4.3.2 in the development phase, subsystems tend to become more complex because of refactoring, and increasing the appearance pattern can provide a simple interface to reduce dependencies between them.
4.3.3 in maintaining a legacy large system, this system is already very difficult to maintain and expand, can develop a façade class for the new system, to provide the design of rough or highly complex legacy code of a relatively clear simple interface, so that the new system and the façade object interaction, The façade interacts with the legacy code for all the complex work.
5. Application example: in layered development, we add dataaccess as an external interface to the data access layer to manipulate the database subsystem.
5.1 Implementation Class diagram
5.2 Implementation Code
public class Employee {public string Name {get; set;}
public int Age {get; set;}
Public Salary Salary {get; set;}
public class Salary {public DateTime from {get; set;}
Public DateTime to {get; set;}
Public decimal Amount {get; set;} public class Employeedataaccess {public void Saveemployee (employee employee) {Console.WriteLine ("Save employ
EE to database. ");}
public void Deleteemployee (employee employee) {Console.WriteLine ("Remode Employee from Database."); } public class Salarydataaccess {public void savesalary (Salary Salary) {Console.WriteLine (' Save Salary to D
Atabase. ");}
public void Deletesalary (Salary Salary) {Console.WriteLine ("Remove Salary from Database."); }///<summary>///DataAccess provides a simple interface for clients///</summary> public class DataAccess {private employ
Eedataaccess employeedataaccess = new employeedataaccess (); Private salarydataaccess salarydataaccess = new SAlarydataaccess ();
public void Saveemployee (employee employee) {//First save employee basic information Employeedataaccess.saveemployee (employee); Save Employee Salary Information Salarydataaccess.savesalary (employee.
Salary); public void Removeemployee (employee employee) {//Delete employee Salary Information Salarydataaccess.deletesalary (employee).
Salary);
Delete Employee basic information Employeedataaccess.deleteemployee (employee);
}
}
5.3 Client Code
Class program
{
static void Main (string[] args)
{
dataaccess.dataaccess dataaccess = new Dataaccess.dataaccess ();
Dataaccess.employee Employee = new Dataaccess.employee () {Salary = new dataaccess.salary (), Name = "Wang Kevin", age = 22 };
Dataaccess.saveemployee (employee);
Dataaccess.removeemployee (employee);
Console.read ();
}
Run results
The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.