Façade mode facade
Façade mode: The interface between the system and the connection between the frames
(1) Using the form of interface to develop, do not need to know the interface of the implementation of internal methods;
(2) The façade mode is to provide a unified interface for the subsystem to provide a set of interfaces, so that other systems access to the system through this unified interface to complete.
(3) When a load system needs to provide interfaces externally, it is necessary to uniformly encapsulate the provided interface in the appearance class for the external system to use.
Façade mode schematic diagram
Façade mode schematic diagram
Situations without façade design patterns
Holiday.java
public class Holiday {public double getholiday (String empno) {return 2;}}
Salary.java
public class Salary {public double getsalary (String empno) {return 2000;}}
Tax.java
public class Tax {public double gettax (String empno) {return 0;}}
Client Client.java
public class Client {public static void main (string[] args) {Salary Salary = new Salary (); Holiday Holiday = new Holiday (); Tax tax = new tax ();d ouble money = salary.getsalary ("100222") * Holiday.getholiday ("100222")/30-tax.gettax ("100222"); SYSTEM.OUT.PRINTLN (Money);}}
the client will have a greater degree of contact with several classes; to minimize the client program, we extract the code block for calculating the salary into a class:
Salarycomputer.java
public class Salarycoumputer {public double dosalary () {Salary Salary = new Salary (); Holiday Holiday = new Holiday (); Tax tax = new tax ();d ouble money = salary.getsalary ("100222") * Holiday.getholiday ("100222")/30-tax.gettax ("100222"); re Turn Money;}}
The client program becomes:
public class Client {public static void main (string[] args) {Salarycoumputer salarycoumputer = new Salarycoumputer (); System.out.println (Salarycoumputer.dosalary ());}}
If there is more than one calculation of salary, it is not appropriate, so you can then change the salary of the class to an interface
Computer.java
Public interface Computer {public double dosalary (String empno);
Salarycomputer.java
public class Salarycoumputer implements Computer{public double Dosalary (String empno) {Salary Salary = new Salary (); Holiday Holiday = new Holiday (); Tax tax = new tax ();d ouble money = salary.getsalary ("100222") * Holiday.getholiday ("100222")/30-tax.gettax ("100222"); re Turn Money;}}
A class that specifically reads the Properties property file
public class Propertiesutil {static Properties p = new properties ();p ublic static Object getinstance () {InputStream in = n Ull;object obj = null;try {in = PropertiesUtil.class.getClassLoader (). getResourceAsStream ("Class.properties");p. Load (in);} catch (IOException e) {e.printstacktrace ();} finally{try {In.close ();} catch (IOException e) {e.printstacktrace ()}} try {obj = (Object) class.forname ((String) propertiesutil.getvalue ("Class")). newinstance ();} catch ( Instantiationexception e) {e.printstacktrace (),} catch (Illegalaccessexception e) {e.printstacktrace ();} catch ( ClassNotFoundException e) {e.printstacktrace ();} return obj;} public static Object GetValue (String key) {return p.get (key);}}
Client.java
public class Client {public static void main (string[] args) {Computer computer = (computer) propertiesutil.getinstance (); System.out.println (Computer.dosalary ("100222"));}}
applications such as: the encapsulation of the SPRINGJDBC of the JDBC package.
use of the environment and benefits
"design mode" gives the use of the façade mode environment :
(1) When you want to provide a simple interface for a complex subsystem. The reason has been described above.
(2) There is a great dependence between the client program and the implementation part of the abstract class. The introduction of facade separates this subsystem from the client and other subsystems, which can improve the independence and portability of the subsystem (mentioned above).
(3) When you need to build a hierarchical subsystem, use the facade pattern to define the entry points for each layer in the subsystem. If subsystems are interdependent, you can make them communicate only through facade, simplifying their dependencies.
Advantages :
(1) It shields the subsystem components from the customer, thus reducing the number of objects processed by the customer and making the subsystem more convenient to use.
(2) It realizes the loose coupling between the subsystem and the customer, and the functional components inside the subsystem are often tightly coupled. Loosely coupled relationships make the components of a subsystem change without affecting its customers. The facade pattern helps to build a hierarchical system and also helps to layer dependencies between objects. The facade pattern eliminates complex cyclic dependencies. This is especially important when the client program and subsystem are implemented separately. Reducing compilation dependencies in large software systems is critical. When the subsystem class changes, you want to minimize the recompilation effort to save time. Using facade can reduce compilation dependencies and limit the recompilation required for minor changes in important systems. The facade model also facilitates the simplification of the porting process between different platforms, since compiling a subsystem generally does not require compiling all of the other subsystems.
(3) If the application requires it, it does not restrict their use of subsystem classes. So you can choose between ease-of-use and versatility for your client program.
Design pattern (vii) façade mode (facade)-Structural type