博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#设计模式系列:组合模式(Composite)
阅读量:7172 次
发布时间:2019-06-29

本文共 8038 字,大约阅读时间需要 26 分钟。

1、组合模式简介

1.1>、定义

  组合模式主要用来处理一类具有“容器特征”的对象——即它们在充当对象的同时,又可以作为容器包含其他多个对象。

1.2>、使用频率

   中高

2、组合模式结构图

2.1>、结构图

2.2>、参与者

  组合模式参与者:

  ◊ Component

    ° 声明组合中对象的接口;

    ° 实现全部类中公共接口的默认行为;

    ° 声明访问和管理子类的接口;

    ° (可选择)定义接口提供在递归结构中访问父类。

  ◊ Leaf

    ° 表示在组合对象中叶子节点对象,没有子节点;

    ° 定义组合对象中的初始行为。

  ◊ Composite

    ° 定义Component子类的行为;

    ° 保存Component子类;

    ° 实现Component接口的子类关联操作。

  ◊ Client

    ° 通过Component接口组合多个对象。

3、组合模式结构实现

  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
_children = new List
(); 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.cs:

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请按任意键继续. . .

4、组合模式实践应用

  Shape.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.CompositePattern.Practical{    public abstract class Shape    {        protected string _name;        public Shape(string name)        {            this._name = name;        }        ///         /// 面积        ///         /// 
public abstract double Area(); /// /// 显示 /// public abstract void Display(); }}

  Circle.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.CompositePattern.Practical{    ///     /// 圆形    ///     public class Circle : Shape    {        private double _radius;        public Circle(string name, double radius)            : base(name)        {            this._radius = radius;        }        public override double Area()        {            return Math.Round(Math.PI * _radius * _radius, 2);        }        public override void Display()        {            Console.WriteLine("{0} 半径:{1},面积:{2}", _name, _radius, this.Area());        }    }}

  Rectangle.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.CompositePattern.Practical{    ///     /// 矩形    ///     public class Rectangle : Shape    {        private double _width;        private double _height;        public Rectangle(string name, double width, double height)            : base(name)        {            this._width = width;            this._height = height;        }        public override double Area()        {            return _width * _height;        }        public override void Display()        {            Console.WriteLine("{0} 长:{1},宽:{2},面积:{3}", _name, _width, _height, this.Area());        }    }}

  Triangle.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.CompositePattern.Practical{    ///     /// 三角形    ///     public class Triangle : Shape    {        private double _a;        private double _b;        private double _c;        ///         /// 三角形构造函数        /// 参数:三角形名称和三条边长        ///         /// 三角形名称        /// 第一条边长        /// 第二条边长        /// 第三条边长        public Triangle(string name, double a, double b, double c)            : base(name)        {            _a = a;            _b = b;            _c = c;        }        public override double Area()        {            double p = (_a + _b + _c) / 2;            return Math.Sqrt(p * (p - _a) * (p - _b) * (p - _c));        }        public override void Display()        {            Console.WriteLine("{0} 三条边长:{1},{2},{3},面积:{3}", _name, _a, _b, _c, this.Area());        }    }}

  Graphics.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.CompositePattern.Practical{    public class Graphics : Shape    {        private List
_children = new List
(); public Graphics(string name) : base(name) { } public override double Area() { double sum = 0; foreach (Shape child in _children) { sum += child.Area(); } return sum; } public override void Display() { foreach (Shape child in _children) { child.Display(); } Console.WriteLine("{0} 总面积:{1}", _name, this.Area()); } public void Add(Shape child) { _children.Add(child); } public void Remove(Shape child) { _children.Remove(child); } }}

  Client.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DesignPatterns.CompositePattern.Practical{    public class Client    {        static void Main(string[] args)        {            Graphics graphics = new Graphics("全部图形");            Circle circle = new Circle("圆形", 5);            graphics.Add(circle);            Rectangle rectangle = new Rectangle("矩形", 4, 5);            graphics.Add(rectangle);            Triangle triangle = new Triangle("三角形", 3, 4, 5);            graphics.Add(triangle);            graphics.Display();        }    }}

  运行结果:

圆形 半径:5,面积:78.54矩形 长:4,宽:5,面积:20三角形 三条边长:3,4,5,面积:5全部图形 总面积:104.54请按任意键继续. . .

5、组合模式应用分析

  组合模式可以适用以下情形:

  ◊ 希望把对象表示成部分—整体层次结构;

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

  组合模式具有以下特点:

  ◊ 定义了包含基本对象和组合对象的类层次结构。基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,不断的递归下去。客户代码中,任何用到基本对象的地方都可以使用组合对象;

  ◊ 简化客户代码。客户可以一致地使用组合结构和单个对象。这样用户就不必关心处理的是一个叶子节点还是一个组合组件,从而简化了客户代码;

  ◊ 使得新增类型的组件更加容易。新定义的Composite或Leaf子类自动地与已有的结构和客户代码一起协同工作,客户程序不需因新的Component类而改变。

6、参考资料

转载于:https://www.cnblogs.com/libingql/p/3496345.html

你可能感兴趣的文章
磁盘分区和目录结构
查看>>
grep、egrep、fgrep及正则表达式
查看>>
我的友情链接
查看>>
熟悉dstat命令用法
查看>>
kali linux字体渲染和infinality安装配置
查看>>
高效出去List集合和数组中的重复元素
查看>>
我的友情链接
查看>>
grep常见选项及命令
查看>>
nodejs终端的坑--新手向
查看>>
pycharm修改默认__author__ = '$USER'
查看>>
DNS服务的原理与配置(详细图文教程)
查看>>
threejs 之box贴图--6个 面集中在一个图片上
查看>>
Exchange Server 2010安装测试
查看>>
设计模式第九课 策略模式
查看>>
给定两个排序后的数组A和B,其中A的末端有足够的缓冲空间容纳B。将B合并入A并排序...
查看>>
FFmpeg avcodec_version函数使用
查看>>
一步步手动构建小于10M的类嵌入式Linux系统
查看>>
saltstack细节要点
查看>>
端口回流与dns-map与域内NAT
查看>>
linux 中特殊符号用法详解
查看>>