factory-pattern

Constructor Injection - Do we inject factories as well?

馋奶兔 提交于 2019-12-03 12:23:29
After listening to the Clean Code Talks , I came to understand that we should use factories to compose objects. So, for example, if a House has a Door and a Door has a DoorKnob , in HouseFactory we create a new DoorKnob and pass it to the constructor of Door , and then pass that new Door object to the constructor of House . But what about the class that uses the House (say the class name is ABC ) ? It will depend on the HouseFactory , right? So do we pass the HouseFactory in the constructor of ABC ? Won't we have to pass a whole lot of factories in the constructor that way? Staying with the

Do we ever need to prefer constructors over static factory methods? If so, when?

送分小仙女□ 提交于 2019-12-03 12:20:59
I have been reading Effective Java by Joshua Bloch and so far it really lives up to its reputation. The very first item makes a convincing case for static factory methods over constructors . So much that I began to question the validity of the good old constructors :). The advantages/disadvantages from the book are summarized below: Advantages: They have names! We have total instance control (Singletons, performance, etc.) They can return a subtype/interface Compiler can provide type inference Disadvantages: Private classes cannot be subclassed They do not stand out in the documentation as

Factory Pattern to build many derived classes

落花浮王杯 提交于 2019-12-03 09:51:55
问题 I have a factory object ChallengeManager to generate instances of a Challenge object for a game I'm building. There are many challenges. The constructors for each Challenge class derivation are different, however there is a common interface among them, defined in the base class. When I call manager.CreateChallenge() , it returns an instance of Challenge , which is one of the derived types. Ideally, I would like to keep the code for the object construction inside the derived class itself, so

Singleton in Conjunction with the Factory Pattern in PHP5

拜拜、爱过 提交于 2019-12-03 09:16:42
What is the best method for using singleton design pattern in conjunction with the factory method pattern in PHP5? My simplest usage scenario for this is instantiation selective database connection only once for each database type. singleton factory for DB connection: class Registry { private static $_objects; public static function set($key, $object) { if (!array_key_exists($key, self::$_objects)) self::$_objects[$key] = $object; } public static function get($key) { if (array_key_exists($key, self::$_objects)) return self::$_objects[$key]; else return false; } } class DBFactory { public

Parameterized Factories Using Ninject

匆匆过客 提交于 2019-12-03 09:10:35
How to make Ninject to instantiate object based on variable on run time?. I am trying to inject the correct Repository in The Controller action - MVC 3 - based on parameter come from user input. If user input "BMW" it would bind ICarRepository to BMWRepository , and if he input "KIA" KiaRepository will be injected. [HttpPost] public ActionResult SearchResult(FormCollection values) { string carModel = values["model"]; ICarRepository myRepository = RepositoryFactory.getRepository(carModel); ..... } This is known by switch/case noob instantiation or Parameterized Factories, and i know how to do

Factory Pattern but with object Parameters

六月ゝ 毕业季﹏ 提交于 2019-12-03 08:37:26
问题 Take the following classic factory pattern: public interface IPizza { decimal Price { get; } } public class HamAndMushroomPizza : IPizza { decimal IPizza.Price { get { return 8.5m; } } } public abstract class PizzaFactory { public abstract IPizza CreatePizza(ItalianPizzaFactory.PizzaType pizzaType); } public class ItalianPizzaFactory : PizzaFactory { public enum PizzaType { HamMushroom, Deluxe, Hawaiian } public override IPizza CreatePizza(PizzaType pizzaType) { switch (pizzaType) { case

Why use the Service Manager in Zend Framework 2?

人盡茶涼 提交于 2019-12-03 08:27:54
问题 lets say i have a service: namespace Helloworld\Service; class GreetingService { public function getGreeting() { if(date("H") <= 11) return "Good morning, world!"; else if (date("H") > 11 && date("H") < 17) return "Hello, world!"; else return "Good evening, world!"; } } and i create an invokable for it public function getServiceConfig() { return array( 'invokables' => array( 'greetingService' => 'Helloworld\Service\GreetingService' ) ); } then in my controller i could do: public function

How to correctly make a thread safe Singleton Factory in Java? [duplicate]

牧云@^-^@ 提交于 2019-12-03 06:12:31
This question already has answers here : What is an efficient way to implement a singleton pattern in Java? [closed] (29 answers) This is the first time I am writing a Factory class. Below is my Factory class, I am not sure whether this is the correct way of making thread safe Singleton Factory class or not. I will be returning instance of my Client using this factory? public class ClientFactory { private static ClientFactory instance = null; private ClientFactory() { } public static ClientFactory getInstance() { if (instance == null) { instance = new ClientFactory(); } return instance; }

augment the factory pattern in java

99封情书 提交于 2019-12-03 04:49:38
I am trying to use a factory pattern to create a QuestionTypeFactory where the instantiated classes will be like MultipleChoice, TrueFalseQuestion etc. The factory code looks something like this class QuestionFactory { public enum QuestionType { TrueFalse, MultipleChoice, Essay } public static Question createQuestion(QuestionType quesType) { switch (quesType) { case TrueFalse: return new TrueFalseQuestion(); case MultipleChoice: return new MultipleChoiceQuestion(); case Essay: return new EssayQuestion(); } throw new IllegalArgumentException("Not recognized."); } } This works ok for now. If I

Spring dynamic injection, factory-like pattern

≡放荡痞女 提交于 2019-12-03 04:25:32
问题 A continuation from Dependency injection, delayed injection praxis. I have the Main class: package test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import java.util.List; import java.util.Scanner; @Component public class Main { @Autowired private StringValidator stringValidator; @Autowired private