您的位置:首页 > 其它

【设计模式】——组合模式

2016-09-18 22:30 411 查看

前言

           从刚开始知道设计模式,到后来第一遍学习设计模式再到现在软考的学习,我们一步一步的走着。刚开始只是要求知道编者在模式中举的生活中的例子。关于代码部分也是一遍就过了。导致后来有人在提起设计模式都是一脸懵逼。这并不代表设计模式这方面就补不上了。前几天听了聚哥的讲解,感觉相见恨晚,所以在此总结一下那天讲课的内容。今天小编带大家领略的是设计模式之组合模式的奥秘!

内容

一、组合模式(Composite)

  概念:将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
  类图:



  现实应用:不同于UML图中的组合关系,组合模式其实就是整体与部分可以被一致对待的问题。比如公司和各个部门之间的关系就是部分与整体的关系。我们即可以在原有部门的基础上增加新的部门也可以删除部门。这就是组合模式在现实中的应用。

二、组合模式的代码实现

 Component.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.CompositePattern.Structural
{
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 Display(int depth);
}
}
  Leaf.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.CompositePattern.Structural
{
public class Leaf : Component
{
public Leaf(string name)
: base(name)
{
}

public override void Add(Component c)
{
Console.WriteLine("Cannot add to a leaf");
}

public override void Remove(Component c)
{
Console.WriteLine("Cannot remove from a leaf");
}

public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + _name);
}
}
}
  Composite.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.CompositePattern.Structural
{
public class Composite : Component
{
private List<Component> _children = new List<Component>();

public Composite(string name)
: base(name)
{
}

public override void Add(Component component)
{
_children.Add(component);
}

public override void Remove(Component component)
{
_children.Remove(component);
}

public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + _name);

foreach (Component component in _children)
{
component.Display(depth + 2);
}
}
}
}
  在客户端Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.CompositePattern.Structural
{
public class Client
{
static void Main(string[] args)
{
// Create a tree structure
Composite root = new Composite("root");
root.Add(new Leaf("Leaf A"));
root.Add(new Leaf("Leaf B"));

Composite comp = new Composite("Composite X");
comp.Add(new Leaf("Leaf XA"));
comp.Add(new Leaf("Leaf XB"));

root.Add(comp);
root.Add(new Leaf("Leaf C"));

// Add and remove a leaf
Leaf leaf = new Leaf("Leaf D");
root.Add(leaf);
root.Remove(leaf);

// Recursively display tree
root.Display(1);
}
}
}
运行结果:
-root
---Leaf A
---Leaf B
---Composite X
-----Leaf XA
-----Leaf XB
---Leaf C
请按任意键继续. . .

三、何时使用组合模式

  
  我们使用组合模式是为了解除耦合,当你发现需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象关于单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑用组合模式了。在Windows操作系统中也有很多的例子,比如曾经ASP.NET中的TreeView。还有系统中资源管理器。都是组合模式的应用。

四、组合模式的好处

  
  基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,这样不断地地柜下去。客户代码中,任何用到基本对象的地方都可以使用组合对象。简单点说组合模式让客户可以一致地使用组合结构和单个对象。在我的理解中,就是我们可以随意的扩充组合中的单个对象。

五、总结

 
   组合模式重要的是UML图的理解。一个高级开发要能看到UML图就能写出代码。师哥还讲了引用和引用在堆和栈中的参数传递。关于内存中的传址和传值问题,还是要学习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: