标签:
// Design Pattern.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> #include <vector> using namespace std; //文件类 class TextFile { private: string name; public: TextFile(string n) : name(n) { } //文件操作功能 void Option() { cout << name <<" 文件操作" << endl; } }; class FileFolder { private: string name; vector<FileFolder*> fileFolderVec; vector<TextFile*> textFileVec; public: FileFolder(string n) : name(n) { } void AddFile(TextFile* file) { textFileVec.push_back(file); } void AddFileFolder(FileFolder* folder) { fileFolderVec.push_back(folder); } void Option() { cout<< name << " 文件夹被操作" << endl; //先遍历文件容器,将文件夹本身包含的文件进行操作 for (vector<TextFile*>::iterator it = textFileVec.begin(); it != textFileVec.end(); ++it) { (*it)->Option(); } //再遍历文件夹容器,递归调用文件夹下文件夹的操作 for (vector<FileFolder*>::iterator it = fileFolderVec.begin(); it != fileFolderVec.end(); ++it) { (*it)->Option(); } } }; int _tmain(int argc, _TCHAR* argv[]) { //我们创建几个文件fileA1,fileA2, fileB1, fileB2, fileC1和文件夹folderA,folderB,folderC TextFile* fileA1 = new TextFile("fileA1"); TextFile* fileA2 = new TextFile("fileA2"); TextFile* fileB1 = new TextFile("fileB1"); TextFile* fileB2 = new TextFile("fileB2"); TextFile* fileC1 = new TextFile("fileC1"); FileFolder* folderA = new FileFolder("folderA"); FileFolder* folderB = new FileFolder("folderB"); FileFolder* folderC = new FileFolder("folderC"); //设置文件夹与文件之间的包含关系为C(A(A1,A2) B(B1,B2),C1) folderA->AddFile(fileA1); folderA->AddFile(fileA2); folderB->AddFile(fileB1); folderB->AddFile(fileB2); folderC->AddFile(fileC1); folderC->AddFileFolder(folderA); folderC->AddFileFolder(folderB); //操作C文件夹,递归操作C文件夹下所有文件夹与文件 folderC->Option(); system("pause"); return 0; }结果:
// Design Pattern.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> #include <vector> using namespace std; //抽象出来一个文件夹和文件的基类 class Component { public: virtual void Add(Component* component) = 0; virtual void Option() = 0; }; //文件类 class TextFile : public Component { private: string name; public: TextFile(string n) : name(n) { } //覆写添加子节点的功能,由于叶子节点没有增加子节点的功能,这里给出错误提示 virtual void Add(Component* component) override { cout << "非法操作,文件下不可再包含文件!" << endl; } //覆写操作功能 virtual void Option() override { cout << name <<" 文件操作" << endl; } }; //文件夹类 class FileFolder : public Component { private: string name; vector<Component*> componentVec; public: FileFolder(string n) : name(n) { } //覆写添加子节点的功能,针对接口编程,我们可以省略很多重复代码 void Add(Component* component) override { componentVec.push_back(component); } //覆写操作功能 void Option() override { cout<< name << " 文件夹被操作" << endl; //直接针对所有子节点进行操作 for (vector<Component*>::iterator it = componentVec.begin(); it != componentVec.end(); ++it) { (*it)->Option(); } } }; int _tmain(int argc, _TCHAR* argv[]) { //我们创建几个文件fileA1,fileA2, fileB1, fileB2, fileC1和文件夹folderA,folderB,folderC TextFile* fileA1 = new TextFile("fileA1"); TextFile* fileA2 = new TextFile("fileA2"); TextFile* fileB1 = new TextFile("fileB1"); TextFile* fileB2 = new TextFile("fileB2"); TextFile* fileC1 = new TextFile("fileC1"); FileFolder* folderA = new FileFolder("folderA"); FileFolder* folderB = new FileFolder("folderB"); FileFolder* folderC = new FileFolder("folderC"); //设置文件夹与文件之间的包含关系为C(A(A1,A2) B(B1,B2),C1),所有的操作都是用Add来进行 folderA->Add(fileA1); folderA->Add(fileA2); folderB->Add(fileB1); folderB->Add(fileB2); folderC->Add(fileC1); folderC->Add(folderA); folderC->Add(folderB); //操作C文件夹,递归操作C文件夹下所有文件夹与文件 folderC->Option(); system("pause"); return 0; }结果:
// Design Pattern.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> #include <vector> using namespace std; //抽象出来一个文件夹和文件的基类 class Component { public: virtual void Option() = 0; }; //文件类 class TextFile : public Component { private: string name; public: TextFile(string n) : name(n) { } //覆写操作功能 virtual void Option() override { cout << name <<" 文件操作" << endl; } }; //文件夹类 class FileFolder : public Component { private: string name; vector<Component*> componentVec; public: FileFolder(string n) : name(n) { } //添加子节点的功能 void Add(Component* component) { componentVec.push_back(component); } //覆写操作功能 void Option() override { cout<< name << " 文件夹被操作" << endl; //直接针对所有子节点进行操作 for (vector<Component*>::iterator it = componentVec.begin(); it != componentVec.end(); ++it) { (*it)->Option(); } } }; int _tmain(int argc, _TCHAR* argv[]) { //我们创建几个文件fileA1,fileA2, fileB1, fileB2, fileC1和文件夹folderA,folderB,folderC TextFile* fileA1 = new TextFile("fileA1"); TextFile* fileA2 = new TextFile("fileA2"); TextFile* fileB1 = new TextFile("fileB1"); TextFile* fileB2 = new TextFile("fileB2"); TextFile* fileC1 = new TextFile("fileC1"); FileFolder* folderA = new FileFolder("folderA"); FileFolder* folderB = new FileFolder("folderB"); FileFolder* folderC = new FileFolder("folderC"); //设置文件夹与文件之间的包含关系为C(A(A1,A2) B(B1,B2),C1),所有的操作都是用Add来进行 folderA->Add(fileA1); folderA->Add(fileA2); folderB->Add(fileB1); folderB->Add(fileB2); folderC->Add(fileC1); folderC->Add(folderA); folderC->Add(folderB); //操作C文件夹,递归操作C文件夹下所有文件夹与文件 folderC->Option(); system("pause"); return 0; }结果同上。
标签:
原文地址:http://blog.csdn.net/puppet_master/article/details/51338802