factory-pattern

Factory pattern using variadic template?

倖福魔咒の 提交于 2019-11-28 22:00:20
I have an abstract class template <class T> struct A { /* virtual methods */ }; and several concrete derived classes with various constructors // The constructor of B takes 1 input template <class T> struct B : public A<T> { B() { /* default ctor */ } B( T *input ) { /* initializer */ } // .. implement virtual methods } // The constructor of C takes 2 inputs template <class T> struct C : public A<T> { double some_member; C() { /* default ctor */ } C( T *input, double& value ) { /* initializer */ } // .. implement virtual methods } I created a Factory that returns pointers to A , and I am

Replace factory with AutoFac

心不动则不痛 提交于 2019-11-28 21:03:18
问题 I'm accustomed to creating my own factories as shown (this is simplified for illustration): public class ElementFactory { public IElement Create(IHtml dom) { switch (dom.ElementType) { case "table": return new TableElement(dom); case "div": return new DivElement(dom); case "span": return new SpanElement(dom); } return new PassthroughElement(dom); } } I'm finally getting around to using an IoC container (AutoFac) in my current project, and I'm wondering is there some magic way of achieving the

Is this Factory Method creation pattern?

为君一笑 提交于 2019-11-28 16:05:21
I have been using factory method creation pattern for awhile now. I was just recently told that this: public static class ScheduleTypeFactory { public static IScheduleItem GetScheduleItem(ScheduleTypeEnum scheduleType) { IScheduleItem scheduleItem = null; switch (scheduleType) { case ScheduleTypeEnum.CableOnDemandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum.BroadbandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum.LinearCableScheduleTypeID: { scheduleItem = new LinearScheduleItem(); break; } case ScheduleTypeEnum

Passing Properties to Factory method

99封情书 提交于 2019-11-28 13:50:47
I have a factory method which returns implementation of an interface. The thing is - implementations have different constructor parameters. My question is - how to pass parameters through factory method to different implementations of the interface? I have an idea, but I'm not sure if it makes sense - pass Properties object to factory method? This way each of interface implementations can get the properties that needs for its constructor, while factory interface will be unified. Does this make sense, or there is a better solution? I decided to add-up an example, so I could clarify the problem

Annotation based ServiceLocatorFactoryBean?

为君一笑 提交于 2019-11-28 09:50:19
I would like to implement Factory pattern in my project..i have gone through online resources and I came to know that spring ServiceLocatorFactoryBean should be implemented instead of normal java factory pattern.... i have followed this link but it is explained in xml based....can any one tell me how to do it using annotations based Factory pattern?? Spring Java Configuration ref guide @Configuration Interface Parser.class public interface Parser { void parse(String str); } Implementation for above interface. JsonParser.java public class JsonParser implements Parser { @Override public void

Castle Windsor: A better way to implement 2 levels of (nested) factories?

拥有回忆 提交于 2019-11-28 05:17:11
问题 We have a pattern we've used several times, whereby we implement handlers and factories in separate Dlls. We configure exe's at runtime saying what dlls are loaded, and therefore what handlers are available to the app. We do this because we have custom handling for some customers, also it allows great flexibility because we can quickly develop new handlers in isolation, and test and deploy them with confidence that we haven't even touched any other parts of a running application. We can also

WCF Dependency injection and abstract factory

烂漫一生 提交于 2019-11-28 04:45:05
I have this wcf method Profile GetProfileInfo(string profileType, string profileName) and a business rule: if profileType is "A" read from database. if profileType is "B" read from xml file. The question is: how to implement it using a dependency injection container? Let's first assume that you have an IProfileRepository something like this: public interface IProfileRepository { Profile GetProfile(string profileName); } as well as two implementations: DatabaseProfileRepository and XmlProfileRepository . The issue is that you would like to pick the correct one based on the value of profileType.

Motivation for Simple Factory and Factory Method Pattern

陌路散爱 提交于 2019-11-28 00:16:18
问题 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

Return one of two possible objects of different types sharing a method

谁都会走 提交于 2019-11-27 22:49:09
I have 2 classes: public class Articles { private string name; public Articles(string name) { this.name = name; } public void Output() { Console.WriteLine("The class is: " + this.GetType()); Console.WriteLine("The name is: " + name); } } And public class Questionnaire { private string name; public Questionnaire(string name) { this.name = name; } public void Output() { Console.WriteLine("The class is: " + this.GetType()); Console.WriteLine("The name is: " + name); } } I want to write a method, that takes an integer (1 meaning Articles should be returned, 2 meaning Questionnaire ) and a name.

Why do static Create methods exist?

若如初见. 提交于 2019-11-27 22:10:04
问题 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? 回答1: XmlReader is an abstract class. You cannot instantiate it. Providing a Create