常用软件设计模式(五)组合模式

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

优点:

  1. 组合模式使得客户端代码可以一致地处理对象和对象容器,无需关系处理的单个对象,还是组合的对象容器。

  2. 将”客户代码与复杂的对象容器结构“解耦。

  3. 可以更容易地往组合对象中加入新的构件。

缺点: 使得设计更加复杂。客户端需要花更多时间理清类之间的层次关系。

树枝和树叶数量可以根据需求随意搭配,但是树枝可以有分支,树叶不能再有分支,所以这里有两种实现方式:透明方式和安全方式

透明组合模式:在Component中声明所有来管理子对象的方法,其中包括Add,Remove等。这样实现Component接口的所有子类都具备了Add和Remove方法。这样做的好处是叶节点和枝节点对于外界没有区别,它们具备完全一致的接口。

缺点:客户端对叶节点和枝节点是一致的,但叶节点并不具备Add和Remove的功能,因而对它们的实现是没有意义的

/// 一个抽象构件,声明一个接口用于访问和管理Component的子部件
public abstract class Component
{
    protected string name;
    public Component(string name)
    {
       this.name = name;
    }
     
    /// 增加一个节点
     public abstract void Add(Component component);

     /// 移除一个节点
     public abstract void Remove(Component component);

    /// 显示层级结构
    public abstract void Display(int level);
}

 /// 叶子节点
public class Leaf : Component
{

    public Leaf(string name)

        : base(name)  { }
/// 由于叶子节点没有子节点,所以Add和Remove方法对它来说没有意义,但它继承自Component,这样做可以消除叶节点和枝节点对象在抽象层次的区别,它们具备完全一致的接口。
    public override void Add(Component component)
    {
        Console.WriteLine("Can not add a component to a leaf.");
    }


    /// 实现它没有意义,只是提供了一个一致的调用接口
    public override void Remove(Component component)
    {
        Console.WriteLine("Can not remove a component to a leaf.");
    }

    public override void Display(int level)
    {
      Console.WriteLine(new string('-', level) + name);
    }
}


/// 定义有枝节点的行为,用来存储部件,实现在Component接口中对子部件有关的操作
public class Composite : Component
{
    public Composite(string name)

        : base(name){ }

     /// 一个子对象集合,用来存储其下属的枝节点和叶节点
     private List<Component> children = new List<Component>();

     /// 增加子节点
     public override void Add(Component component)
    {
        children.Add(component);
    }

 
    /// 移除子节点
    public override void Remove(Component component)
    {
        children.Remove(component);
    }

 

    public override void Display(int level)
    {
        Console.WriteLine(new string('-', level) + name);
        // 遍历其子节点并显示

        foreach (Component component in children)
        {
           component.Display(level + 2);
        }
    }
}

 

/// 调用

static void Main(string[] args)
{

    // 生成树根,并为其增加两个叶子节点
    Component root = new Composite.Composite("Root");
    root.Add(new Leaf("Leaf A in Root"));
    root.Add(new Leaf("Leaf B in Root"));

    // 为根增加两个枝节点
    Component branchX = new Composite.Composite("Branch X in Root");
    Component branchY = new Composite.Composite("Branch Y in Root");
    root.Add(branchX);
    root.Add(branchY);
 
    // 为BranchX增加页节点
    branchX.Add(new Leaf("Leaf A in Branch X"));

    // 为BranchX增加枝节点
    Component branchZ = new Composite.Composite("Branch Z in Branch X");
    branchX.Add(branchZ);
 
    // 为BranchY增加叶节点
    branchY.Add(new Leaf("Leaf in Branch Y"));

    // 为BranchZ增加叶节点
    branchZ.Add(new Leaf("Leaf in Branch Z"));

    // 显示树
    root.Display(1);

}

安全组合模式:在Component中不去声明Add和Remove方法,那么子类的Leaf就不需要实现它,而是在Composit声明所有用来管理子类对象的方法。

缺点:叶节点无需在实现Add与Remove这样的方法,但是对于客户端来说,必须对叶节点和枝节点进行判定,为客户端的使用带来不便。

public abstract class Component
{
    protected string name;
    public Component(string name)
    {
        this.name = name;
    }

    /// 显示层级结构
    public abstract void Display(int level);
}

 /// 叶子节点
public class Leaf : Component
{
    public Leaf(string name)
        : base(name){ }

    public override void Display(int level)
    {
        Console.WriteLine(new string('-', level) + name);
    }

}

 /// 定义有枝节点的行为,用来存储部件,实现在Component接口中对子部件有关的操作

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

     /// 一个子对象集合,用来存储其下属的枝节点和叶节点
     private List<Component> children = new List<Component>();

    /// 增加子节点
    public void Add(Component component)
    {
        children.Add(component);
    }


    /// 移除子节点
     public void Remove(Component component)
    {
        children.Remove(component);
    }

 

    public override void Display(int level)
    {
        Console.WriteLine(new string('-', level) + name);
        // 遍历其子节点并显示
        foreach (Component component in children)
        {
            component.Display(level + 2);
        }
    }
}

 
static void Main(string[] args)
{
    // 生成树根,并为其增加两个叶子节点
    Composite.Composite root = new Composite.Composite("Root");
    root.Add(new Leaf("Leaf A in Root"));
    root.Add(new Leaf("Leaf B in Root"));
 
    // 为根增加两个枝节点
    Composite.Composite branchX = new Composite.Composite("Branch X in Root");
    Composite.Composite branchY = new Composite.Composite("Branch Y in Root");
    root.Add(branchX);
    root.Add(branchY);

    // 为BranchX增加页节点
    branchX.Add(new Leaf("Leaf A in Branch X"));

    // 为BranchX增加枝节点
    Composite.Composite branchZ = new Composite.Composite("Branch Z in Branch X");
    branchX.Add(branchZ);

    // 为BranchY增加叶节点
    branchY.Add(new Leaf("Leaf in Branch Y"));

    // 为BranchZ增加叶节点
    branchZ.Add(new Leaf("Leaf in Branch Z"));

    // 显示树
    root.Display(1);

}

猜你喜欢

转载自blog.csdn.net/Chenrongsake/article/details/83115627