Design Mode note (11)-appearance mode (Structural)

Source: Internet
Author: User
Document directory
  • Gof Definition
  • Motivation
  • Several highlights of the facade Mode
Gof Definition

Provides a consistent interface for a group of interfaces in the subsystem. The Fa-ade mode defines a high-level interface, which makes the subsystem easier to use.

Let's look at a small example. Suppose we need to develop a tank simulation system to simulate the behavior of tank vehicles in various combat environments. The tank system consists of engines, controllers, wheels, bodies, and other subsystems. The following classes are generated:

public class Wheel{ }public class Engine{ }public class Controller{ }public class BodyWork{ }

Different scenarios have different requirements. Some subsystems may or may not be used. These different scenarios are equivalent to external interfaces. The relationships between these scenarios and subsystems are as follows:

Figure 1

The relationship in is messy, and the coupling between the scenario and the subsystem is very high. To reduce the coupling, it is necessary to make the interaction with the scenario different from those subsystems, but a relatively single middle layer, such:

Figure 2

The facade of will hide the subsystem, and different scenarios are directly interacting with the facade.

Motivation

The problem with the solution in figure 1 above is that the component customers and various complicated subsystems in the component have too many coupling. With the evolution of external customer programs and subsystems, this excessive coupling is faced with many changing challenges. How to simplify the interaction interfaces between external client programs and systems? How can we decouple the dependency between the evolution of external customer programs and the changes of internal subsystems? This requires the facade mode. First, let's look at the structure diagram:

Code implementation: first define some sub-system classes

public class Wheel{    public void WAction1() { }    public void WAction2() { }}public class Engine{    public void EAction1() { }    public void EAction2() { }}public class Controller{    public void CAction1() { }    public void CAction2() { }}public class BodyWork{    public void BAction1() { }    public void BAction2() { }}

Facade class used to combine these subsystems

Public class tankfacade {wheel [] Wheels = new wheel [4]; engine [] engines = new engine [4]; bodywork = new bodywork (); controller controller = new controller (); Public void start () {// use one or more of the subsystems} public void stop () {// use one or more} public void run () {// use one or more} public void shot () in the subsystem () {// use one or more subsystems }}

Client call

Public class app {static void main () {tankfacade facade = new tankfacade (); // you can choose to call the corresponding method based on the needs of different scenarios. // you can use facade as needed by the customer, you do not need to know the implementation of the sub-system. // The Sub-system is decoupled from the sub-system. facade. start (); facade. run (); facade. shot (); facade. stop ();}}
Several highlights of the facade Mode

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 program of the component, to some extent, it achieves a "decoupling" effect-any changes to the internal subsystem will not affect the changes to the fa c Ade interface.

The fa c ade design model focuses more on the entire system from the architectural level, rather than the individual class level. Fa C Ade is often an architectural design model.

Be sure to differentiate the fa c Ade mode, adapter mode, bridge mode, and decorator mode. The FA-ade mode focuses on simplified interfaces, the adapter mode focuses on the conversion interfaces, and the bridge mode focuses on the separation of interfaces (abstraction) and their implementation. The decorator mode focuses on the object extension function on the premise of a stable interface.

Return to the beginning (INDEX)

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.