Bir nesnenin contruction işlemi için özel bir sınıf oluşturduysak bu factory method oluyor. Atıyorum koltuk üretmek için new koltuk(); çağrısı yapmaktan ziyade KoltukFabrikası(“koltuk”); gibi bir çağrı yapıyorsak bu factory methodu oluyor.
Peki tek tip bir koltuğum yoksa ne olacak. Farklı stiller için aynı fabrikayı mı kullanmalıyım? Farklı stiller için farklı fabrikalar kullanabiliriz: KoltukFabrikası() nı abstract bir class haline getirip. Onu miras alan VintageKoltukFabrikası() , ModernKoltukFabrikası , CevreciKoltukFabrikası gibi sınıfları kullanırı ve buna da abstract factory deriz.
Tabi gereksiz yere kullanmamak lazım.
Because your example is not complicated enough. For such a simple scenario it doesn’t even make sense to use an advanced pattern.
But if you have to know more than the product to construct A, B or C, and you can’t have direct access to that knowledge, then it is useful. Then you are using the factory to act as a knowledge center for producing needed objects.
Maybe those objects need a reference to some object X, which the factory can provide, but your code in the place where you want to construct A, B or C can’t or shouldn’t have access to X. Maybe when you have X you create A and B but if you have Y type then you create C.
Also consider that some objects might need 20 dependencies to create; what then? Going to hunt for those dependencies in a place where they should not be accessible might be problematic.


Leave a Reply