Designpatterns Study Notes: C + + language implementation---2.6 facade

Source: Internet
Author: User

Designpatterns Study Notes: C + + language implementation---2.6 facade

2016-07-22

(WWW.CNBLOGS.COM/ICMZN)

Pattern understanding


1. Façade mode facade, also known as appearance mode
All external and one subsystems must be required to communicate through a "unified object". This object is the "façade" of the subsystem, that is, the façade provides a unified calling interface, making
The subsystem is simple to use.
The façade pattern focuses on "uniform objects", except for this unified object, which does not allow the behavior of other calls to subsystems to occur. A subsystem can be a collection of class objects or a set of objects, a more intuitive
No matter how disorganized the subsystem is, as long as the "unified object" is simple and tidy, that is, "respectability veneer" effect.
General two characters:
(1) Facade façade object
The role has no actual business logic, just a delegate class. It must be noted that the façade method does not participate in the business logic, otherwise it generates an inverted dependency problem, and the subsystem must rely on the façade to be accessed.
This violates the single principle of the interface. In the system, the façade pattern should be stable, should not change frequently, should send all possible changes in the operation encapsulated in the subclass, no matter what,
From the outside view is a simple façade.
(2) Collection of Subsystem subsystem objects
2. Advantages of Facade mode


3. Facade Practical Scenario
When a system is more complex, a good encapsulation can be achieved through the façade mode. If the algorithm or business is more complex, it can encapsulate a plurality of façade, simple structure and good extensibility.


4. Facade Discussion and expansion

Program Implementation (c + +)

Fecade.h

1 #pragmaOnce2#include <iostream>3 using namespacestd;4 5 classCsubsysa6 {7  Public:8     voidDomethioda ()9     {Tencout <<"the invocation of a system"<<Endl; One     } A }; -  - classCSUBSYSB the { -  Public: -     voidDomethiodb () -     { +cout <<"Invocation of System B"<<Endl; -     } + }; A  at //For non-façade objects do not participate in the specific business logic, the subsystem is encapsulated in a layer, providing a unified interface - classCSUBSYSC - { -  Public: -CSUBSYSC (csubsysa* PA, csubsysb*pb): M_psuba (PA), M_psubb (Pb) {}; -     //do not manage member pointers pointing to Objects in~CSUBSYSC () -     { to  +     } -  the  Public: *     voidDomethiodab () $     {Panax NotoginsengM_psuba->Domethioda (); -M_psubb->Domethiodb (); the     } + Private: Acsubsysa*M_psuba; thecsubsysb*M_psubb; + }; - classCfacadedef $ { $  Public: - cfacadedef (); -~cfacadedef (); the }; - Wuyi classCfacade the { -  Public: Wu Cfacade () -     { AboutM_psuba =NewCsubsysa (); $M_psubb =NewCSUBSYSB (); -M_PSUBC =NewCSUBSYSC (M_psuba, M_psubb); -     } -~Cfacade () A     { + Delete M_psuba; the Delete M_psubb; - Delete m_psubc; $     } the  Public: the     //Unified Template Class Interface object the     voidDomethioda () the     { -M_psuba->Domethioda (); in     } the     voidDomethiodb () the     { AboutM_psubb->Domethiodb (); the     } the     //This is done so that the template class is not involved in the specific business object the     voidDomethiodac () +     { -M_psubc->Domethiodab (); the     }Bayi  the Private: thecsubsysa*M_psuba; -csubsysb*M_psubb; -csubsysc*M_PSUBC; the};

(1) Template application

Main.cpp

1 //Facade.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"FacadeDef.h"6 7 int_tmain (intARGC, _tchar*argv[])8 {9cfacade* Pfacade =NewCfacade ();TenPfacade->Domethioda (); OnePfacade->Domethiodb (); APfacade->Domethiodac (); - Delete Pfacade; -System"Pause"); the     return 0; -}

(2) Output display

Designpatterns Study Notes: C + + language implementation---2.6 facade

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.