现在的位置: 首页 > 综合 > 正文

设计模式–组合模式

2012年09月15日 ⁄ 综合 ⁄ 共 2104字 ⁄ 字号 评论关闭

定义:

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

关键词:

Composite

结构图:

image

应用场景:

表达的对象成"整体-部分"层次结构时.

即表达的对象最后结果承树形时,可以采用组合模式.

例如:

中国移动下属3个品牌:全球通,神州行,动感地带.

同时,中国移动下属有北京移动,天津移动,江苏移动等分公司.下属各分公司又包含3个品牌.

各分公司又含有下属单位及相关品牌.

这种树状结构的层次使用组合模式.

实际上,品牌和公司并不是一个类可以描述的,但是为了使客户处理起来方便,可以不分复杂元素和简单元素,也可以使之如此设计.

例子:

类图:

image

代码:
public abstract class Mobile
{
    protected string name;
	public Mobile(string name)
	{
        this.name = name;
	}

    public abstract void Add(Mobile m);

    public abstract void Remove(Mobile m);

    public abstract void Duty(int depth);
}

public class Easyown : Mobile
{

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

    public override void Add(Mobile m)
    {
        
    }

    public override void Remove(Mobile m)
    {
       
    }

    public override void Duty(int depth)
    {
        HttpContext.Current.Response.Write(new string('-', depth) + name + "(轻松由我,神州行!)<br/>");
    }
}

public class M_Zone : Mobile
{
    public M_Zone(string name)
        : base(name)
	{
        
	}

    public override void Add(Mobile m)
    {
        
    }

    public override void Remove(Mobile m)
    {
        
    }

    public override void Duty(int depth)
    {
        HttpContext.Current.Response.Write(new string('-', depth) + name + "(我的地盘,听我的!)<br/>");
    }
}

public class GoTone : Mobile
{
    public GoTone(string name)
        : base(name)
	{
        
	}

    public override void Add(Mobile m)
    {
        
    }

    public override void Remove(Mobile m)
    {
        
    }

    public override void Duty(int depth)
    {
        HttpContext.Current.Response.Write(new string('-', depth) + name + "(我能!)<br/>");
    }
}

public class MobileCompany : Mobile
{
    List<Mobile> al = new List<Mobile>(10);

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

    public override void Add(Mobile m)
    {
        al.Add(m);
    }

    public override void Remove(Mobile m)
    {
        al.Remove(m);
    }

    public override void Duty(int depth)
    {
        HttpContext.Current.Response.Write(new string('-', depth) + name + "<br/>");

        foreach(Mobile m in al)
        {
            m.Duty(depth+2);
        }
    }
}
页面调用:
    protected void Page_Load(object sender, EventArgs e)
    {
        Mobile m1 = new MobileCompany("中国移动");

        m1.Add(new Easyown("神州行"));
        m1.Add(new M_Zone("动感地带"));
        m1.Add(new GoTone("全球通"));

        Mobile m2 = new MobileCompany("江苏移动");
        m2.Add(new Easyown("神州行"));
        m2.Add(new M_Zone("动感地带"));
        m2.Add(new GoTone("全球通"));

        Mobile m3 = new MobileCompany("四川移动");
        m3.Add(new Easyown("神州行"));
        m3.Add(new M_Zone("动感地带"));
        m3.Add(new GoTone("全球通"));

        Mobile m31 = new MobileCompany("成都移动");
        m31.Add(new Easyown("神州行"));
        m31.Add(new M_Zone("动感地带"));
        m31.Add(new GoTone("全球通"));

        m3.Add(m31);

        m1.Add(m2);
        m1.Add(m3);

        m1.Duty(0);
    }
效果:
image 
【上篇】
【下篇】

抱歉!评论已关闭.