摘要: 应用场景:在软件的构建过程中,某些对象的状态在转换的过程中,可能由于某种的需要,要求程序能够回溯到以前的状态。Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.仅仅是在外部新增加一个类,然后在这个类中保存类的状态信息。实现代码: // 只是封装状态,而不封装其他的方法。 class Rectangle : ICloneable { int x; int y; 阅读全文
posted @ 2010-07-23 21:59 qiang.xu 阅读(167) 评论(0) 推荐(0) 编辑
摘要: 应用场景:Chain the receiving objects and pass the request along the chain until an object handles it.实现代码: class Request { } abstract class BaseHandler { public BaseHandler(BaseHandler next) { this.next = next; } // 下一个可能处理这个request的对象 // 这里相当于链表 private BaseHandler next; public BaseHandler Next { get { 阅读全文
posted @ 2010-07-23 20:47 qiang.xu 阅读(157) 评论(0) 推荐(0) 编辑
摘要: 应用场景:在软件的构建构成中,经常存在多个对象相互交互的情况,这样的话,一个类的改变的话,其他关联的对象都需要改变。解决的办法是在各个对象之间增加一个Mediator中介者,使用这个对象来管理这些对象的交互。实现代码:第一种:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Mediator{ class Program { static void Main(string[] args) { ChatRoom cr = new ChatRoom(); Parti 阅读全文
posted @ 2010-07-23 16:57 qiang.xu 阅读(173) 评论(0) 推荐(0) 编辑
摘要: 应用场景:Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.给定一个语言,定义它的发文的一种的表示,并定义一种解释器,使用这个解释器来解释这些句子。实现代码: class Program { static void Main(string[] args) { string roman = "MCMXXVIII"; Conte 阅读全文
posted @ 2010-07-23 14:48 qiang.xu 阅读(237) 评论(0) 推荐(0) 编辑
摘要: 应用场景:通常的情况下,直接new完一个类之后,然后调用这个类的办法,但是在实际的项目中存在需要存在“重做”等功能,这样就需要使用command模式来实现。实现代码: class Program { static void Main(string[] args) { User user = new User(); user.Compute('+', 100); user.Compute('-', 50); user.Compute('*', 10); user.Compute('/', 2) 阅读全文
posted @ 2010-07-23 14:21 qiang.xu 阅读(233) 评论(0) 推荐(0) 编辑