Öncelikle ilişkili sınıflarımızı yazarsak:
public abstract class Product
{
public int deger
{
get;
set;
}
}
class ProductA:Product
{
}
class ProductB:Product
{
}
abstract class Creator
{
public abstract Product CreateProduct();
}
class CreatorA:Creator
{
public override Product CreateProduct()
{
return new ProductA();
}
}
class CreatorB:Creator
{
public override Product CreateProduct()
{
return new ProductB();
}
}
- Main Metot
static void Main(string[] args)
{
Product p;
Creator c;
String type = Console.ReadLine();
if (type == "ProductA")
{
c = new CreatorA();
p = c.CreateProduct();
}
else
{
c = new CreatorB();
p = c.CreateProduct();
}
Console.WriteLine(p.GetType().ToString());
Console.ReadLine();
}
Program burada Console'dan aldığı parametreye göre ilgili nesneyi üretir ve tipini console'a yazar.Creator sınıfı eğer default bir üretim olacaksa abstract olarak tanımlanmayabilir.Bunun yerine CreateProduct() metodu virtual olarak tanımlanır.Ayrıca pattern bu durumda pek mantıklı görünmeyebilir.Ancak her nesne yaratımında birçok işlem yapılabileceğini düşünürsek tüm bu ayrıntılar clienttan soyutlanmış olacaktır.
0 yorum:
Yorum Gönder