Decorative mode decorate

Source: Internet
Author: User

The ability to dynamically extend an object without having to change the original class file and use inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object.

Characteristics:

(1) The Decoration object and the real object have the same interface. This allows the client object to interact with the adornment object in the same way as the real object. (2) The Adornment object contains a reference to a real object (reference) (3) The Adornment object accepts all requests from the client. It forwards these requests to the real object. (4) Decorative objects can add additional functionality before or after forwarding these requests. This ensures that additional functionality can be added externally without modifying the structure of a given object at run time. In object-oriented design, the extension of functionality to a given class is usually achieved through inheritance. Applies To: Use Decorator Mode 1 for the following situations. You need to extend the functionality of a class or add additional responsibilities to a class. 2. The need to dynamically add functionality to an object, these functions can be re-dynamic revocation. 3. It is necessary to increase the number of functions resulting from the permutations of some basic functions, thus making the inheritance relationship impractical. 4. When a method of generating subclasses cannot be used for expansion. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses. 1. If there is only one concrete Component class without an abstract Component interface, you can let decorator inherit concrete Component. 2. If there is only one concrete decorator class, you can merge decorator and concrete decorator.
//Decorator.cpp:Defines the entry point for the console application.//#include"stdafx.h"#include<iostream>#include<STRING>using namespacestd;classcake{ Public: Cake (): Name ("Cake"){}    Virtual~Cake () {}Virtual stringgetname () {returnname; } Public:    stringname;};classMikecake: Publiccake{ Public: Mikecake () { This->name ="Milkcake"; } mikecake (stringstr) {         This->name =str; }};classDecorate: Publiccake{ Public: Decorate (Cake*p):p cake (p) {}Virtual~decorate () {};protected: Cake*Pcake;};classFlowerdecorate: Publicdecorate{ Public: Flowerdecorate (Cake*p):D ecorate (p) {Name= (string("flowered --") +=p->getname ()); }};classFruitdecorate: Publicdecorate{ Public: Fruitdecorate (Cake*p):D ecorate (p) {Name= (string("fruited --") +=p->getname ()); }};intMainintargcChar*argv[]) {Cake* Pcake =NewMikecake ("Cake1"); cout<<pcake->getname () <<Endl; cout<<flowerdecorate (Pcake). GetName () <<Endl; cout<<fruitdecorate (&flowerdecorate (Pcake)). GetName () <<Endl; DeletePcake; return 0;}

Decorative mode decorate

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.