要深入学习面向对象,了解设计模式并在编程实践中不断实践是一条必经之路。之前读过《大话设计模式》,通过易懂的例子来说明每个设计模式的用法和效果,但是读完之后感觉很难在实际中运用,就是缺乏实践练习,没有深入思考,而且23个设计模式一下子接收起来很容易就弄混淆,而且对于适用场合也特别难以分清。最近一段时间开始重新学习,找来了《设计模式——可复用面向对象软件基础》这本被称为设计模式圣经的书,书的编排很好,每个模式按照意图、别名、动机、适用性、结构等12个方面依次阐述,非常适合不断思考和学习。
由于需要,我使用C#实现了一个抓取信息的类库,中间特别联系使用了Builder、Abstract factory模式,同时区分了Abstract factory和Factory method模式的区别。
首先,对于HttpWebRequest对象(产品)的构建使用了Builder模式,此模式适用性如下:
public class HttpRequestBuilder { private HttpWebRequest _httpRequest; public bool SetCookie(Dictionary<string,string> key_val) { if (key_val == null) { return false; } CookieContainer cc = new CookieContainer(100,20,4000); foreach(KeyValuePair<string,string> kvp in key_val) { Cookie cookie = new Cookie(kvp.Key,kvp.Value); cc.Add(cookie); } _httpRequest.CookieContainer = cc; return true; } public bool SetConnection(string connection) { if (string.IsNullOrEmpty(connection)) { return false; } _httpRequest.Connection = connection; return true; } public bool SetUA(string ua) { if (string.IsNullOrEmpty(ua)) { return false; } _httpRequest.UserAgent = ua; return true; } public bool SetReferer(string referer) { if (string.IsNullOrEmpty(referer)) { return false; } _httpRequest.Referer = referer; return true; } public bool SetMethod(string method = "GET") { if (string.IsNullOrEmpty(method)) { return false; } _httpRequest.Method = method; return true; } public bool SetTimeOut(int time) { if (time <= 0) { return false; } _httpRequest.Timeout = time; return true; } public void SendRequest(string url) { _httpRequest = (HttpWebRequest)WebRequest.Create(url); } public HttpWebRequest GetObject() { return _httpRequest; } }
class HttpRequestDirector { private HttpRequestBuilder hrb; /// <summary> /// Normal construct HttpWebRequest object, method by GET /// </summary> /// <param name="url"></param> /// <returns></returns> public HttpRequestBuilder NormalConstruct(string url) { hrb = new HttpRequestBuilder(); hrb.SetReferer("http://www.google.com"); hrb.SetTimeOut(1000 * 60 * 5); hrb.SendRequest(url); return hrb; } /// <summary> /// Strict(with cookies) construct HttpWebRequest object, method by GET /// </summary> /// <param name="url"></param> /// <returns></returns> public HttpRequestBuilder StrictConstruct(string url,Dictionary<string,string> cookies) { hrb = new HttpRequestBuilder(); hrb.SetConnection("keep-alive"); hrb.SetCache(DateTime.Now, true); hrb.SetCookie(cookies); hrb.SetMethod("GET"); hrb.SetReferer("http://www.google.com"); hrb.SetUA("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31"); hrb.SetTimeOut(1000 * 60 * 5); hrb.SendRequest(url); return hrb; } /// <summary> /// Post data /// </summary> /// <param name="url"></param> /// <returns></returns> public HttpRequestBuilder PostConstruct(string url) { hrb = new HttpRequestBuilder(); hrb.SetConnection("keep-alive"); hrb.SetMethod("POST"); hrb.SetReferer("http://www.google.com"); hrb.SetUA("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31"); hrb.SetTimeOut(1000 * 60 * 10); hrb.SendRequest(url); return hrb; } }