原型模式:
把对象的创建权限关闭。提供一个公开的静态方法,提供全新的对象 ,不走构造函数,避免对象的重复创建,提高效率
多用于创建新对象很慢的时候,可以使用该方法,使用原型模式,内存复制模式创建新对象
public class yxmodel
{
private volatile static yxmodel _instance = null;//volatile 线程安全
private static readonly object Obj = new object();
private yxmodel()
{
// 业务初始化
}
public static yxmodel CreateInstance()
{
if (_instance == null)
{
lock (Obj)
{
if (_instance == null)
_instance = new yxmodel();
}
}
//和单例模式的区别地方。返回实例的的clone(); 全新对象 避免重复创建
yxmodel yxmodel = (yxmodel)_instance.MemberwiseClone();
return yxmodel;
}
}