模式九(合成模式)

来源:互联网 发布:word怎么打矩阵 编辑:程序博客网 时间:2024/05/29 05:15

合成模式有时又叫做部分-整体模式,合成模式将对象组织到树结构中,可以用来描述整体与部分的关系,合成模式可以使客户端将单纯元素与复合元素同等看待。

 

对象的树结构:

一个树结构由两种结点组成:树枝结点和树叶结点,树枝结点可以有子结点,而一个树叶结点不可以有子结点,除了根结点外,其他结点有且只有一个父节点。

一个树枝结点可以不带任何叶子,但是它因为有带叶子的能力,因此仍然是树枝结点,而不会成为叶节点,一个树叶结点永远不可能带有子节点。

 

 

抽象构件角色:这是一个抽象角色,它给参与组合的对象规定一个接口,这个角色给出共有接口及其默认行为。

树叶构件角色:代表参加组合的树叶对象,一个树叶对象没有下级子对象。

树枝构件角色:代表参加组合的有子对象的对象,并给出树枝构件对象的行为。

 

其中Composite类型的对象可以包含其它Component类型的对象,换而言之,Composite类型对象可以含有其它的树枝类型或树叶类型的对象。

 

合成模式的实现根据所实现接口的区别分为两种形式,分别称为安全模式和透明模式。合成模式可以不提供父对象的管理方法,但合成模式必须在合适的地方提供子对象的管理方法。

 

透明方式

作为第一种选择,在Component里面声明所有的用来管理子类对象的方法,包括add(),remove(),以及getChild()方法。这样做的好处是所有的构件类都有相同的接口,在客户端看来,树叶类对象与合成类对象的区别起码在接口层次上消失了,客户端可以同等的对待所有的对象,这既是透明形式的合成模式。

 

这个选择的缺点是不够安全的,因为树叶类对象和合成类对象在本质上是有区别的,树叶类对象不可能有下一个层次的对象,因此add(),remove()以及getChild()方法没有意义,是在编译时期不会出错,而只会在运行时期才会出错。

 

安全方式

第二种选择是在Composite类里面声明所有的用来管理子类对象的方法,这样的做法是安全的做法,因为树叶类型的对象根本就没有管理子类对象的方法,因此,如果客户端对树叶类对象使用这些方法时,程序会在编译时期出错。

这个选择的缺点是不够透明,因为树叶类和合成类将具有不同的接口。

这两个形式各有优缺点,需要根据软件的具体情况做出取舍决定。

 

安全式的合成模式的结构

安全式的合成模式要求管理聚集的方法只出现在树枝构件类中,而不出现在树叶构件中。

 

抽象构件角色:这是一个抽象角色,它给参加组合的对象定义出公共的接口及其默认行为,可以用来管理所有的子对象,在安全式的合成模式里,构件角色并不是定义出管理子对象的方法,这一定义由树枝构件对象给出。
树叶构件角色:树叶对象是没有下级子对象的对象,定义出参见组合的原始对象的行为。
树枝构件角色:代表参加组合的有下级子对象的对象,树枝对象给出所有的管理子对象的方法,如add(),remove(),getChild()
安全式合成模式结构实例
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Simple_Factory
{
    abstract class Component
    {
        protected string name;
        public Component(string name)
        {
            this.name = name;
        }
        public abstract void Display(int depth);
    }
    class Composite : Component
    {
        private ArrayList children = new ArrayList();
        public Composite(string name) : base(name) { }
        public void Add(Component component)
        {
            children.Add(component);
        }
        public 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);
            }
        }
    }
    class Leaf:Component
    {
        public Leaf(string name) : base(name) { }
        public override void Display(int depth)
        {
            Console.WriteLine(new String('-', depth) + name);
        }
    }
    class Composite_Pattem
    {
        public static void Main()
        {
            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"));
            Leaf l = new Leaf("Leaf D");
            root.Add(l);
            root.Remove(l);
            root.Display(l);
        }
    }
}
透明式的合成模式实例

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Simple_Factory
{
    /// <summary>
    /// 透明式合成模式
    /// </summary>
    abstract class Component
    {
        protected string name;

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

        abstract public void Add(Component c);

        abstract public void Remove(Component c);

        abstract public void Display(int depth);
    }

    class Composite:Component
    {
        private ArrayList children = new ArrayList();

        public Composite(string name) : base(name) { }
       
        public override void Add(Component c)
        {
            children.Add(c);
        }

        public override void Remove(Component c)
        {
            children.Remove(c);
        }

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

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

    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);
        }
    }

    class Composite_PattemTwo
    {
        public static void Main()
        {
            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"));

            Leaf l = new Leaf("Leaf D");
            root.Add(l);

            root.Remove(l);

            root.Display(l);
        }
    }
}

 

使用合成模式时考虑的几个问题:

1,明显的给出父对象的引用,在子对象里面给出父对象的引用,可以很容易的遍历所有父对象,有了这个引用,可以方便的应用责任链模式。

2,在通常的系统里,可以使用享元模式实现构件的共享,但是由于合成模式的对象经常要有对父对象的引用,因此共享不容易实现。

3,有时候系统需要遍历一个树枝结构的子构件很多次,这时候可以考虑把遍历子构件的结果暂时存储在父构件里面作为缓存。

4,关于使用什么数据类型来存储子对象的问题,在示意性的代码中使用了ArrayList,在实际系统中可以使用其他聚集或数组等。

5,客户端尽量不要直接调用树叶类中的方法,而是借助其父类的多态性完成调用,这样可以增加代码的复用性。

原创粉丝点击