JAVA design mode (22)-appearance (façade) mode __java

Source: Internet
Author: User

Definition: provides a consistent interface for a set of interfaces in a subsystem, and the façade pattern defines a high-level interface that makes the subsystem easier to use.

Type: Object-structured pattern

class Diagram:



structure of the façade pattern

appearance Role (façade): is the core of the pattern and is invoked by the client client role. It knows those subsystem classes are responsible for processing the request and forwarding the customer's request to the appropriate subsystem object. subsystem Role (subsystem classes): Implements subsystem functions and handles tasks assigned by a façade object. For subsystems, the façade and client roles are unknown and there is no relevant information about the façade, that is, there is no instance pointing to the façade. customer role (client): invokes the façade role to achieve the appropriate functionality.
The collaboration client communicates with the subsystem by sending requests to the façade, and the façade forwards the messages to the appropriate subsystem object. Although the object in the subsystem is actually working, the façade pattern itself must transform its interface into a subsystem interface. Client programs that use facade mode do not need to directly access subsystem objects.
Code implementation

Abstract class façade
abstract class facade{public
	abstract void compile ();
	public abstract void execute ();
	
}

The specific Realization
class Concretefacade extends facade{
	private Subsystema SubA;
	Private Subsystemb Subb;
	
	@Override public
	Void execute () {
		subb.run ();
	}

	@Override public
	Void Compile () {
		suba.complie ();
	}

	public void Setsuba (Subsystema subA) {
		This.suba = SubA;
	}

	public void Setsubb (Subsystemb subb) {
		This.subb = Subb;
	}
	
}

subsystem 
class subsystema{public
	
	void Complie () {	//mock compilation
		System.out.println ("Complie");
	}
}

Class subsystemb{public
	
	void Run () {	//Impersonation Execution
		System.out.println ("Run");
	}

Client public
class Facadeclient {

	/**
	 * @param args
	/public static void Main (string[) args) {
		
		Concretefacade façade = new Concretefacade ();
		Facade.setsuba (New Subsystema ());
		Facade.setsubb (New Subsystemb ());
		
		Facade.compile ();
		Facade.execute ();
		
	}


Facade Mode Benefits:
The Customer Shield subsystem component reduces the number of objects processed by the client and makes it easier to use the subsystem. By introducing a skin pattern, the client code becomes simple, with few objects associated with it. The loose coupling relationship between subsystem and customer is realized, which makes the component change of subsystem not affect the client class that calls it, only need to adjust the appearance class. Reduced compilation dependencies in large software systems and simplified porting of systems across platforms because compiling a subsystem does not normally require compiling all other subsystems. The modification of one subsystem has no effect on other subsystems, and the internal changes of the subsystem will not affect the appearance objects. It simply provides a unified portal to the access subsystem and does not affect the user's use of subsystem classes directly. disadvantages of the façade model
It is not a good way to limit the use of subsystem classes, and if you do too many restrictions on customer access subsystem classes, you reduce variability and flexibility. Without introducing an abstract appearance class, adding a new subsystem may require modifying the appearance class or the source code of the client, violating the open and closed principle.


Applicable Scenarios
Use the façade pattern when you are experiencing the following situations:
When you want to provide a simple interface for a complex subsystem. Subsystems tend to become more complex as they evolve. Most patterns will produce more and smaller classes when used. This makes subsystems more reusable and easier to customize for subsystems, but it also brings some usability difficulties for users who do not need a custom subsystem. The façade can provide a simple default view,
This view is sufficient for most users, and users who need more customization can cross the façade layer.
There is a large dependency between the client program and the implementation part of the abstract class. The introduction of a façade separates the subsystem from the customer and other subsystems, which can improve the subsystem's independence and portability. When you need to build a hierarchical subsystem, use the façade pattern to define the entry points for each layer in the subsystem. If subsystems are interdependent, you can simplify their dependencies by allowing them to communicate only through a façade.


schema Extension
A system has multiple appearance classes:

In skin mode, you typically only need one appearance class, and this skin class has only one instance, in other words, it is a singleton class. In many cases, in order to conserve system resources, the appearance class is generally designed as a single case class. This does not mean, of course, that there can be only one appearance class in the system, that multiple appearance classes can be designed in one system and that each appearance class is responsible for interacting with specific subsystems to provide the user with the appropriate business functions.
Do not attempt to add new behavior to the subsystem through the Appearance class:
It is wrong to not add new behavior to the subsystem by inheriting a skin class. The intention of the appearance pattern is to provide a centralized and simplified communication channel for the subsystem instead of adding new behavior to the subsystem, which should be implemented by modifying the original subsystem class or by adding a new subsystem class, which cannot be implemented through the appearance class.
Appearance pattern and Dimitri law:
The appearance pattern creates an appearance object, minimizes the number of collaborative partners that are involved in a subsystem by the client, and causes the interaction of the client and the objects within the subsystem to be replaced by the Appearance object. The appearance class acts as the "third party" between the customer class and the subsystem class, reduces the coupling between the customer class and the subsystem class, and the appearance pattern is a powerful weapon that realizes the code refactoring to achieve the "Dimitri Law" requirement.
Introduction of abstract appearance classes:
The biggest disadvantage of the appearance pattern is that it violates the "open and close" principle,
When you add a new subsystem or remove a subsystem, you need to modify the appearance class, and you can solve the problem to some extent by introducing an abstract appearance class, which is programmed by the client for the abstract appearance class. For new business requirements, do not modify the original appearance class, and corresponding to add a new specific appearance class, by the new concrete appearance class to associate the new subsystem object, while modifying the configuration file to achieve the purpose of not modifying the source code and replacing the appearance class.







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.