组合模式

目录

一、组合模式

二、组合模式角色

三、代码实例一

四、代码实例二

 五、代码实例三

UML图:


一、组合模式

1、组合模式又叫做部分-整体角色。组合模式将对象整合到树结构中,描述部分和整体关系。

本质是统一了叶子对象和组合对象。

2、使用场景

体现部分与整体结构层次时

希望忽略组合对象和单个对象的不同,统一使用组合结构中所有对象。

3、优点

1、符合开放-封闭原则

2、统一了叶子对象和组合对象

3、简化了客户端调用,不再区分叶子和树枝节点

4、定义了包含基本对象和组合对象的类层次结构

5、更容易扩展,新定义节点或树枝对象就好

4、缺点

难以限制组合中组件类型

5、本质是统一组合对象和叶子对象

组合模式分为透明和不透明。透明是所有构件都有共同接口。

优点:

客户端看来,所有组合对象和叶子对象在层次上没区别,可同等对待所有对象。

缺点:

不够安全,叶子类对象不会有下一层次。

二、组合模式角色

1、抽象构件,抽象角色,给组合的所有对象规定一个接口。

2、树叶构件,最下层对象。

3、树枝构件,参加组合的、有子对象的对象。

 

三、代码实例一

第一个类是抽象构件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Composite
{
    abstract class Component
    {
        protected string name;

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

        public abstract void Add(Component c);
        public abstract void Del(Component c);
        public abstract void Depth(int depth);
    }
}

第二个类是树枝构件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Composite
{
    class Composite : Component
    {
        private List<Component> child = new List<Component>();

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

        public override void Add(Component c)
        {
            child.Add(c);
        }

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

        public override void Depth(int depth)
        {
            Console.WriteLine(new string('-', depth) + name);
            foreach (Component c in child)
            {
                c.Depth(depth + 2);
            }
        }
    }
}

第三个是树叶构件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Composite
{
    class Leaf : Component
    {
        public Leaf(string name) : base(name) { }
        public override void Add(Component c)
        {
            Console.WriteLine("can not add!");
        }

        public override void Del(Component c)
        {
            Console.WriteLine("can not delete!");
        }

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

 测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Composite
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Composite root = new Composite("root");
            root.Add(new Leaf("leaf1"));
            root.Add(new Leaf("leaf2"));
            root.Depth(1);
            Console.WriteLine("-------------------");
            Composite root1=new Composite("root1");
            root1.Add(new Leaf("leaf11"));
            root1.Add(new Leaf("leaf22"));
            root.Add(root1);
            root.Depth(1);
            Console.WriteLine("-------------------");
            Composite root2=new Composite("root2");
            root2.Add(new Leaf("leaf111"));
            root2.Add(new Leaf("leaf222"));
            root1.Add(root2);
            root.Add(new Leaf("ooo"));
            root.Depth(1);
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
        }
    }
}

结果:

四、代码实例二

现在需要管理商品类别树,假设要求能实现输出如上商品类别树的结构功能,应该如何实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompositeOne
{
    abstract class Component
    {
        protected string name;

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

        public abstract void Add(Component c);
        public abstract void Del(Component c);
        public abstract void Show(int depth);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompositeOne
{
    class Composite : Component
    {
        private List<Component> child = new List<Component>();
        public Composite(string name) : base(name) { }
        public override void Add(Component c)
        {
            child.Add(c);
        }

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

        public override void Show(int depth)
        {
            Console.WriteLine(new string('-', depth) + name);
            foreach (Component c in child)
            {
                c.Show(depth + 2);
            }
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompositeOne
{
    class Leaf : Component
    {
        public Leaf(string name) : base(name) { }
        public override void Add(Component c)
        {
            Console.WriteLine("cann not!");
        }

        public override void Del(Component c)
        {
            Console.WriteLine("cann not!");
        }

        public override void Show(int depth)
        {
            Console.WriteLine(new string('-', depth) + name);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cout;
namespace CompositeOne
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Composite root1 = new Composite("男装");
            root1.Add(new Leaf("衬衫"));
            root1.Add(new Leaf("西装"));
            root1.Show(1);
            Composite root2 = new Composite("女装");
            root2.Add(new Leaf("裙子"));
            root2.Add(new Leaf("套装"));
            root2.Show(1);
            root1.Add(root2);
            root1.Show(1);
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
        }
    }
}

 五、代码实例三

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompositeThree
{
    abstract class Company
    {
            protected string name;

            protected Company(string name)
            {
                this.name = name;
            }

            public abstract void Add(Company c);
            public abstract void Del(Company c);
            public abstract void Show(int depth);
            public abstract void LineDuty();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompositeThree
{
    class ConcreteCompany : Company
    {
        private List<Company> child = new List<Company>();
        public ConcreteCompany(string name) : base(name) { }
        public override void Add(Company c)
        {
            child.Add(c);
        }

        public override void Del(Company c)
        {
            child.Remove(c);
        }

        public override void LineDuty()
        {
            foreach (Company c in child)
                c.LineDuty();
        }

        public override void Show(int depth)
        {
            Console.WriteLine(new string('*', depth) + name);
            foreach(Company c in child)
            {
                c.Show(depth+2);
            }
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompositeThree
{
    class FinanceDepartment : Company
    {
        public FinanceDepartment(string name) : base(name) { }
        public override void Add(Company c)
        {
            throw new NotImplementedException();
        }

        public override void Del(Company c)
        {
            throw new NotImplementedException();
        }

        public override void LineDuty()
        {
            Console.WriteLine("{0},财政管理", name);
        }

        public override void Show(int depth)
        {
            Console.WriteLine(new string('-', depth) + name);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompositeThree
{
    class HRDepartment : Company
    {
        public HRDepartment(string name) : base(name) { }
        public override void Add(Company c)
        {
            throw new NotImplementedException();
        }

        public override void Del(Company c)
        {
            throw new NotImplementedException();
        }

        public override void LineDuty()
        {
            Console.WriteLine("{0},人力资源管理", name);
        }

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

 测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompositeThree
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            ConcreteCompany root = new ConcreteCompany("北京总公司");
            root.Add(new HRDepartment("总公司人力资源部门"));
            root.Add(new FinanceDepartment("总公司财政部门"));
            ConcreteCompany root1 = new ConcreteCompany("上海分公司");
            root1.Add(new HRDepartment("上海分公司人力资源部门"));
            root1.Add(new FinanceDepartment("上海分公司财政部门"));
            root.Add(root1);
            Console.WriteLine("结构体:");
            root.Show(1);
            Console.WriteLine("职责:");
            root.LineDuty();
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
        }
    }
}

UML图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值