1 //Decorator.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <memory>6#include <string>7#include <iostream>8 using namespacestd;9 Ten class Person One { A Public: -Person (stringName =""): name_ (name) - {} the Virtual voidShow () - { -cout <<"My name is:"<< name_ <<Endl; - } + protected: - stringname_; + }; A at classFinery: Public Person - { - Public: - voidDecorator (shared_ptr<person>Person ) - { -Person_ =Person ; in } - Virtual voidShow () to { + if(Nullptr! =person_) - { thePerson_->Show (); * } $ }Panax Notoginseng protected: -Shared_ptr<person>Person_; the }; + A classTshirt: PublicFinery the { + Public: - Virtual voidShow () $ { $cout <<"This is tshirt."<<Endl; -Person_->Show (); - } the }; - Wuyi classSneaker: PublicFinery the { - Public: Wu Virtual voidShow () - { Aboutcout <<"This is sneaker."<<Endl; $Person_->Show (); - } - }; - A intMain () + { theAuto person = make_shared<person> ("Cauchy"); -Auto Tshirt = make_shared<tshirt>(); $Auto sneaker = make_shared<sneaker>(); theTshirt->Decorator (person); theTshirt->Show (); theSneaker->Decorator (tshirt); theSneaker->Show (); - in return 0; the}
Design mode (4)---decoration mode