组合模式
用透明组合模式实现教材中的“文件夹浏览”这个例子。
类图
java
package rjsj.no10; public class Client { public static void main(String[] args) { AbstractFile obj1,obj2,obj3,obj4,obj5; Folder plate1,plate2,plate3; obj1 = new ImageFile("图片文件1"); obj2 = new TextFile("文本文件1"); plate1 = new Folder("父文件夹1"); plate1.add(obj1); plate1.add(obj2); obj3 = new VideoFile("视频文件1"); obj4 = new Folder("子文件夹1"); plate2 = new Folder("父文件夹2"); plate2.add(obj3); plate2.add(obj4); obj5 = new TextFile("文本文件2"); plate3 = new Folder("父文件夹3"); plate3.add(obj5); plate3.add(plate1); plate3.add(plate2); plate3.display(); } }
package rjsj.no10; import java.util.ArrayList; public class Folder extends AbstractFile{ private String fileName; private ArrayList fileList=new ArrayList(); public Folder(String filename) { this.fileName=new String(); this.fileName=filename; } @Override public void add(AbstractFile element) { fileList.add(element); System.out.println("新建文件夹"); } @Override public void remove(AbstractFile element) { fileList.remove(element); System.out.println("删除文件夹"); } @Override public void display() { for (Object obj:fileList){ ((AbstractFile)obj).display(); } System.out.println("打开文件夹"); } }
package rjsj.no10; public class ImageFile extends AbstractFile{ private String fileName; public ImageFile(String filename) { this.fileName=new String(); this.fileName=filename; } @Override public void add(AbstractFile element) { System.out.println("添加图片文件"); } @Override public void remove(AbstractFile element) { System.out.println("移除图片文件"); } @Override public void display() { System.out.println(fileName); System.out.println("打开图片文件"); } }
package rjsj.no10; public class TextFile extends AbstractFile{ private String fileName; public TextFile(String filename) { this.fileName=new String(); this.fileName=filename; } @Override public void add(AbstractFile element) { System.out.println("添加文本文件"); } @Override public void remove(AbstractFile element) { System.out.println("移除文本文件"); } @Override public void display() { System.out.println(fileName); System.out.println("打开文本文件"); } }
package rjsj.no10; public class VideoFile extends AbstractFile{ private String fileName; public VideoFile(String filename) { this.fileName=new String(); this.fileName=filename; } @Override public void add(AbstractFile element) { System.out.println("添加视频文件"); } @Override public void remove(AbstractFile element) { System.out.println("移除视频文件"); } @Override public void display() { System.out.println(fileName); System.out.println("播放视频文件"); } }
package rjsj.no10; public abstract class AbstractFile { public abstract void add(AbstractFile element); public abstract void remove(AbstractFile element); public abstract void display(); }
C++
#include<iostream> #include<string> #include<list> using namespace std; class AbstractFile { public: virtual void add(AbstractFile* element) = 0; virtual void remove(AbstractFile* element) = 0; virtual void display() = 0; }; class VideoFile : public AbstractFile { private: string fileName; public: VideoFile(string fileName) { this->fileName = fileName; } void add(AbstractFile* element) { cout << "打开VideoFile" << endl; } void remove(AbstractFile* element) { cout << "关闭VideoFile" << endl; } void display() { cout << "浏览VideoFile" << endl; } void displayFile() { cout << "VideoFile的内容" << endl; } }; class TextFile :public AbstractFile { private: string fileName; public: TextFile(string fileName) { this->fileName = fileName; } void add(AbstractFile* element) { cout << "打开" <<this->fileName<< endl; } void remove(AbstractFile* element) { cout << "关闭" << this->fileName<<endl; } void display() { cout << "浏览" << this->fileName<<endl; } void displayFile() { cout <<this->fileName<< "的内容" << endl; } }; class ImageFile :public AbstractFile { private: string fileName; public: ImageFile(string fileName) { this->fileName = fileName; } void add(AbstractFile* element) { cout << "打开" <<this->fileName<< endl; } void remove(AbstractFile* element) { cout << "关闭"<<this->fileName << endl; } void display() { cout << "浏览"<<this->fileName << endl; } void displayFile() { cout <<this->fileName<< "的内容" << endl; } }; class Folder :public AbstractFile { private: list<AbstractFile*>* _list; string fileName; public: Folder(string fileName) { this->fileName = fileName; _list = new list<AbstractFile*>; } ~Folder() { if (_list != NULL) { while (_list->empty()) { delete *(_list->begin()); _list->erase(_list->begin()); } delete _list; } } string getFileName() const { return fileName; } void add(AbstractFile* element) { _list->push_back(element); } void remove(AbstractFile* element) { cout << "关闭"<<this->fileName; _list->remove(element); } void display() { if (_list != NULL) { list<AbstractFile*>::iterator it = _list->begin(); while (it != _list->end()) { (*it++)->display(); } } } }; int main() { AbstractFile *obj1, *obj2, *obj3, *obj4; Folder *plate1, *plate2; obj1 = new ImageFile("ImageFile"); obj2 = new TextFile("TextFile"); plate1 = new Folder("folder1"); cout << "打开Folder1"; plate1->add(obj1); plate1->add(obj2); plate1->display(); obj3 = new VideoFile("VideoFile"); cout << "打开Folder2"; plate2 = new Folder("folder2"); plate2->add(obj3); plate2->display(); plate1->remove(obj1); plate1->remove(obj3); }
运行截图