| Tony Huang's profileSouline PLUS planBlogListsNetwork | Help |
|
29 July 初试C#中的Generic : 利用Generic实现Singleton在应用Singleton模式的时候,往往会重复、单调地写类似的代码,为什么不用Generic来简化呢?
// ConstructorNotFoundException.cs
namespace Utility
{ public class ConstructorNotFoundException : Exception { } } // Singleton.cs
using System;
using System.Collections.Generic; using System.Text; namespace Utility
{ public class Singleton<ObjectType> { private static bool m_InstanceCreated = false; private static ObjectType m_Instance; public static ObjectType Instance()
{ if (!m_InstanceCreated) { Type objObjectType = typeof(ObjectType); System.Reflection.ConstructorInfo objCon = objObjectType.GetConstructor(Type.EmptyTypes); if (objCon != null) { m_Instance = (ObjectType)objCon.Invoke(new Object[0]); m_InstanceCreated = true; } else { throw new ConstructorNotFoundException(); } } return m_Instance; } } } 28 July 初试C# 2.0中的Generic : 实现简单的ObjectCache此处ObjectCache的由来:
实际使用过程中往往要讲xml数据解析成对象,这是一个非常消耗cpu的过程,所以引入cache机制,减少cpu的运算量,加快反应速度。是一种以空间换时间方法。
// CacheTimeoutException.cs
namespace Souline.LibCache
{
public class CacheTimeoutException : Exception
{
}
}
// CacheNotFoundException.cs
namespace Souline.LibCache
{
public class CacheNotFoundException : Exception
{
}
}
// ObjectCacheItem.cs
namespace Souline.LibCache
{
public class ObjectCacheItem<ObjectType>
{
private string m_Key;
private DateTime m_CacheTime;
private ObjectType m_Object;
public ObjectCacheItem(string A_Key, ObjectType A_Object)
{
this.m_Key = A_Key;
this.m_Object = A_Object;
this.m_CacheTime = DateTime.Now;
}
public string Key
{
get
{
return this.m_Key;
}
}
public DateTime CacheTime
{
get
{
return this.m_CacheTime;
}
}
public ObjectType Object
{
get
{
return this.m_Object;
}
set
{
this.m_CacheTime = DateTime.Now;
this.m_Object = value;
}
}
}
}
// ObjectCache.cs
using System.Collections.Generic;
namespace Souline.LibCache
{
public class ObjectCache<ObjectType>
{
private Dictionary<string, ObjectCacheItem<ObjectType>> m_Cache = new Dictionary<string, ObjectCacheItem<ObjectType>>();
private int m_Timeout;
public ObjectCache(int A_Timeout)
{
this.m_Timeout = A_Timeout;
}
public bool Cached(string strKey)
{
return this.m_Cache.ContainsKey(strKey);
}
public ObjectType GetItem(string strKey)
{
if(!this.Cached(strKey))
{
throw new CacheNotFoundException();
}
if(this.CacheTime.AddSeconds(this.m_Timeout) < DateTime.Now)
{
throw new CacheTimeoutException();
}
return this.m_Cache[strKey].Object;
}
public void Update(string strKey, ObjectType objItem)
{
ObjectCacheItem<ObjectType> objCacheItem = null;
if(!this.Cached(objItem))
{
objCachedItem = new ObjectCacheItem<ObjectType>(strKey, objItem);
this.m_Cache.Add(strKey, objCachedItem);
}
else
{
objCachedItem = this.m_Cache[strKey];
objCachedItem.Object = objItem;
}
}
}
}
09 July 不愿成为加西莫多我不愿意成为加西莫多
不是因为加西莫多很丑
只是因为不希望像他一样
悲惨地只在死神即将降临的时候,自己的爱人才知道他才是真正爱她的人
我不愿意成为加西莫多
不是因为加西莫多很丑
只是因为不希望像他一样
不过成为一个傻乎乎地殉情的人,在死后才得以与爱人在一起。
曾经在报纸上看到:
爱情就好像跷跷板,这个时候他在上面,或许下一个时候就换你在上面了。
我对它笑笑,说这个大概就是光棍们互相勉励的言谈吧。又联想到陈小春的歌:
爱情这东西没道理的,有人很强手,有人每资格。
这说的是跷跷板的一半吧
都不知道自己说的什么,总结一下:爱情太奇怪。 |
|
|