预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点。示例保证一个类仅有一个实例。Singleton usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespacePattern.Singleton{/**////<summary>///泛型实现单例模式///</summary>///<typeparamname="T">需要实现单例的类</typeparam>publicclassSingleton<T>whereT:new(){/**////<summary>///返回类的实例///</summary>publicstaticTInstance{get{returnSingletonCreator.instance;}}classSingletonCreator{internalstaticreadonlyTinstance=newT();}}} Test usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;HYPERLINK"http://sucai.qqq80.com"http://sucai.qqq80.comusingPattern.Singleton;publicpartialclassSingleton:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){//使用单例模式,保证一个类仅有一个实例Response.Write(Singleton<Test>.Instance.Time);Response.Write("<br/>");Response.Write(Singleton<Test>.Instance.Time);Response.Write("<br/>");//不用单例模式Testt=newTest();Response.Write(t.Time);Response.Write("<br/>");Testt2=newTest();Response.Write(t2.Time);Response.Write("<br/>");}}publicclassTest{privateDateTime_time;publicTest(){System.Threading.Thread.Sleep(3000);_time=DateTime.Now;}HYPERLINK"http://liebiao.ttplay8.cn"http://liebiao.ttplay8.cnpublicstringTime{get{return_time.ToString();}}} 运行结果2007-2-1022:35:112007-2-1022:35:112007-2-1022:35:142007-2-1022:35:17 乐在其中设计模式(C#)-适配器模式(AdapterPattern) 介绍将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。示例有一个Message实体类,某个类对它的操作有Insert()和Get()方法。现在需要把这个类转到另一个接口,分别对应Add()和Select()方法。MessageModel usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespacePattern.Adapter{/**////<summary>///Message实体类///</summary>publicclassMessageModel{/**////<summary>///构造函数///</summary>///<paramname="msg">Message内容</param>///<paramname="pt">Message发布时间</param>publicMessageModel(stringmsg,DateTimept){this._message=msg;this._publishTime=pt;}privatestring_message;/**////<s