河北辛集住房和城乡建设厅网站,购车网站开发数据库er图,投票网站做seo如何,wordpress主题打开慢2012年4月18日 天气阴 天气灰蒙蒙的#xff0c;对于我们这种要为毕业做准备的人来说#xff0c;这天气舒服#xff0c;凉爽 中午睡了一个时后 打开电脑 突然感觉 眼睛不适应电脑屏幕的亮度#xff0c;就是最近眼睛看电脑太久了 不工作 了 呵呵 看来该休息一下… 2012年4月18日 天气阴 天气灰蒙蒙的对于我们这种要为毕业做准备的人来说这天气舒服凉爽 中午睡了一个时后 打开电脑 突然感觉 眼睛不适应电脑屏幕的亮度就是最近眼睛看电脑太久了 不工作 了 呵呵 看来该休息一下喽 好久没更新文章了 更新一个吧 单例模式 using System; using System.Collections.Generic; using System.Text; using System.IO; namespace 单例模式 { //单例模式介绍 //一个类在一个程序中只有一个对象 //类似于程序的配置文件一个程序中只能有一个配置文件多个的话程序 不知道在哪个文件去调 //应用 文件缓存管理器 class Program { static void Main(string[] args) { //Earth eanew Earth();//不能创建对象 /*Earth ea1Earth.getEarth(); Earth ea2Earth.getEarth(); Console.WriteLine(object.ReferenceEquals(ea1,ea2));//看是否是同一个对象 return true; */ //应用 文件缓存器 FileCacheManager fc FileCacheManager.instanse;//因为instanse是filecachemanager唯一的对象 所以其他类都只存这个对象的值 string sfc.ReadFile(c:\1.txt); Console.WriteLine(s); System.Threading.Thread.Sleep(8000);//为看到效果 在睡的8秒中 把文本文件 修改一下 看能否得到正确结果 s fc.ReadFile(c:\1.txt); Console.WriteLine(s); Console.ReadKey(); } } class Earth { //static的赋值语句运行一次在类的第一次加载的时候 private static Earth instanse new Earth();//2、声明一个静态字段初始化一个实例提供对象的唯一实例 private Earth()//1、把构造函数private(防止外部调用构造函数创建对象) { } public static Earth getEarth()//3、编写一个静态方法或者静态属性返回那个唯一的实例 { return instanse; } public int population { get; set; } } sealed class FileCacheManager//新建一个文件缓存管理器类 防止继承 用sealed关键字 { public static readonly FileCacheManager instanse new FileCacheManager();//设置一个只读的变量 存储该对象 Dictionarystring, CacheItem dic new Dictionarystring, CacheItem();//创建字典 private FileCacheManager() { } public string ReadFile(string filename) { if (dic.ContainsKey(filename))//判断文件 是否已读 { DateTime lasttime File.GetLastWriteTime(filename); if (lasttime dic[filename].lastwriteTime)//判断文件 是否修改 根据最后修改时间 { return dic[filename].content; } else return readtext(filename); } else { return readtext(filename); } } private string readtext(string filename)//读取文件 放到字典中 { string txt File.ReadAllText(filename); CacheItem ch new CacheItem(); ch.content txt; ch.lastwriteTime File.GetLastWriteTime(filename);//因为文件有可能被修改 为了获取的数据保持同步 所以保存他的最后修改时间 再去判断一下 dic[filename] ch;//写入字典 return txt; } } class CacheItem//用于保存缓存项 { public string content; public DateTime lastwriteTime; } }转载于:https://www.cnblogs.com/arno-zhang/archive/2012/04/18/5027557.html