Facade mode of JAVA Design Mode

Source: Internet
Author: User

Hospital example

Modern software systems are complex. A common method for designers to process complex systems is to divide them into several smaller subsystems. If a hospital is used as a sub-system, the system can be divided into registration, outpatient service, price assignment, testing, billing, and drug taking according to the Department's functions. It is not easy for a patient to deal with these departments, just as the client of a sub-system deals with various categories of a sub-system.

First, the patient must register first and then go to the clinic. If the doctor asks for a test, the patient must first make a price and then pay the fee before going to the test department for a test. After the test, return to the clinic.

Describes the patient experience in the hospital. The box in the figure represents the hospital.

The method to solve this inconvenience is to introduce the facade mode. The hospital can set up a receptionist's location, where the receptionist is responsible for registration, price calculation, payment, and medicine. This receptionist is the embodiment of the facade model. The patient only contacts the receptionist, who deals with various departments.

Facade mode structure

The facade mode does not have a general class graph description. The best description method is actually an example.

Because the structure of the facade mode is too abstract, it is slightly specific. Assume that the subsystem has three modules: ModuleA, ModuleB, and ModuleC. They have an example method. The overall structure of the example is as follows:

In this object diagram, two roles are displayed: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release/Nu6e2y7/release/2z8KjrLG + vcfJq7vhvavL + dPQtNO/release/FybW9z + release + 1zbO + release + release + xu8PFw + release + 1LS0 + sLrPC9oMz4KPHA + oaghodftz7xns73hyavw0lxewocjuw.vcd4kphbyzsbjbgfzcz0 = "brush: java; "> public class ModuleA {// specifies the public void testA () {System. out. println ("Call testA method in ModuleA ");}}

Public class ModuleB {// indicates the public void testB () {System. out. println ("Call the testB method in ModuleB ");}}


Public class ModuleC {// indicates the public void testC () {System. out. println ("Call the testC method in ModuleC ");}}


Corner class:

Public class Facade {// schematic method, which provides the public void test () {ModuleA a = new ModuleA ();. testA (); ModuleB B = new ModuleB (); B. testB (); ModuleC c = new ModuleC (); c. testC ();}}

Client angle:

public class Client {    public static void main(String[] args) {                Facade facade = new Facade();        facade.test();    }}

The Facade class is actually equivalent to the appearance interface of the, B, and C modules. With this Facade class, the client does not need to call the, B, and C modules of the subsystem, you do not need to know the implementation details inside the system, or even the existence of modules A, B, and C. The client only needs to interact with the Facade class, in this way, the decoupling between the client and sub-system modules A, B, and C is better implemented, making the client easier to use the system.

Facade mode implementation

Another advantage of using the facade mode is that the method can be selectively exposed. The methods defined in a module can be divided into two parts, one for external use of the subsystem, and the other for mutual calls between modules of the subsystem. With the Facade class, the methods used to call each other between internal modules of the subsystem do not need to be exposed to the external modules of the subsystem.

For example, the following modules are defined: A, B, and C.

Public class Module {/*** provides a method for external use of the subsystem */public void a1 (){}; /*** method used for inter-subsystem calls */public void a2 () {}; public void a3 (){};}


Public class ModuleB {/*** method provided for external use of the subsystem */public void b1 (){}; /*** method used for inter-subsystem calls */public void b2 () {}; public void b3 (){};}


Public class ModuleC {/*** method provided for external use of the subsystem */public void c1 (){}; /*** method used for inter-system calls */public void c2 () {}; public void c3 (){};}


Public class ModuleFacade {ModuleA a = new ModuleA (); ModuleB B = new ModuleB (); ModuleC c = new ModuleC (); /*** the following are the methods provided by the, B, and C modules to the external system */public void a1 () {. a1 ();} public void b1 () {B. b1 ();} public void c1 () {c. c1 ();}}

Defining a ModuleFacade class effectively shields internal details, so that the client does not need to know some methods when calling the Module class. For example, the a2 () and a3 () methods do not need to be known to the client. Otherwise, internal details are exposed, and the client is confused. For the client, he may have to think about what the a2 () and a3 () methods are used? In fact, the a2 () and a3 () Methods interact with internal modules. Originally, they are not external to the subsystem, so do not let the client know.

A system can have several portals.

In facade mode, only one facade class is required, and this facade class has only one instance. In other words, it is a singleton class. Of course, this does not mean that there is only one portal class in the entire system, but only one portal class for each subsystem. In other words, if a system has several subsystems, each subsystem has a portal class, and the entire system can have several portals.

Add new behaviors to subsystems

Beginners often think that they can add new behaviors to the subsystem by inheriting a facade class. This is wrong. The facade model is intended to provide a centralized and simplified communication channel for the subsystem, rather than adding new behaviors to the subsystem. For example, the receptionist in the hospital is not a medical specialist, and the receptionist cannot provide medical services for the patient.

Advantages of Facade Mode

Advantages of the facade mode:

● Loose coupling

  The facade mode loose the coupling relationship between the client and subsystem, making it easier to expand and maintain modules within the subsystem.

  ● Easy to use

  The facade mode makes the subsystem easier to use. The client no longer needs to understand the internal implementation of the subsystem, nor need to interact with the internal modules of many subsystems. It only needs to interact with the portal class.

  ● Better access levels

  The rational use of Facade can help us better classify access layers. Some methods are external to the system, and some methods are used inside the system. The functions that need to be exposed to the external are concentrated in the facade, which facilitates the use of the client and hides the internal details.


Author: jason0539

Weibo: http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)

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.