creation-pattern

how to avoid “cascading if\case”-statements in a factory class

[亡魂溺海] 提交于 2020-02-05 03:48:28
问题 I have to create an object based on a specific situation. I read, that the solution could be the Factory Pattern, but in my case it has a lot of disadvantages. For instance: I have an application that manages animals. At some point a client gives to me a list of animals that must be created. A solution by using the factory pattern should be: //PRODUCTS public interface Animal { String getCall(); } public class Dog implements Animal { public String getCall() { return "Bau"; } } public class

how to avoid “cascading if\case”-statements in a factory class

半世苍凉 提交于 2020-02-05 03:48:07
问题 I have to create an object based on a specific situation. I read, that the solution could be the Factory Pattern, but in my case it has a lot of disadvantages. For instance: I have an application that manages animals. At some point a client gives to me a list of animals that must be created. A solution by using the factory pattern should be: //PRODUCTS public interface Animal { String getCall(); } public class Dog implements Animal { public String getCall() { return "Bau"; } } public class

What creational pattern I should use?

≯℡__Kan透↙ 提交于 2019-12-08 05:13:46
问题 My program have two classes; both derive from same base class. class A : MyBase { internal A(InitVal initVal) } class B : MyBase { internal B(InitVal initVal) } InitVal is another class which is injected through constructor. This class is for internal usage. Due to internal constructor, user cannot create instance of class A and B directly. Instead, I created method which creates these objects. class Initiator { InitVal initVal; public T CreateObject<T>(ObjectInstance objectInstance) where T

Registry pattern Vs Service locator pattern Vs Dependency Injection Container

故事扮演 提交于 2019-12-03 10:49:58
问题 Is there any difference between them rather than set and get objects in an array by key? class Registry { private $container=array(); public static function Set($name,$object){ self::$container[$name]=$object; } public static function &Get($name){ return self::$container[$name]; } } 回答1: Registry Pattern Registry pattern is a pattern used to lookup an object knowing only its name. This pattern stores instances of objects internally and uses a dictionary mapping to retrieve those instances