Abstract Factory Tasarım Kalıbı
Abstract Factory tasarım kalıbı, aralarında ilişki bulunan
sınıfların nesnelerinin üretiminden sorumlu olan sınıfların tasarlandığı
tasarım kalıbıdır. İsminden de anlaşılacağı gibi fabrika olarak düşünülebilir. Bu fabrikamız bize sınıfların nesnelerini üretir.
Nesneler üretilirken IF gibi karar mekanizmaları ile kod
karmaşasına gerek olmadan aynı abstract sınıfı ya da interface’i uygulayan
sınıflar bizim factory’miz olur.
Abstract Factory UML
Daha fazla uzatmadan bir örnek ile anlamaya çalışalım.
Örneğimizde bir buton tasarladığımızı düşünelim. Web’de farklı Android’de
farklı şekilde tasarlandığını varsayalım. Bunun için uygulamanın koştuğu
platforma göre buton değişmeli. Öyle bir program yapalım ki Android’de butonu
farklı göstersin Web’de farklı.
Uygulamanın class diyagramı
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IAyarlar | |
{ | |
string Name { get; set; } | |
void Goster(IButton btn); | |
} | |
public interface IButton | |
{ | |
void ButonOlustur(); | |
string ButonText { get; set; } | |
} | |
public interface ButtonFactory | |
{ | |
IButton ButtonOlustur(string buttonText); | |
IAyarlar OSAyarlari(); | |
} | |
public class AndroidButtonFactory : ButtonFactory | |
{ | |
public IButton ButtonOlustur(string buttonText) | |
{ | |
AndroidButton androidButton = new AndroidButton(); | |
androidButton.ButonText = buttonText; | |
androidButton.ButonOlustur(); | |
return androidButton; | |
} | |
public IAyarlar OSAyarlari() | |
{ | |
AndroidIface android = new AndroidIface(); | |
android.Name = "Android Tarayici"; | |
return android; | |
} | |
} | |
public class WebButtonFactory : ButtonFactory | |
{ | |
public IButton ButtonOlustur(string buttonText) | |
{ | |
WebButton webButton = new WebButton(); | |
webButton.ButonText = buttonText; | |
webButton.ButonOlustur(); | |
return webButton; | |
} | |
public IAyarlar OSAyarlari() | |
{ | |
WebIface web = new WebIface(); | |
web.Name = "Web Tarayıcı"; | |
return web; | |
} | |
} | |
public class AndroidButton : IButton | |
{ | |
public string ButonText { get; set; } | |
public void ButonOlustur() | |
{ | |
Console.WriteLine("AndroidButton Oluşturuluyor.."); | |
} | |
} | |
public class WebButton : IButton | |
{ | |
public string ButonText { get; set; } | |
public void ButonOlustur() | |
{ | |
Console.WriteLine("WebButton Oluşturuluyor.."); | |
} | |
} | |
public class AndroidIface : IAyarlar | |
{ | |
public string Name { get; set; } | |
public void Goster(IButton btn) | |
{ | |
Console.WriteLine(string.Format("{0} butonu oluşturuldu..", Name)); | |
} | |
} | |
public class WebIface : IAyarlar | |
{ | |
public string Name { get; set; } | |
public void Goster(IButton btn) | |
{ | |
Console.WriteLine(string.Format("{0} butonu oluşturuldu..", Name)); | |
} | |
} | |
// İŞİ YAPACAK OLAN SINIFIMIZ | |
public class Olustur<T> where T : ButtonFactory,new() | |
{ | |
private IButton button; | |
private IAyarlar ayarlar; | |
private T factory; | |
public Olustur(string buttonName) | |
{ | |
factory = new T(); | |
button = factory.ButtonOlustur(buttonName); | |
ayarlar = factory.OSAyarlari(); | |
} | |
public void Goster() | |
{ | |
ayarlar.Goster(button); | |
} | |
} | |
// ÇALIŞTIRMAK İÇİN GEREKLİ KOD | |
static void Main(string[] args) | |
{ | |
Olustur<WebButtonFactory> webButton = new Olustur<WebButtonFactory>("Web"); | |
webButton.Goster(); | |
Olustur<AndroidButtonFactory> androidButton = new Olustur<AndroidButtonFactory>("Android"); | |
androidButton.Goster(); | |
Console.ReadKey(); | |
} | |
Yorumlar
Yorum Gönder