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

歡迎訪問 生活随笔!

生活随笔

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

asp.net

设计模式复习-组合模式

發布時間:2025/6/17 asp.net 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式复习-组合模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#pragma once #include "stdafx.h" #include<set> #include<string> #include<iostream> using namespace std;/* 設計模式-組合模式(Composite)[用類建一棵樹]將對象組合成樹形結構以表示 部分-整體 的層次結構。組合模式使得用戶對 單個對象和組合對象的使用具有一致性。 */class CComponent {//所有節點統一接口 protected:string m_strName; public:CComponent(const string &strName) {m_strName = strName;}virtual void Add(CComponent * const pc) = 0;virtual void Remove(CComponent * const pc) = 0;virtual void Display(const int & nDepth) = 0; };class CLeaf : public CComponent { public:CLeaf(const string &strName) : CComponent(strName) {}void Add(CComponent *const pc) {cout << "Cannot add to a leaf" << endl;}void Remove(CComponent *const pc) {cout << "Cannot remove from a leaf" << endl;}void Display(const int &nDepth) {cout << "-" << nDepth << ":" << m_strName << endl;} };class CCpmposite : public CComponent { private:set<CComponent*>m_cChildren; public:CCpmposite(const string &strName) : CComponent(strName) {m_cChildren.clear();}void Add(CComponent * const pc) {m_cChildren.insert(pc);}void Remove(CComponent * const pc) {m_cChildren.erase(pc);delete pc;}void Display(const int &nDepth) {cout << "-" << nDepth << ":" << m_strName << endl;for each(auto i in m_cChildren) {i->Display(nDepth + 1);}}~CCpmposite() {for each(auto i in m_cChildren) {delete i;}} };int main() {CCpmposite *pRoot = new CCpmposite("root");pRoot->Add(new CLeaf("Lead A"));pRoot->Add(new CLeaf("Lead B"));CCpmposite *pComp = new CCpmposite("Composite X");pComp->Add(new CLeaf("Lead A"));pComp->Add(new CLeaf("Lead B"));pRoot->Add(pComp);pRoot->Display(1);delete pRoot;getchar();return 0; }

總結

以上是生活随笔為你收集整理的设计模式复习-组合模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。