`
langgufu
  • 浏览: 2288471 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

组合模式-Component(转)

阅读更多

一、组合模式简介(Brief Introduction

组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。

 

二、解决的问题(What To Solve

解决整合与部分可以被一致对待问题。

三、组合模式分析(Analysis

1、组合模式结构

Component类:组合中的对象声明接口,在适当情况下,实现所有类共有接口的行为。声明一个接口用于访问和管理Component的子部件

Leaf类:叶节点对象,叶节点没有子节点。由于叶节点不能增加分支和树叶,所以叶节点的Add和Remove没有实际意义。

有叶节点行为,用来存储叶节点集合

Composite类:实现Componet的相关操作,比如Add和Remove操作。

children:用来存储叶节点集合

2、源代码

1、抽象类Component

public abstract class Component

{

    protected string name;

 

    public Component(string name)

    {

        this.name = name;

    }

 

    public abstract void Add(Component c);

    public abstract void Remove(Component c);

    public abstract void Diaplay(int depth);

}

 

2、叶子节点Leaf 继承于Component

public class Leaf:Component

{

 

    public Leaf(string name)

        :base(name)

    {

       

    }

 

    public override void Add(Component c)

    {

        Console.WriteLine("不能向叶子节点添加子节点");

    }

 

    public override void Remove(Component c)

    {

        Console.WriteLine("叶子节点没有子节点");

    }

 

    public override void Diaplay(int depth)

    {

        Console.WriteLine(new string('-',depth)+name);

    }

}

 

3、组合类Composite继承于Component,拥有枝节点行为

public class Composite : Component

{

 

    List<Component> children;

 

    public Composite(string name)

        :base(name)

    {

        if (children == null)

        {

            children = new List<Component>();

        }

    }

 

    public override void Add(Component c)

    {

        this.children.Add(c);

    }

 

    public override void Remove(Component c)

    {

        this.children.Remove(c);

    }

 

    public override void Diaplay(int depth)

    {

        Console.WriteLine(new String('-',depth)+name);

        foreach (Component component in children)

        {

            component.Diaplay(depth + 2);

        }

    }

}

 

 

4、客户端代码

static void Main(string[] args)

{

    Composite root = new Composite("根节点root");

    root.Add(new Leaf("根上生出的叶子A"));

    root.Add(new Leaf("根上生出的叶子B"));

 

    Composite comp = new Composite("根上生出的分支CompositeX");

    comp.Add(new Leaf("分支CompositeX生出的叶子LeafXA"));

    comp.Add(new Leaf("分支CompositeX生出的叶子LeafXB"));

 

    root.Add(comp);

 

    Composite comp2 = new Composite("分支CompositeX生出的分支CompositeXY");

    comp2.Add(new Leaf("分支CompositeXY生出叶子LeafXYA"));

    comp2.Add(new Leaf("分支CompositeXY生出叶子LeafXYB"));

 

    comp.Add(comp2);

 

    root.Add(new Leaf("根节点生成的叶子LeafC"));

    Leaf leafD = new Leaf("leaf D");

    root.Add(leafD);

    root.Remove(leafD);

    root.Diaplay(1);

    Console.Read();

}

 

3、程序运行结果

四.案例分析(Example

1、场景

假设公司组织结构为:

--总结理

----技术部门经理

------开发人员A

------开发人员B

----销售部门经理

总经理直接领导技术部经理和销售部经理,技术部经理直接领导开发人员A和开发人员B。销售部经理暂时没有直接下属员工,随着公司规模增大,销售部门会新增销售员工。计算组织结构的总工资状况。

如下图所示

IComponent接口:此接口包括了Component和Composite的所有属性,公司每个角色都有职称Title和工资待遇Salary,Add方法把员工加入到组织团队中。

Component叶子节点:叶节点没有子节点,Add方法实现没有任何意义。

Composite组合类:此类有一个员工集合_listEmployees,Add方法向此集合中添加员工信息。

GetCost方法获得组织结构中的工资待遇总和

2、代码

1、接口IComponent

  1. public interface IComponent   
  2.     {   
  3.         string Title { getset; }   
  4.         decimal Salary { getset; }   
  5.         void Add(IComponent c);   
  6.         void GetCost(ref decimal salary);   
  7.     }   

8.    

 

2、叶节点Component 

  1. public class Component : IComponent   
  2.     {   
  3.         public string Title { getset; }   
  4.         public decimal Salary { getset; }   
  5.   
  6.         public Component(string Title, decimal Salary)   
  7.         {   
  8.             this.Title = Title;   
  9.             this.Salary = Salary;   
  10.         }   
  11.   
  12.         public void Add(IComponent c)   
  13.         {   
  14.             Console.WriteLine("Cannot add to the leaf!");   
  15.         }   
  16.   
  17.         public void GetCost(ref decimal salary)   
  18.         {   
  19.             salary += Salary;   
  20.         }   
  21.     }   

22.    

 

 

3、组合类Composite 

1.   public class Composite : IComponent   

2.       {   

3.           private List<IComponent> _listEmployees;   

4.     

5.           public string Title { getset; }   

6.           public decimal Salary { getset; }   

7.     

8.           public Composite(string Title, decimal Salary)   

9.           {   

10.               this.Title = Title;   

11.               this.Salary = Salary;   

12.               _listEmployees = new List<IComponent>();   

13.           }   

14.     

15.           public void Add(IComponent comp)   

16.           {   

17.               _listEmployees.Add(comp);   

18.           }   

19.     

20.           public void GetCost(ref decimal salary)   

21.           {   

22.               salary += this.Salary;   

23.     

24.               foreach (IComponent component in this._listEmployees)   

25.               {   

26.                   component.GetCost(ref salary);   

27.               }   

28.           }   

29.       }   

 

 

4、客户端代码

  1. static void Main(string[] args)   
  2.         {   
  3.             decimal costCEO = 0.0M;   
  4.             decimal costVPD = 0.0M;   
  5.   
  6.             //Create CEO Node   
  7.             IComponent compCEO = new Composite("CEO", 500000);   
  8.   
  9.             //Create VP-Development and Developer nodes   
  10.             IComponent compVPDev = new Composite("VP-Development", 250000);   
  11.   
  12.             IComponent compDev1 = new Component("Developer1", 75000);   
  13.             IComponent compDev2 = new Component("Developer2", 50000);   
  14.   
  15.             compVPDev.Add(compDev1);   
  16.             compVPDev.Add(compDev2);   
  17.   
  18.             //Create VP-Sales node   
  19.             IComponent compVPSales = new Component("VP-Sales", 300000);   
  20.   
  21.             compCEO.Add(compVPDev);   
  22.             compCEO.Add(compVPSales);   
  23.   
  24.             //Get the cost incurred at the CEO level   
  25.             compCEO.GetCost(ref costCEO);   
  26.   
  27.             Console.WriteLine(String.Format("The Cost incurred at the CEO            level is {0:c} ", costCEO));   
  28.   
  29.             //Get the cost incurred at the VP-Development level   
  30.             compVPDev.GetCost(ref costVPD);   
  31.             Console.WriteLine(String.Format("The Cost incurred at the VP-Development level is {0:c} ", costVPD));   
  32.         }   

33.    

 

五、总结(Summary

组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。解决整合与部分可以被一致对待问题

分享到:
评论

相关推荐

    graphql-component:可组合的graphql组件

    graphql-component使您可以通过graphql模式依赖关系树逐步构建模式。 储存库结构 lib -graphql组件代码。 test/examples/example-listing/property-component - Property的组件实现。 test/examples/example-...

    使用组合模式编写的实例

    使用组合模式有如下的好处: l 定义了饱含基本对象和组合对象的类层次结构,基本对象可以被组合成更复杂的组合对象,而这个组合对象有可以被组合。 简化客户代码 客户可以一直地使用组合结构和单个对象,通常用户不...

    ComponentOne Studio For ActiveX 2010 Vol1 cracked

    在您的应用程序中加入多列列表和组合框。 True DBList Pro 是一个由两个面向数据的ActiveX构件套,可为您的应用程序提供耐用的数据访问、数据表示和用户界面功能,使其能像网格一样运行,但却对列表有轻量化的要求。...

    json-schema-component:基于JSON模式的基于jQuery的编辑器组件呈现表单

    JSON模式组件该JavaScript库将根据给定的生成表单, 用(隐藏的)文本区域中包含的JSON数据结构填充表单, 将对表单的更改同步到数据结构,以及如果数据结构无法根据架构进行验证,则以表格形式显示验证错误。...

    树的构件与遍历---使用组合模式与迭代模式

    使用组合模式与迭代模式的结合构件树以及轻松遍历树的子节点示例

    C# 设计模式系列教程-组合模式

    组合模式使得用户对单个对象和组合对象的使用具有一致性。 2. 解决的问题  当希望忽略单个对象和组合对象的区别,统一使用组合结构中的所有对象(将这种“统一”性封装起来)。 3. 组合模式中的角色  3.1 组合部件...

    d3-component:D3.js的轻量级组件抽象

    可组合的(甚至是!)无状态功能组件。 可靠的destroy钩,用于清理物品。 与一起使用效果很好。 例子: | | | | 托多斯 | 时钟 机场时钟 example-viewer(Redux,ES6) 分形饼图(ES6) 使用此组件...

    深入剖析设计模式中的组合模式应用及在C++中的实现

    组合模式将对象组合成树形结构以表示“部分-整体”的层次结构。C o m p o s i t e 使得用户对单个对象和组合对象的使用具有一致性。 模式图: 适用场景: 你想表示对象的部分-整体层次结构。 你希望用户忽略组合...

    C++设计模式之组合模式(Composite)

    组合模式为了描述分支包含关系,也就是我们说的树形关系,其对象分为枝和叶,每一枝可包含枝和叶,直到全部为叶节点。我们对枝和叶进行行为抽象,可认为枝和叶都是Component,而叶是最小的操作单元,其下不存在枝和...

    PHP设计模式之组合模式定义与应用示例

    * 组合模式 * * 将对象组合成树形结构以表示部分-整体的层次结构,使得客户对单个对象和复合对象的使用具有一致性 */ abstract class MenuComponent { public function add($component) { } public function ...

    JavaScript设计模式开发中组合模式的使用教程

    我们平时开发过程中,一定会遇到...组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。 组合模式主要有三个角色: (1)抽象组件(Component)

    php设计模式 Composite (组合模式)

    php /** * 组合模式 * * 将对象组合成树形结构以表示”部分-整体”的层次结构,使得客户对单个对象和复合对象的使用具有一致性 */ abstract class MenuComponent { public function add($component){} public...

    java.awt.container源码中的组合设计模式

    java.awt.container #add(component) 是使用的组合设计模式。下面上两个类的代码。 public abstract class Component implements ImageObserver, MenuContainer, Serializable { boolean updateGraphicsData...

    [设计模式]装饰、组合、职责链模式

    组合模式:使得用户对单个对象和组合对象的使用具有一致性 责任链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个...

    C#设计模式结构型

    处理对象和对象容器,无需关心处理的是单个的对象,还是组合 的对象容器 – 将“客户代码与复杂的对象容器结构”解耦是合成模式的核心思 想,这样做之后,客户代码将与纯粹的抽象接口而非对象容器的 复杂内部实现...

    java高手真经 (UML建模+设计模式+面向服务架构) 卷6

    pattern/src/structure/proxy //12.3代理模式 pattern/src/structure/facade //12.4外观模式 pattern/src/structure/bridge //12.5桥接模式 pattern/src/structure/composite //12.6组合模式 pattern/src/structure/...

    lwc-modal:用于Salesforce.com(SFDC)的Lightning Web Components(LWC)系统的可访问,可组合模式

    闪电Web组件模态如何实现可组合模式LWC的示例,该模型可在任何项目中使用。 要阅读有关将它们组合在一起的整个博客文章,请参阅。 还将LWC中基于的组成的几个要点联系在一起,以及如何为Lightning Web Components...

    java高手真经 (UML建模+设计模式+面向服务架构) 卷8

    pattern/src/structure/proxy //12.3代理模式 pattern/src/structure/facade //12.4外观模式 pattern/src/structure/bridge //12.5桥接模式 pattern/src/structure/composite //12.6组合模式 pattern/src/structure/...

    ComponentOneStudio_2013v1.part06.rar

    大名鼎鼎的ComponentOne Studio 2013V1版全套控件,内包含ComponentOne Ultimate、ComponentOne Studio Enterprise、ComponentOne Studio for Windows Forms、ComponentOne Studio for ASP.NET Wijmo、ComponentOne ...

Global site tag (gtag.js) - Google Analytics