using System.Collections.Generic;
namespace PrototypePattern.framework
{
public interface ICopyable
{
ICopyable createClone();
}
public class CloneManager
{
private static volatile CloneManager uniqueInstance = null;
protected CloneManager()
{
}
public static CloneManager Instance
{
get
{
if (uniqueInstance == null)
{
lock (typeof(CloneManager))
{
if (uniqueInstance == null)
uniqueInstance = new CloneManager();
}
}
return uniqueInstance;
}
}
private Dictionary<PrototypeID, ICopyable> showcase = new Dictionary<PrototypeID, ICopyable>();
/// <summary>
/// 등록
/// </summary>
/// <param name="id"></param>
/// <param name="prototype"></param>
public void Register(PrototypeID id, ICopyable prototype)
{
showcase.Add(id, prototype); //새 객체 Add
}
/// <summary>
/// 생성 및 가져오기
/// </summary>
/// <param name="prototypeID"></param>
/// <returns></returns>
public ICopyable Create(PrototypeID prototypeID)
{
return (ICopyable)showcase[prototypeID];
}
/// <summary>
/// 삭제
/// </summary>
/// <param name="prototypeID"></param>
public void Remove(PrototypeID prototypeID)
{
showcase.Remove(prototypeID);
}
/// <summary>
/// 재생성
/// </summary>
/// <param name="prototypeID"></param>
/// <returns></returns>
public ICopyable Recreate(PrototypeID prototypeID)
{
Remove(prototypeID);
return Create(prototypeID);
}
/// <summary>
/// 덮어쓰기
/// </summary>
/// <param name="prototypeID"></param>
/// <param name="prototype"></param>
public void OverWrite(PrototypeID prototypeID, ICopyable prototype)
{
Remove(prototypeID);
Register(prototypeID, prototype);
}
}
public enum PrototypeID
{
Original,
Copy,
Edit
}
}