Witam!
Otóż staram się "rozpracować" wzorzec Prototype. Wygooglowałem taką stronę http://ashishkhandelwal.arkutil.com/?p=1089 i testuję... Napisałem taki oto programik w C# i chyba coś nie pasuje:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication11
{
abstract class Prototype : ICloneable
{
// cloning the prototype into a new instance
public abstract object Clone();
// some usefull action
public abstract void Action();
}
class Dziecko : Prototype
{
public string imie;
public string urodziny;
public Cechy cechy;
public Dziecko(string _urodziny)
{
urodziny = _urodziny;
}
public override object Clone()
{
return this.MemberwiseClone();
}
public override void Action()
{
Console.WriteLine("\nImie: "+imie+"\nUrodziny: "+urodziny);
cechy.Action();
}
}
class Cechy : Prototype
{
public string cecha;
public Cechy(string _cecha)
{
cecha = _cecha;
}
public override object Clone()
{
return this.MemberwiseClone();
}
public override void Action()
{
Console.WriteLine("Cecha: "+cecha);
}
}
class Program
{
static void Main(string[] args)
{
Dziecko ob1 = new Dziecko("11 stycznia");
ob1.cechy = new Cechy("Uczulony na mleko");
Dziecko ob2 = (Dziecko)ob1.Clone();
ob1.imie = "Tom";
ob2.imie = "Bob";
//ob1.cechy = new Cechy("Uczulona na pyłki");
ob1.Action();
Console.WriteLine("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
ob2.Action();
Console.ReadKey();
}
}
}
Jeśli dobrze zrozumiałem podany przeze mnie link, to obiekt "Cecha" nie powinien być sklonowany, a jednak jest... Czy może jednak ja jestem w błędzie?