ArticleDirectory
Dr. Yan Hong's book "Java and patterns" describes the facade mode in this way:
The facade mode is the object structure mode. External communication with a sub-system must be performed through a unified facade object. The facade mode provides a high-level interface to make subsystems easier to use.
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:
●Facade role:The client can call this role method. This role is aware of the functions and responsibilities of one or more subsystems. Under normal circumstances, this role delegates all requests sent from the client to the corresponding subsystem.
●Subsystem role:You can have one or more subsystems at the same time. Every subsystem is not a separate class, but a collection of classes.(The subsystems above are composed of modulea, moduleb, and modulec). Each subsystem can be called directly by the client or by the facade role. The subsystem does not know the existence of the facade. For the subsystem, the facade is just another client.
Source code
Classes in subsystem roles:
Public class modulea {
/// schematic method
Public void