原型模式(Prototype Pattern):是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式;
1: 专门用来快速生产对象!
2: 把单例实例对象在内存中拷贝一份
3: 避免单列对象污染
实例
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace SingletonPattern
{
/// <summary>
///原型模式
/// </summary>
public class SingletonFifth
{
public int Id { get; set; }
public string Name { get; set; }
/// <summary>
/// 1.饿汉式写法
/// </summary>
private static SingletonFifth singleton = new SingletonFifth(); //静态,只执行一次
/// <summary>
/// 2.私有化构造函数
/// </summary>
private SingletonFifth()
{
Console.WriteLine($"{typeof(SingletonFifth).Name}被构造。。。");
}
/// <summary>
/// 懒汉式学习 (MemberwiseClone()基于内存克隆)
/// </summary>
/// <returns></returns>
public static SingletonFifth CreateInstance()
{
return (SingletonFifth)singleton.MemberwiseClone(); //基于内存克隆
}
public static void Test()
{
Console.WriteLine("this is Test...");
}
}
}