设计模式——组合模式

一、任务

用透明组合模式实现教材中的“文件夹浏览”这个例子

二、类图

三、代码

1、Java

 1 package tutorial10;
 2 
 3 public class Client {
 4 
 5     public static void main(String[] args) {
 6         // TODO 自动生成的方法存根
 7         File obj1,obj2,obj3,obj4,obj5;
 8         Folder plate1,plate2,plate3;
 9         
10         obj1=new ImageFile("image1");
11         obj2=new TextFile("text");
12         plate1=new Folder("folder1");
13         plate1.addFile(obj1);
14         plate1.addFile(obj2);
15         
16         obj3=new VideoFile("video1");
17         obj4=new VideoFile("video2");
18         plate2=new Folder("folder2");
19         plate2.addFile(obj3);
20         plate2.addFile(obj4);
21         
22         obj5=new ImageFile("image2");
23         plate3=new Folder("folder3");
24         plate3.addFile(plate1);
25         plate3.addFile(plate2);
26         plate3.addFile(obj5);
27         
28         plate3.show(plate3);
29     }
30 
31 }
Client.Java
 1 package tutorial10;
 2 
 3 public abstract class File {
 4 
 5     
 6     
 7     public void addFile(File element) {
 8         
 9     }
10     
11     public void removeFile(File element) {
12         
13     }
14     
15     public void show(File element) {
16         
17     }
18 }
File
 1 package tutorial10;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class Folder extends File {
 6     private String fileName;
 7     private ArrayList fileList=new ArrayList();
 8     public Folder(String name) {
 9         this.fileName=new String();
10         this.fileName=name;
11     }
12     
13     
14     public String getFileName() {
15         return fileName;
16     }
17 
18 
19     public void addFile(File element)
20     {
21         fileList.add(element);
22         System.out.println("增加文件夹"+element.getClass().getName());
23     }
24     public void removeFile(File element)
25     {
26         fileList.remove(element);
27         System.out.println("删除文件夹"+element.getClass().getName());
28     }
29     public void show(File element)
30     {
31         System.out.println("展示文件夹:");
32         for(Object object:fileList)
33         {
34             ((File)object).show(element);
35         }
36         
37     }
38 }
Folder
 1 package tutorial10;
 2 
 3 public class ImageFile extends File{
 4     private String fileName;
 5     
 6     public ImageFile(String filename)
 7     {
 8         this.fileName=new String();
 9         this.fileName=filename;
10     }
11 
12     public void addFile(File element) {
13         System.out.println("增加图像文件夹:"+element.getClass().getName());
14     }
15     
16     public void removeFile(File element) {
17         System.out.println("删除图像文件夹:"+element.getClass().getName());
18     }
19     
20     public void show(File element) {
21         System.out.println("展示图像文件夹:"+element.getClass().getName());
22     }
23 }
ImageFile
 1 package tutorial10;
 2 
 3 public class TextFile extends File {
 4     private String fileName;
 5     public TextFile(String fileName) {
 6         this.fileName=new String();
 7         this.fileName=fileName;
 8     }
 9 
10     public void addFile(File element) {
11         System.out.println("增加文本文件夹:"+element.getClass().getName());
12     }
13     
14     public void removeFile(File element) {
15         System.out.println("删除文本文件夹:"+element.getClass().getName());
16     }
17     
18     public void show(File element) {
19         System.out.println("展示文本文件夹:"+element.getClass().getName());
20     }
21 }
TextFile
 1 package tutorial10;
 2 
 3 public class VideoFile extends File {
 4     private String fileName;
 5     public VideoFile(String filename) {
 6         this.fileName=new String();
 7         this.fileName=filename;
 8     }
 9 
10     public void addFile(File element) {
11         System.out.println("增加视频文件夹:"+element.getClass().getName());
12     }
13     
14     public void removeFile(File element) {
15         System.out.println("删除视频文件夹:"+element.getClass().getName());
16     }
17     
18     public void show(File element) {
19         System.out.println("展示视频文件夹:"+element.getClass().getName());
20     }
21 }
VideoFile

2、C++

  1 #include <iostream>
  2 #include <string>
  3 #include<list>
  4 using namespace std;
  5 
  6 class AbstractFile
  7 {
  8 public:
  9     virtual void add() {   }
 10     virtual void remove() {   }
 11     virtual void display() { }
 12 };
 13 
 14 class ImageFile : public AbstractFile
 15 {
 16 private:
 17     string fileName;
 18 public:
 19     ImageFile(string filename)
 20     {
 21         fileName = filename;
 22     }
 23     void add()
 24     {
 25         cout << "添加成功" << endl;
 26     }
 27     void remove()
 28     {
 29         cout << "删除成功" << endl;
 30     }
 31     void display()
 32     {
 33         cout << fileName << endl;
 34     }
 35 };
 36 
 37 
 38 class TextFile : public AbstractFile
 39 {
 40 private:
 41     string fileName;
 42 public:
 43     TextFile(string filename)
 44     {
 45         fileName = filename;
 46     }
 47     void add()
 48     {
 49         cout << "添加成功" << endl;
 50     }
 51     void remove()
 52     {
 53         cout << "删除成功" << endl;
 54     }
 55     void display()
 56     {
 57         cout << fileName << endl;
 58     }
 59 };
 60 
 61 class VideoFile : public AbstractFile
 62 {
 63 private:
 64     string fileName;
 65 public:
 66     VideoFile(string filename)
 67     {
 68         fileName = filename;
 69     }
 70     void add()
 71     {
 72         cout << "添加成功" << endl;
 73     }
 74     void remove()
 75     {
 76         cout << "删除成功" << endl;
 77     }
 78     void display()
 79     {
 80         cout << fileName << endl;
 81     }
 82 };
 83 
 84 class Folder : public  AbstractFile
 85 {
 86 private:
 87     string fileName;
 88     int level;
 89     list<AbstractFile*> abstractfiles;
 90 
 91 public:
 92     Folder(string filename)
 93     {
 94         fileName = filename;
 95     }
 96     Folder(string filename, int level)
 97     {
 98         fileName = filename;
 99         this->level = level;
100     }
101     void add(AbstractFile* abstractfile)
102     {
103         abstractfiles.push_front(abstractfile);
104     }
105     void remove(AbstractFile* abstractfile)
106     {
107         abstractfiles.remove(abstractfile);
108     }
109     void  display()
110     {
111         cout << fileName << endl;
112         list<AbstractFile*>::iterator iter = abstractfiles.begin();
113         for (; iter != abstractfiles.end(); iter++)
114         {
115             if (this->level != 1)
116             {
117                 cout << "   -";
118                 (*iter)->display();
119             }
120             else {
121                 cout << "             *";
122                 (*iter)->display();
123             }
124         }
125     }
126 };
127 
128 int main()
129 {
130     
131     int choice;
132     bool flag = true;
133     cout << "输入根目录文件夹名:" << endl;
134     string str;
135     cin >> str;
136     Folder* fold3 = new Folder(str);
137     Folder* fold1 = NULL;
138     Folder* fold2 = NULL;
139     Folder* fold4 = NULL;
140     AbstractFile* obj1 = NULL;
141     AbstractFile* obj2 = NULL;
142     AbstractFile* obj4 = NULL;
143     while(flag){
144         cout << "1、创建图像文件夹" << endl;
145         cout << "2、创建视频文件夹" << endl;
146         cout << "3、创建文本文件夹" << endl;
147         cout << "4、展示文件夹" << endl;
148         cout << "5、退出" << endl;
149         cout << "******请选择******" << endl;
150         cin >> choice;
151         if (choice==1) {
152             cout << "请输入文件夹名:";
153             string name;
154             cin >> name;
155             fold1 = new Folder(name, 1);
156             cout << "请输入文件名:";
157             string name1;
158             cin >> name1;
159             obj1 = new ImageFile(name1);
160             fold1->add(obj1);
161             fold3->add(fold1);
162         }
163         else if (choice == 2) {
164             cout << "请输入文件夹名:";
165             string name;
166             cin >> name;
167             fold2 = new Folder(name, 1);
168             cout << "请输入文件名:";
169             string name1;
170             cin >> name1;
171             obj2 = new VideoFile(name1);
172             fold2->add(obj2);
173             fold3->add(fold2);
174         }
175         
176         else if (choice == 3) {
177             cout << "请输入文件夹名:";
178             string name;
179             cin >> name;
180             fold4 = new Folder(name, 1);
181             cout << "请输入文件名:";
182             string name1;
183             cin >> name1;
184             obj4 = new TextFile(name1);
185             fold4->add(obj4);
186             fold3->add(fold4);
187         }
188         else if (choice == 4) {
189             fold3->display();
190         }
191         else if (choice == 5) {
192             cout << "****再见******" << endl;
193             
194             flag = false;
195         }
196         else {
197             cout << "输入错误!!!!" << endl;
198             flag = false;
199             
200         }
201     }
202     delete obj1, obj2, obj4;
203     delete fold1, fold2, fold3, fold4;
204     
205 }
main

四、结果截图

1、Java

2、C++

原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/14903874.html