当前位置: 首页 > news >正文

Composite Design Pattern

最近项目中用到了组合模式,自己系统地总结一下该模式:

组合模式(Composite Design Pattern)主要功能是能够使客户端(Client Code) 平等的对待单一组件(Single Componets)和容器组件(Container Componen)。从而实现客户端代码与组件之间的解耦。

从上图可以看出,Client依赖与抽象AComponet,该抽象可以是interface 或abstract类型。Leaf1,leaf2和Composite泛化于AComponent,同是Composite依赖于AComponet。习惯上称像Leaf1和leaf2这样的Single Componet为叶子组件,Composite这样的Container Componet为树枝,AComponet为根(root)。该模式主要用在容器组件递归调用。

下面给出具体例子,我们知道ASP.Net中控件都有Render方法,控件类型有容器类型(Panel,Form)与非容器类型(Button)。下面简单模拟Form显示窗体上所有控件。

首先,定义一个接口IControl,上图中的Root,代码如下:

ContractedBlock.gif ExpandedBlockStart.gifIControl  
1 namespace ComposityPattern
2 {
3    public interface IControl
4    {
5      void Render();
6    }
7 }

接下来实现Button类,相当于Leaf1

ContractedBlock.gif ExpandedBlockStart.gif Button
1 namespace ComposityPattern
2 {
3    public class Button : IControl
4    {
5      public void Render()
6      {
7        Console.WriteLine( " Button Rended " );
8      }
9    }
10 }

实现Form类,相当于Composite类

ContractedBlock.gif ExpandedBlockStart.gif Form
1 namespace ComposityPattern
2 {
3    public class Form : IControl
4    {
5      private List < IControl > controls = new List < IControl > ();
6
7      public void Add(IControl control)
8      {
9         controls.Add(control);
10     }
11
12     public void Remove(IControl control)
13     {
14        controls.Remove(control);
15     }
16
17     public void Render()
18     {
19        Console.WriteLine( " Form Rended " );
20
21    if (controls != null )
22    {
23      foreach (var control in controls)
24      {
25        control.Render();
26      }
27    }
28   }
29 }
30 }

Main函数(创建对象可以使用Abstract Factory Pattern)

ContractedBlock.gif ExpandedBlockStart.gif Main
1 namespace ComposityPattern
2 {
3    class Program
4    {
5      static void Main( string [] args)
6      {
7        Form form = new Form();
8        Button buttonInForm = new Button();
9
10       Console.WriteLine( " Container's Render Method excute. " );
11        form.Add(buttonInForm);
12          form.Render();
13
14           Console.WriteLine( " Single's Render Method excute. " );
15          Button button = new Button();
16          button.Render();
17
18          Console.ReadLine();
19        }
20    }
21 }

结果:

Container's Render Method excute.
Form Rended
Button Rended
Single's Render Method excute.
Button Rended

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/philwiki/articles/1985289.html

相关文章:

  • 网页绘制图表 Google Charts with JavaScript #1....好强、好简单啊!
  • NOIP 模拟17
  • HTML——MP4视频不能播放
  • 初始线性基
  • Semaphore的简介及应用场景
  • 织梦单独调用上下篇文档标题、链接、缩略图
  • vjudge Trailing Zeroes (III) (二分答案 数论)
  • 七夕过后,我依然单身,于是我用Python爬了你们单身的原因
  • NOIP 模拟19
  • GIT上传失败,报错信息:HTTP 413 curl 22 The requested URL returned error: 413
  • vscode——如何对MarkDown文件进行预览
  • VirtualEvn+jupyter
  • hibernate8
  • 如何成为一名专家级的开发人员
  • NOIP 模拟22
  • [译] React v16.8: 含有Hooks的版本
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 2019.2.20 c++ 知识梳理
  • codis proxy处理流程
  • isset在php5.6-和php7.0+的一些差异
  • java8 Stream Pipelines 浅析
  • SpiderData 2019年2月13日 DApp数据排行榜
  • 从伪并行的 Python 多线程说起
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 记录:CentOS7.2配置LNMP环境记录
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 面试总结JavaScript篇
  • 嵌入式文件系统
  • 如何进阶一名有竞争力的程序员?
  • 突破自己的技术思维
  • 我看到的前端
  • 项目实战-Api的解决方案
  • 一文看透浏览器架构
  • 如何用纯 CSS 创作一个货车 loader
  • ​iOS安全加固方法及实现
  • ​决定德拉瓦州地区版图的关键历史事件
  • #Z2294. 打印树的直径
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • (java)关于Thread的挂起和恢复
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (二)正点原子I.MX6ULL u-boot移植
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • (转)用.Net的File控件上传文件的解决方案
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .chm格式文件如何阅读
  • .net 4.0发布后不能正常显示图片问题
  • .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——