Composite 合成模式可以让客户程序把单个基本对象和对象的合成用一种统一的方式进行处理
MyFile
1using System;
2
3namespace Gof.Test.Composite
4{
5 public class MyFile:FolderAbstract
6 {
7 public MyFile(string name)
8 {
9 _name = name;
10 }
11 private string _name = string.Empty;
12
13 public override void Add(FolderAbstract folder)
14 {
15 Console.WriteLine("Can't Add a Folder or a File to a File");
16 }
17 public override string DisplayName()
18 {
19 return _name;
20 }
21 public override void Remove(FolderAbstract folder)
22 {
23 Console.WriteLine("Can't Remove a Folder or a File to a File");
24 }
25 }
26}
27
1using System;
2
3namespace Gof.Test.Composite
4{
5 public class MyFile:FolderAbstract
6 {
7 public MyFile(string name)
8 {
9 _name = name;
10 }
11 private string _name = string.Empty;
12
13 public override void Add(FolderAbstract folder)
14 {
15 Console.WriteLine("Can't Add a Folder or a File to a File");
16 }
17 public override string DisplayName()
18 {
19 return _name;
20 }
21 public override void Remove(FolderAbstract folder)
22 {
23 Console.WriteLine("Can't Remove a Folder or a File to a File");
24 }
25 }
26}
27
Component
1using System;
2
3namespace Gof.Test.Composite
4{
5 public abstract class FolderAbstract
6 {
7 public FolderAbstract()
8 {}
9
10 public string Name
11 {
12 get
13 {
14 return _name;
15 }
16 set
17 {
18 _name = value;
19 }
20 }private string _name = string.Empty;
21
22 public abstract void Add(FolderAbstract folder);
23 public abstract void Remove(FolderAbstract folder);
24 public abstract string DisplayName();
25 }
26}
1using System;
2
3namespace Gof.Test.Composite
4{
5 public abstract class FolderAbstract
6 {
7 public FolderAbstract()
8 {}
9
10 public string Name
11 {
12 get
13 {
14 return _name;
15 }
16 set
17 {
18 _name = value;
19 }
20 }private string _name = string.Empty;
21
22 public abstract void Add(FolderAbstract folder);
23 public abstract void Remove(FolderAbstract folder);
24 public abstract string DisplayName();
25 }
26}
Folder
1using System;
2
3namespace Gof.Test.Composite
4{
5 public class Folder:FolderAbstract
6 {
7 public Folder(string name)
8 {
9 _name = name;
10 }
11 private string _name = string.Empty;
12 private System.Collections.ArrayList arForlders = new System.Collections.ArrayList();
13 public override void Add(FolderAbstract folder)
14 {
15 arForlders.Add(folder);
16 }
17 public override string DisplayName()
18 {
19 string result = ""+_name+" has ";
20 for(int i=0;i<arForlders.Count;i++)
21 {
22 result += ((FolderAbstract)arForlders[i]).DisplayName() + " ";
23 }
24 return result;
25 }
26 public override void Remove(FolderAbstract folder)
27 {
28 arForlders.Remove(folder);
29 }
30 }
31}
1using System;
2
3namespace Gof.Test.Composite
4{
5 public class Folder:FolderAbstract
6 {
7 public Folder(string name)
8 {
9 _name = name;
10 }
11 private string _name = string.Empty;
12 private System.Collections.ArrayList arForlders = new System.Collections.ArrayList();
13 public override void Add(FolderAbstract folder)
14 {
15 arForlders.Add(folder);
16 }
17 public override string DisplayName()
18 {
19 string result = ""+_name+" has ";
20 for(int i=0;i<arForlders.Count;i++)
21 {
22 result += ((FolderAbstract)arForlders[i]).DisplayName() + " ";
23 }
24 return result;
25 }
26 public override void Remove(FolderAbstract folder)
27 {
28 arForlders.Remove(folder);
29 }
30 }
31}
客户代码
1Gof.Test.Composite.Folder folder = new Gof.Test.Composite.Folder("FolderOne");
2 Gof.Test.Composite.MyFile file = new Gof.Test.Composite.MyFile("FileOne");
3 folder.Add(file);
4 Gof.Test.Composite.MyFile file2 = new Gof.Test.Composite.MyFile("FileTwo");
5 folder.Add(file2);
6 Gof.Test.Composite.MyFile file3 = new Gof.Test.Composite.MyFile("FileThree");
7 folder.Add(file3);
8 Gof.Test.Composite.Folder folder2 = new Gof.Test.Composite.Folder("FolderTwo");
9 folder.Add(folder2);
10 Gof.Test.Composite.MyFile file4 = new Gof.Test.Composite.MyFile("FileFour");
11 folder2.Add(file4);
12 //Display name of file3
13 Console.WriteLine( file3.DisplayName() );
14 Console.ReadLine();
15 //Display name of folder
16 Console.WriteLine( folder.DisplayName() );
17 Console.ReadLine();
1Gof.Test.Composite.Folder folder = new Gof.Test.Composite.Folder("FolderOne");
2 Gof.Test.Composite.MyFile file = new Gof.Test.Composite.MyFile("FileOne");
3 folder.Add(file);
4 Gof.Test.Composite.MyFile file2 = new Gof.Test.Composite.MyFile("FileTwo");
5 folder.Add(file2);
6 Gof.Test.Composite.MyFile file3 = new Gof.Test.Composite.MyFile("FileThree");
7 folder.Add(file3);
8 Gof.Test.Composite.Folder folder2 = new Gof.Test.Composite.Folder("FolderTwo");
9 folder.Add(folder2);
10 Gof.Test.Composite.MyFile file4 = new Gof.Test.Composite.MyFile("FileFour");
11 folder2.Add(file4);
12 //Display name of file3
13 Console.WriteLine( file3.DisplayName() );
14 Console.ReadLine();
15 //Display name of folder
16 Console.WriteLine( folder.DisplayName() );
17 Console.ReadLine();