转载地址:
http://www.cnblogs.com/xyz168/archive/2011/06/26/2090512.html
本文主要分为三部分:1、组合模式的基本定义;2、组合模式的类图结构;3、代码示例
一、定义
Composite:将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得客户对单个对象和复合对象的使用具有一致性。
二、类图结构
三、代码示例
例子:一个项目包含多个文件夹、类、接口等,而一个文件夹也同样可以包含多个文件夹、类、接口。
代码:
项目抽象类:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacey.CompositePattern
{
publicabstractclass ComponentProject
{
protectedstringname = string.Empty;
publicComponentProject(stringname)
{
this.name = name;
}
publicabstractvoid Add(ComponentProject componentProject);
publicabstractvoid Remove(ComponentProject componentProject);
publicabstractvoid ShowTitle(intdepth);
}
}
文件夹实现类:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacey.CompositePattern
{
publicclassCompositeFolder:ComponentProject
{
privateList<ComponentProject> subFolder = newList<ComponentProject>();
publicCompositeFolder(stringname)
:base(name)
{ }
publicoverridevoid Add(ComponentProject componentProject)
{
subFolder.Add(componentProject);
}
publicoverridevoid Remove(ComponentProject componentProject)
{
subFolder.Remove(componentProject);
}
publicoverridevoid ShowTitle(intdepth)
{
Console.WriteLine(newstring('-',depth)+name);
foreach(ComponentProject project insubFolder)
{
project.ShowTitle(depth+2);
}
}
}
}
”类“实现类:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacey.CompositePattern
{
publicclassLeafClass:ComponentProject
{
publicLeafClass(stringname)
:base(name)
{ }
publicoverridevoid Add(ComponentProject componentProject)
{
}
publicoverridevoid Remove(ComponentProject componentProject)
{
}
publicoverridevoid ShowTitle(intdepth)
{
Console.WriteLine(newstring('-', depth) + name);
}
}
}
“接口”实现类:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacey.CompositePattern
{
publicclassLeafInterface:ComponentProject
{
publicLeafInterface(stringname)
:base(name)
{ }
publicoverridevoid Add(ComponentProject componentProject)
{
}
publicoverridevoid Remove(ComponentProject componentProject)
{
}
publicoverridevoid ShowTitle(intdepth)
{
Console.WriteLine(newstring('-', depth) + name);
}
}
}
主程序:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacey.CompositePattern
{
classProgram
{
staticvoidMain(string[] args)
{
Console.Title="组合模式";
Console.WriteLine("Press any ket to exit program.\r\n");
CompositeFolder mainFolder = newCompositeFolder("Composite Example");
mainFolder.Add(newLeafClass("Main Class"));
mainFolder.Add(newLeafInterface("Main Interface"));
CompositeFolder bussinessFolder = newCompositeFolder("Bussiness Folder");
bussinessFolder.Add(newLeafInterface("Bussiness Interface"));
bussinessFolder.Add(newLeafClass("Bussiness Class"));
mainFolder.Add(bussinessFolder);
CompositeFolder databaseFolder = newCompositeFolder("Database Folder");
databaseFolder.Add(newLeafInterface("Database Interface"));
databaseFolder.Add(newLeafClass("Database Class"));
mainFolder.Add(databaseFolder);
Console.WriteLine("Composite Struct:");
mainFolder.ShowTitle(1);
Console.ReadKey();
}
}
}
参考文献:
《大话设计模式》
大话设计模式之组合模式
最新推荐文章于 2024-09-06 16:47:07 发布