日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

设计模式-结构型-装饰

發(fā)布時間:2025/3/21 asp.net 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式-结构型-装饰 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#pragma once#ifndef DECORATOR_H #define DECORATOR_H // 抽象基類,定義一個對象接口,可以為這個接口動態(tài)的添加職責. class Component { public: Component(){} virtual ~Component(){} // 純虛函數(shù),由派生類實現(xiàn) virtual void Operation() = 0; }; // 抽象基類,維護一個指向Component對象的指針 class Decorator : public Component { public: Decorator(Component* pComponent) : m_pComponent(pComponent){} virtual ~Decorator(); protected: Component* m_pComponent; }; // 派生自Component,在這里表示需要給它動態(tài)添加職責的類 class ConcreateComponent : public Component { public: ConcreateComponent(){} virtual ~ConcreateComponent(){} virtual void Operation(); }; // 派生自Decorator,這里代表為ConcreateComponent動態(tài)添加職責的類 class ConcreateDecorator : public Decorator { public: ConcreateDecorator(Component* pComponent) : Decorator(pComponent){} virtual ~ConcreateDecorator(){} virtual void Operation(); private: void AddedBehavior(); }; #endif
#include "StdAfx.h" #include "decorator_impl.h"#include <iostream> Decorator::~Decorator() { delete m_pComponent; m_pComponent = NULL; } void ConcreateComponent::Operation() { std::cout << "Operation of ConcreateComponent\n"; } void ConcreateDecorator::Operation() { m_pComponent->Operation(); //componet原有職責AddedBehavior(); //decorator動態(tài)添加職責 } void ConcreateDecorator::AddedBehavior() { std::cout << "AddedBehavior of ConcreateDecorator\n"; }
// Decorator.cpp : 定義控制臺應(yīng)用程序的入口點。 //#include "stdafx.h" #include <stdlib.h> #include <iostream>#include "decorator_impl.h"using namespace std; //動態(tài)地給一個對象添加一些額外的職責。就增加功能來說,Decorator模式相比生成子類更為靈活 int _tmain(int argc, _TCHAR* argv[]) {// 初始化一個Component對象 Component* pComponent = new ConcreateComponent(); // 采用這個Component對象去初始化一個Decorator對象, cout<<"before decorator"<<endl;pComponent->Operation();cout<<endl;// 這樣就可以為這個Component 對象動態(tài)添加職責 Decorator* pDecorator = new ConcreateDecorator(pComponent); //Component* pDecorator = new ConcreateDecorator(pComponent); cout<<"after decorator"<<endl;//對外表現(xiàn)為執(zhí)行了component基本職責,并執(zhí)行了decorator動態(tài)添加的職責pDecorator->Operation(); delete pDecorator; system("pause"); return 0; }
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的设计模式-结构型-装饰的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。