using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrototypePattern
{
public interface Product : ICloneable
{
void use(String s);
Product createClone();
}
class Manager
{
private Dictionary<String, Object> _Dictionary = new Dictionary<String, Object>();
public void register(String name, Product proto)
{
_Dictionary.Add(name, proto);
}
public Product create(String protoName)
{
Product _Product = (Product)_Dictionary[protoName];
return _Product.createClone();
}
}
class MessageBox : Product
{
private char _DecorationChar;
public Object Clone()
{
try
{
return (MessageBox)this;
}
catch (Exception e)
{
throw new Exception();
}
finally
{
//omitted
}
}
public MessageBox(char _DecorationChar)
{
this._DecorationChar = _DecorationChar;
}
public void use(string _String)
{
int length = Encoding.UTF8.GetBytes(_String).Length + 4;
for (int i = 0; i < length; i++) Console.Write(_DecorationChar);
Console.WriteLine("");
Console.WriteLine(_DecorationChar + " " + _String + " " + _DecorationChar);
for (int i = 0; i < length; i++) Console.Write(_DecorationChar);
Console.WriteLine("");
}
public Product createClone()
{
Product _Product = null;
try
{
_Product = (Product)Clone();
}
catch (Exception e)
{
e.StackTrace.ToString();
}
finally
{
//omitted
}
return _Product;
}
}
class UnderlinePen : Product
{
private char _UnderlineChar;
public Object Clone()
{
try
{
return (UnderlinePen)this;
}
catch (Exception e)
{
throw new Exception();
}
finally
{
//omitted
}
}
public UnderlinePen(char _UnderlineChar)
{
this._UnderlineChar = _UnderlineChar;
}
public void use(String _String)
{
int length = Encoding.UTF8.GetBytes(_String).Length;
Console.WriteLine("\"" + _String + "\"");
Console.Write(" ");
for (int i = 0; i < length; i++) Console.Write(_UnderlineChar);
Console.WriteLine("");
}
public Product createClone()
{
Product _Product = null;
try
{
_Product = (Product)Clone();
}
catch (Exception e)
{
e.StackTrace.ToString();
}
finally
{
//omitted
}
return _Product;
}
}
class PrototypePattern
{
static void Main(string[] args)
{
Manager _Manager = new Manager();
UnderlinePen _Pen = new UnderlinePen('*');
MessageBox _Box = new MessageBox('|');
_Manager.register("UnderlinePen", _Pen);
_Manager.register("MessageBox", _Box);
Product _PPen = _Manager.create("UnderlinePen");
_PPen.use("Practice");
Product _PBox = _Manager.create("MessageBox");
_PBox.use("Practice");
}
}
}
'Programming > Design Pattern' 카테고리의 다른 글
AdapterPattern (0) | 2016.11.09 |
---|---|
싱글턴 패턴 (0) | 2016.10.19 |
SingletonPattern (0) | 2016.10.19 |
BuilderPattern 실습 (0) | 2016.09.28 |