Design Mode-Facade appearance Mode

Source: Internet
Author: User

Facade's external view mode is a structural mode. It mainly solves the problem of excessive coupling between the component's customers and various complicated subsystems in the component, with the evolution of external customer programs and subsystems, this excessive coupling is faced with many changing challenges. Here I would like to give an example: for example, if there is a car, we (customer program) want to start it, then we need to launch the engine (subsystem 1 ), turn four wheels (subsystem 2. But in reality, we don't need to push the wheel by hand to make it turn. We step on the accelerator, and then the car rotates the wheel according to some other operations. The accelerator is like the interface left by the system. No matter how the car rotates the wheel or the wheel changes to a certain brand, what we need to do to drive the car is to step on the accelerator.
GoF "Design Pattern" states that it provides a consistent interface for a group of interfaces in the subsystem. The Facade Pattern Defines a high-level interface, which makes the subsystem easier to use.
The structure of the Fa C ade appearance mode is roughly as follows:
 

This figure shows my understanding of the Facade mode. If you think something is wrong, please tell me.
I will write the implementation code in the above case. First, we need to implement three subsystems (Wheel, Engine, Body ):
 
Internal class Engine
{
Public string EngineWork ()
{
Return "BMW's Engine is Working ";
}
 
Public string EngineStop ()
{
Return "BMW's Engine is stoped ";
}
}
 
Internal class Wheel
{
Public string WheelCircumrotate ()
{
Return "BMW's Wheel is Circumrotating ";
}
Public string WheelStop ()
{
Return "BMW's Wheel is stoped ";
}
}
 
Internal class Body
{
Public Wheel [] wheels = new Wheel [4];
Public Engine engine = new Engine ();
Public Body ()
{
For (int I = 0; I <wheels. Length; I ++)
{
Wheels [I] = new Wheel ();
}
}
}
 
Then we can implement car Facade.
Class CarFacade
{
Body body = new Body ();
Public void Run ()
{
Console. WriteLine (body. engine. EngineWork ());
For (int I = 0; I <body. wheels. Length; I ++)
{
Console. WriteLine (body. wheels [I]. WheelCircumrotate ());
}
}
Public void Stop ()
{
Console. WriteLine (body. engine. EngineStop ());
For (int I = 0; I <body. wheels. Length; I ++)
{
Console. WriteLine (body. wheels [I]. WheelStop ());
}
}
}
 
Now let's use the client program for verification. The Code is as follows:
Class Program
{
Static void Main (string [] args)
{
CarFacade car = new CarFacade ();
Car. Run ();
Car. Stop ();
Console. Read ();
}
}

 
The execution result is as follows;
BMW's Engine is Working
BMW's Wheel is Circumrotating
BMW's Wheel is Circumrotating
BMW's Wheel is Circumrotating
BMW's Wheel is Circumrotating
BMW's Engine is stoped
BMW's Wheel is stoped
BMW's Wheel is stoped
BMW's Wheel is stoped
BMW's Wheel is stoped
 
 
As mentioned above: client code (Program) does not need to care about subsystems. It only needs to care about the interfaces left behind by CarFacade for external interaction, while subsystems are aggregated in CarFacade.
 
Key points of the Fa C ade mode:
1. From the perspective of the customer program, the Facade mode not only simplifies the interface of the entire component system, but also for the internal and external customer programs of the component, to some extent, it achieves a "decoupling" effect-any changes to the internal subsystem will not affect the changes to the Facade interface.
2. The Facade design model focuses more on the entire system from the architectural level, rather than the individual class level. In most cases, Facade is an architectural design model.

Related Article

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.