factory-pattern

instance factory methods Vs Static factory methods

霸气de小男生 提交于 2019-11-29 20:36:48
Can't all factory methods be static ? Does something that produces a product need state ? When is it appropriate to go for a instance factory or static factory method ? Can you provide me examples differentiating the two ? Ramon Chiara Assuming that by "instance factory method" you're actually saying about the GoF "factory method", the term "static factory method" is described by Joshua Bloch in his book "Effective Java". Googling around I reached at these sites: Factory Method: http://sourcemaking.com/design_patterns/factory_method Static Factory Method: http://www.informit.com/articles

A Factory Pattern that will satisfy the Open/Closed Principle?

爷,独闯天下 提交于 2019-11-29 18:23:01
问题 I have the following concrete Animal products: Dog and Cat . I am using a parameterized Factory method to create said products. Depending on the AnimalInfo parameter that is passed to the Factory method, a concrete product will be created. The mapping logic is placed in the Factory method. Here is my code: public abstract class AnimalInfo { public abstract String Sound { get; } } public class DogInfo : AnimalInfo { public override string Sound { get { return "Bark"; } } } public class CatInfo

Using a Strategy and Factory Pattern with Dependency Injection

≡放荡痞女 提交于 2019-11-29 13:14:16
问题 I am working on a side project to better understand Inversion of Control and Dependency Injection and different design patterns. I am wondering if there are best practices to using DI with the factory and strategy patterns ? My challenge comes about when a strategy (built from a factory) requires different parameters for each possible constructor and implementation . As a result I find myself declaring all possible interfaces in the service entry point, and passing them down through the

How to inject different services at runtime based on a property with Spring without XML

南笙酒味 提交于 2019-11-29 11:22:28
问题 I am using Spring Boot for Java standalone application. I have a bean which makes use of a service. I want to inject different implementations of that service at runtime, based on a property in a properties file with Spring (4 for that matter). This sounds like the Factory pattern, but Spring also allows using annotations to solve the problem, like this. @Autowired @Qualifier("selectorProperty") private MyService myService; Then in the beans.xml file I have an alias, so that I can use the

Why am I getting “The type parameter must be invariantly valid…” error?

余生颓废 提交于 2019-11-29 10:32:11
I'll attempt to shorten this code example: public interface IThing { //... Stuff } public class Thing1 : IThing { } public class Thing2 : IThing { } public interface IThingView<out T> { ICollection<T> ViewAll(); } public class ThingView<T> : IThingView<T> { ICollection<T> ViewAll() { return new List<T>(); } // There's a big operation here } public interface IThingViewerFactory { public IThingView<IThing> Build(string Which); } public class ThingViewerFactory { public IThingView<IThing> Build(string Which) { if(Which.Equals("Thing1") { return new (IThingView<IThing>)new ThingViewer<Thing1>();}

C# Generic Interface and Factory Pattern

我的梦境 提交于 2019-11-29 07:03:49
问题 I am trying to create a Generic interface where the parameter type of one of the methods is defined by the generic EDIT I've changed the question slightly after realising I have probably confused matters by specifying a type parameter in the Factory creation method. What I have is two types of API calls that I need to make to a 3rd party API. The first retrieves a record from the API using an Id that is an int. The second also retrieves a record from the API but the Id is a string (guid). I

Motivation for Simple Factory and Factory Method Pattern

大憨熊 提交于 2019-11-29 06:51:01
I know there are a lot of questions out there about differences of different factory patterns, but the answers are so different and confusing. The books that i read use unclear and (over)simplified examples. I have a number of questions even after reading Wikipedia explanations, and a lot of online explanations about them including all on these site. The book that I'm currently reading is Head First Design Patterns. In Simple Factory the client uses separate class (Creator) with factory method (which CAN be static) to return Products. In Factory Method Pattern the Creator and the Client are

Why do static Create methods exist?

荒凉一梦 提交于 2019-11-29 03:36:00
I was wondering, why do static Create methods exist? For instance, why use this code: System.Xml.XmlReader reader = System.Xml.XmlReader.Create(inputUri); over this code: System.Xml.XmlReader reader = new System.Xml.XmlReader(inputUri); I cannot find the rationale for using one over the other, and can't find any relation between classes who use this construct over the other. Can anyone shed some light on this? XmlReader is an abstract class. You cannot instantiate it. Providing a Create method is an instance of the factory pattern. Depending on the specified arguments a different

DDD repository and factory

僤鯓⒐⒋嵵緔 提交于 2019-11-28 23:06:17
问题 In my application a few layers. In this topic will focus on Domain and Infrastructure layers. I have repository interface ClientRepositoryInterface in Domain layer. And I have implementation of this interface ClientRepositoryImpl in Infrastructure layer. But to reconstitute the object in the middle of the cycle of its existence I need factory(ReconstitutionClientFactory). Call the factory will be in the repository. The book by Eric Evans is described as a normal practice. But where this

Type Casting and the Factory pattern

时光怂恿深爱的人放手 提交于 2019-11-28 22:01:27
I'm having a hard time figuring out how to implement a factory pattern in a DTO mapper I'm trying to create. I'm pretty sure I need to rethink my design. Here is a very small example of what I'm running in to: public abstract class Person { public string Name { get; set; } public decimal Salary { get; set; } } public class Employee : Person { public Employee() { this.Salary = 20000; } } public class Pilot : Person { public string PilotNumber { get; set; } public Pilot() { this.Salary = 50000; } } public static class PersonFactory { public static Person CreatePerson(string typeOfPerson) {