C#设计模式-概述
终极管理员 知识笔记 93阅读
's设计模式看似时尚,但却非常真实,因为它能让解决方案优雅、简单、可重用。设计模式似乎很神秘。事实上,设计模式只是在项目和程序之间重用面向对象代码的一种简单方式。背后的想法很简单。记录和编目公共对象之间的交互。对于武侠来说,这就像是套路,当然是经典或者MVC框架。该框架的设计目标是描述对象如何通信,而不涉及相互的数据模型和方法。
n style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">其实跟软件工程的高内聚低耦合的目标是一样的,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">这也模式追求的目标,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">也是面向对象追求的目标">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">刚才提到低耦合,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">就是分离类,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">包括封装和继承,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">对于继承,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">因为一个子类可以继承父类的所有方法和所有非私有变量,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">所以一个基类不写得太详细,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">这是设计模式的一条原则

“">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">针对接口编程,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">而不是针对实现编程”

">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">实际上,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">如果您做过winform">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">并使用继承的话,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">有时候你会感觉头痛,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">因为C#">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">只支持类的单继承,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">如果您继承的是窗体,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">那麻烦大了,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">如果基类有任何问题的,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">你的子窗体将不能重画显示
">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">设计模式的第二条原则是
">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">优先使用对象组合,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">而不是继承
">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">如何理解这句话,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">很简单,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">还是实现低耦合的目标,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">去年的一个项目消息系统中,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">就犯了这方面的错误,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">当时没看这书吧:)
">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">因为一个消息系统中包括底层socket,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">命令解释器,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">委托器,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">发送器,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">中间层以及GUI">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">等,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">如果这方面的关系不处理好,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">做出来的系统结构相当差
">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">如果您观察一下设计模式的例子,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">特别是观察者模式和中介者模式等,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">您会发现,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">它处处体现了上述两条设计模式的原则,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">请再次记住他们
1. ">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">针对接口编程,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">而不是针对实现编程
2. ">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">优先使用对象组合,">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">">-font-family: 'Times New Roman'">而不是继承