开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 1215|回复: 0
收起左侧

[C#源码] 设计模式(C#) - 装饰模式(Decorator Pattern)

[复制链接]

结帖率:61% (35/57)
发表于 2013-7-21 21:05:49 | 显示全部楼层 |阅读模式   海南省海口市
介绍
动态地给一个对象添加一些额外的职责。就扩展功能而言,它比生成子类方式更为灵活。
示例
有一个Message实体类,某个对象对它的操作有Insert()和Get()方法,现在扩展这个对象的功能。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;

  4. namespace Pattern.Decorator
  5. {
  6.   /**//// <summary>
  7.   /// Message实体类
  8.   /// </summary>
  9.   public class MessageModel
  10.   {
  11.     /**//// <summary>
  12.     /// 构造函数
  13.     /// </summary>
  14.     /// <param name="msg">Message内容</param>
  15.     /// <param name="pt">Message发布时间</param>
  16.     public MessageModel(string msg, DateTime pt)
  17.     {
  18.       this._message = msg;
  19.       this._publishTime = pt;
  20.     }

  21.     private string _message;
  22.     /**//// <summary>
  23.     /// Message内容
  24.     /// </summary>
  25.     public string Message
  26.     {
  27.       get { return _message; }
  28.       set { _message = value; }
  29.     }

  30.     private DateTime _publishTime;
  31.     /**//// <summary>
  32.     /// Message发布时间
  33.     /// </summary>
  34.     public DateTime PublishTime
  35.     {
  36.       get { return _publishTime; }
  37.       set { _publishTime = value; }
  38.     }
  39.   }
  40. }

  41. AbstractMessage

  42. using System;
  43. using System.Collections.Generic;
  44. using System.Text;

  45. namespace Pattern.Decorator
  46. {
  47.   /**//// <summary>
  48.   /// 操作Message的抽象构件(Component)
  49.   /// </summary>
  50.   public abstract class AbstractMessage
  51.   {
  52.     /**//// <summary>
  53.     /// 获取Message
  54.     /// </summary>
  55.     /// <returns></returns>
  56.     public abstract List<MessageModel> Get();

  57.     /**//// <summary>
  58.     /// 插入Message
  59.     /// </summary>
  60.     /// <param name="mm">Message实体对象</param>
  61.     /// <returns></returns>
  62.     public abstract bool Insert(MessageModel mm);
  63.   }
  64. }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表