Design mode appearance mode (facade)

Source: Internet
Author: User
Design mode appearance mode (facade)

1. Initial knowledge of appearance mode

This time, let's take a look at the definition of the appearance pattern:

Appearance mode: Provides a unified interface for accessing a group of interfaces in a subsystem. The appearance defines a high-level interface that makes the subsystem easier to use.

In the software development system, the client program often generates coupling with the internal subsystems of the complex system, which causes the client program to change with the subsystem. So how to simplify the interface between client program and subsystem. How to decouple the dependencies between the internal subsystem of a complex system and the client program. This is what the appearance pattern does, and we define a high-level interface through a façade class that contains the interfaces in the subsystem so that the client only needs to access the various subsystems through the appearance class. The structure diagram is represented as follows:

Take a look at this example: in life, people are more or less playing stocks, then you know what the difference between stock and fund? The risk of stock is big, the profit is high, but the average shareholder does not have the professional knowledge, the possibility of profit is lower. Then there was a group of people who started to buy funds because the fund was less risky and profitable than bank interest rates. So do you know why. How does the fund work behind it? In fact: Our customers buy funds, money to them, they take the money to buy stocks, buy bonds and so on to carry out money to make money activities, in contrast, they have a professional team, the possibility of more profitable, so the fund has the meaning of existence. Comparing the definition of the appearance pattern, it is not found that this and the appearance of the pattern is extremely consistent. The fund is the appearance ah. Let's see how we can do the code for applying the look pattern. (Examples refer to "Big talk design Mode")

2. Appearance mode realization

The code for the appearance pattern is as follows:

 1//stock 1 Class 2 class Stock1 3 {4 public:5 void Sell ();
 6 void Buy ();
 7};
8//Stock 2 Class 9 class Stock2 {one public:12 void Sell (); void Buy (); 14};
15//National Debt 1 class NationalDebt1 public:19 void Sell (); void Buy (); 21}; 22//Appearance class Fundfacade (Stock1 *stock1), *stock2 NationalDebt1; Ublic:29 Fundfacade () {stock1 = new Stock1 (); stock2 = new Stock2 (); Nationa
L_DEBT1 = new NationalDebt1 ();     BuyMethod1 () () () () (stock1->buy) (); National_debt1->buy (); 39} 40 void BuyMethod2 (), Stock2->buy (); National_debt1->buy ();

Through the appearance mode the client only needs to access the members in the Fundfacade class, and it does not need to be associated with each specific Stock1,Stock2 , and NationalDebt1 . This greatly reduces the coupling, but also makes the system easy to maintain and easy to expand. If you need to expand the stock class, you only need to add the stock class; If you have a new purchase plan, just add a buymethod method to the Fundfacade class.

3. Occasions and benefits of using the appearance mode

(1) in the initial stage of the design, should be aware of the different two layers, such as the classic three-tier architecture, you need to consider the data access layer, business logic layer and presentation layer between the layer and layer to establish the appearance, this can provide a simple interface for the complex subsystem, so that the coupling greatly reduced.

(2) In the development stage, subsystems tend to become more complex because of the evolution of the continuous reconstruction, most of the patterns used will produce a lot of small classes, this is good, but also to the external call their user program brings difficulties in the use of, increase the appearance can provide a simple interface, reduce their dependence.

(3) When maintaining a legacy large system, it may be very difficult to maintain and expand the system, but because it contains very important features, the new system development must rely on it, you can also apply the appearance mode. You can develop a skin class for the new system that provides a clearer, simpler interface to the rough and highly complex legacy code, allowing the new system to interact with the skin class object, and the façade class is responsible for all the complex work that interacts with the legacy code. This is divided into two groups, one team to develop the appearance of the interaction with the old system, the other team just understand the interface of the appearance class, directly develop new systems to call these interfaces.

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.