Prototype Pattern (2010-03-15)
Protype pattern is a pattern for produce new objects from a instance, of the same class, basically clonning objects.
For implement prototype pattern in c#, class must implement ICloneable interface ขออนุญาติไม่แปรนะครับ กัวจะแปลแล้วเข้าใจยากหนักกว่าเดิม
namespace Prototype
{
public class MyPrototype : ICloneable
{
public String myName;
public MyPrototype(String myName)
{
this.myName = myName;
}
public object Clone()
{
MyPrototype newObj = new MyPrototype(this.myName);
newObj.myName = this.myName;
return newObj;
}
}//class
}//Prototype |
การเรียกใช้นะครับ
MyPrototype obj1 = new MyPrototype("Chiwa Kantawong");
MyPrototype obj2 = (MyPrototype) obj1.Clone();
MessageBox.Show(obj1.myName + " <===> " + obj2.myName); |
|