A new solution to the façade pattern

Source: Internet
Author: User
first, façade (façade) mode

External communication with a subsystem must be done through a unified façade (façade) object, which is the façade pattern.

Examples of Hospitals

With an example, if the hospital as a subsystem, according to departmental functions, this system can be divided into registered, outpatient, pricing, laboratory, charge, take medicine and so on. It is not easy for a patient to work with these departments, just as a subsystem client deals with the various classes of a subsystem.

First, the patient must be registered first and then outpatient. If a doctor asks for a test, the patient must first pricing and then make a payment before he can go to the laboratory for testing. After the test, go back to the clinic room.

The way to solve this inconvenience is to introduce a façade model. can set up a receptionist's position, by the receptionist is responsible for registration, pricing, payment, take medicine and so on. The receptionist is the embodiment of the façade pattern, with the patient only contacting the receptionist, who is responsible for dealing with the various departments of the hospital.

What is the façade mode

The façade mode requires that the external and internal communication of a subsystem must be performed through a unified façade (façade) object. The façade mode provides a high-level interface that makes the subsystem easier to use.

Like a hospital receptionist, the façade class separates the client from the internal complexity of the subsystem, allowing the client to deal only with façade objects without having to deal with many objects within the subsystem.
Second, the structure of the façade model

The façade mode is the structure pattern of the object. The façade model does not have a generalized class diagram description, and the following illustration shows a schematic object graph of a façade pattern:

In this object graph, there are two roles:

Façade (façade) Role: A method by which a client can invoke this role. This role is aware of the functions and responsibilities of the related (one or more) subsystems. Normally, this role delegates all requests sent from the client to the appropriate subsystem.

subsystem (SUBSYSTEM) Role: You can have one or more subsystems at the same time. Each subsystem is not a separate class, but rather a collection of classes. Each subsystem can be called directly by the client or by a façade role. Subsystems do not know the presence of the façade, for the subsystem, the façade is just another client.
Third, the realization of the façade mode

a system can have several façade classes

"GOF" in the book pointed out: in the façade mode, usually only need a façade class, and this façade class has only one instance, in other words, it is a singleton class. This does not mean, of course, that there can be only one façade class in the system, but only one façade for each subsystem. Or, if a system has several subsystems, each subsystem has a façade class, the entire system can have several façade classes.

adding new behavior for subsystems

Beginners often think that it is wrong to add new behavior to a subsystem by inheriting a façade class. The façade model is intended to provide a centralized and simplified communication channel for subsystems and not to add new behavior to subsystems.
Four, under what circumstances use the façade mode
to provide a simple interface for a complex subsystem to improve the independence of subsystems in a hierarchical structure, you can use the façade pattern to define the entry of each layer in the system.
How far example

We examine an example of a security system to illustrate the effectiveness of the façade model. A security system consists of two VCRs, three lights, a remote sensor and a siren. The operation of the security system requires that these instruments be regularly activated and closed.

design without the use of façade mode

First, in the case of not using the façade mode, the operator operating the security system must directly operate all of these components. The diagram below shows the design of the system without using a façade pattern.

As you can see, client objects need to be referenced to all recorders (Camera), Lights (Light), sensors (Sensor), and Siren (Alarm) objects. The code is as follows:

Using System;

public class Camera
{
public void Turnon ()
{
Console.WriteLine ("Turning on the Camera");
}

public void Turnoff ()
{
Console.WriteLine ("Turning off the camera");
}

public void Rotate (int degrees)
{
Console.WriteLine ("Rotating the camera by {0} degrees.", degrees);
}
}

public class Light
{

public void Turnoff ()
{
Console.WriteLine ("Turning on the Light");
}

public void Turnon ()
{
Console.WriteLine ("Turning Off the Light");
}

public void Changebulb ()
{
Console.WriteLine ("Changing the light-bulb.");
}
}

public class Sensor
{
public void Activate ()
{
Console.WriteLine ("Activating the sensor.");
}

public void Deactivate ()
{
Console.WriteLine ("Deactivating the sensor.");
}

public void Trigger ()
{
Console.WriteLine ("The sensor has triggered.");
}
}

public class Alarm
{

public void Activate ()
{
Console.WriteLine ("Activating the alarm.");
}

public void Deactivate ()
{
Console.WriteLine ("Deactivating the alarm.");
}

public void Ring ()
{
Console.WriteLine ("Ringing the alarm.");
}

public void stopring ()
{
Console.WriteLine ("Stop the alarm.");
}
}

public class Client
{
private static Camera camera1, Camera2;
private static Light light1, Light2, LIGHT3;
private static Sensor Sensor;
private static Alarm Alarm;

Static Client ()
{
CAMERA1 = new Camera ();
Camera2 = new Camera ();
light1 = new Light ();
Light2 = new Light ();
LIGHT3 = new Light ();
Sensor = new sensor ();
Alarm = new alarm ();
}

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.