Composite 合成模式可以让客户程序把单个基本对象和对象的合成用一种统一的方式进行处理...

本文介绍了一种基于复合设计模式的文件系统实现方法,通过抽象类FolderAbstract定义了文件夹和文件的基本操作,并通过具体类Folder和MyFile实现了文件夹与文件的功能。Folder可以包含多个子文件或文件夹,而MyFile则代表单一文件。
ContractedBlock.gifExpandedBlockStart.gifMyFile
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Composite
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class MyFile:FolderAbstract
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public MyFile(string name)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            _name = name;
10ExpandedSubBlockEnd.gif        }

11InBlock.gif        private string _name = string.Empty;
12InBlock.gif
13InBlock.gif        public override void Add(FolderAbstract folder)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            Console.WriteLine("Can't Add a Folder or a File to a File");
16ExpandedSubBlockEnd.gif        }

17InBlock.gif        public override string DisplayName()
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            return _name;
20ExpandedSubBlockEnd.gif        }

21InBlock.gif        public override void Remove(FolderAbstract folder)
22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
23InBlock.gif            Console.WriteLine("Can't Remove a Folder or a File to a File");
24ExpandedSubBlockEnd.gif        }
 
25ExpandedSubBlockEnd.gif    }

26ExpandedBlockEnd.gif}
                          
27None.gif
ContractedBlock.gifExpandedBlockStart.gifComponent
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Composite
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public abstract class FolderAbstract
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public FolderAbstract()
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
 9InBlock.gif
10InBlock.gif        public string Name
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{                                    
12InBlock.gif            get
13ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
14InBlock.gif                return _name;
15ExpandedSubBlockEnd.gif            }

16InBlock.gif            set
17ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
18InBlock.gif                _name = value;
19ExpandedSubBlockEnd.gif            }

20ExpandedSubBlockEnd.gif        }
private string _name = string.Empty;
21InBlock.gif
22InBlock.gif        public abstract void Add(FolderAbstract folder);
23InBlock.gif        public abstract void Remove(FolderAbstract folder);
24InBlock.gif        public abstract string DisplayName();
25ExpandedSubBlockEnd.gif    }

26ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gifFolder
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Composite
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class Folder:FolderAbstract
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public Folder(string name)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            _name = name;            
10ExpandedSubBlockEnd.gif        }

11InBlock.gif        private string _name = string.Empty;
12InBlock.gif        private System.Collections.ArrayList arForlders = new System.Collections.ArrayList();
13InBlock.gif        public override void Add(FolderAbstract folder)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            arForlders.Add(folder);
16ExpandedSubBlockEnd.gif        }

17InBlock.gif        public override string DisplayName()
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            string result = ""+_name+" has ";
20InBlock.gif            for(int i=0;i<arForlders.Count;i++)
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                result += ((FolderAbstract)arForlders[i]).DisplayName() + " ";
23ExpandedSubBlockEnd.gif            }

24InBlock.gif            return result;
25ExpandedSubBlockEnd.gif        }

26InBlock.gif        public override void Remove(FolderAbstract folder)
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            arForlders.Remove(folder);
29ExpandedSubBlockEnd.gif        }

30ExpandedSubBlockEnd.gif    }

31ExpandedBlockEnd.gif}
ContractedBlock.gifExpandedBlockStart.gif客户代码
 1None.gifGof.Test.Composite.Folder folder = new Gof.Test.Composite.Folder("FolderOne");
 2None.gif                Gof.Test.Composite.MyFile file = new Gof.Test.Composite.MyFile("FileOne");
 3None.gif            folder.Add(file);
 4None.gif                Gof.Test.Composite.MyFile file2 = new Gof.Test.Composite.MyFile("FileTwo");
 5None.gif            folder.Add(file2);    
 6None.gif                Gof.Test.Composite.MyFile file3 = new Gof.Test.Composite.MyFile("FileThree");
 7None.gif            folder.Add(file3);
 8None.gif            Gof.Test.Composite.Folder folder2 = new Gof.Test.Composite.Folder("FolderTwo");
 9None.gif            folder.Add(folder2);
10None.gif            Gof.Test.Composite.MyFile file4 = new Gof.Test.Composite.MyFile("FileFour");
11None.gif            folder2.Add(file4);
12None.gif            //Display name of file3
13None.gif            Console.WriteLine( file3.DisplayName() );
14None.gif            Console.ReadLine();
15None.gif            //Display name of folder
16None.gif            Console.WriteLine( folder.DisplayName() );
17None.gif            Console.ReadLine();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值