factory-pattern

Factory pattern using variadic template?

本秂侑毒 提交于 2019-11-27 14:23:12
问题 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

Type Casting and the Factory pattern

 ̄綄美尐妖づ 提交于 2019-11-27 14:11:34
问题 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;

Improper use of __new__ to generate classes?

微笑、不失礼 提交于 2019-11-27 12:14:54
I'm creating some classes for dealing with filenames in various types of file shares (nfs, afp, s3, local disk) etc. I get as user input a string that identifies the data source (i.e. "nfs://192.168.1.3" or "s3://mybucket/data" ) etc. I'm subclassing the specific filesystems from a base class that has common code. Where I'm confused is in the object creation. What I have is the following: import os class FileSystem(object): class NoAccess(Exception): pass def __new__(cls,path): if cls is FileSystem: if path.upper().startswith('NFS://'): return super(FileSystem,cls).__new__(Nfs) else: return

What is the difference between Factory and Strategy patterns?

拜拜、爱过 提交于 2019-11-27 10:14:04
Can any one explain the difference between factory and strategy patterns? For me both are looking same other than an extra factory class (which create an object of product in factory patterns) A factory pattern is a creational pattern. A strategy pattern is an operational pattern. Put another way, a factory pattern is used to create objects of a specific type. A strategy pattern is use to perform an operation (or set of operations) in a particular manner. In the classic example, a factory might create different types of Animals: Dog, Cat, Tiger, while a strategy pattern would perform

Passing Properties to Factory method

一笑奈何 提交于 2019-11-27 08:01:59
问题 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

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

自闭症网瘾萝莉.ら 提交于 2019-11-27 04:36:29
问题 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,

c++ automatic factory registration of derived types

北慕城南 提交于 2019-11-27 04:06:20
Like many before me, I'm trying so get my derived types to automatically register with my factory. I read through many question and tried to focus on what I didn't find there. I've got everything running nicely except the automatic registration. My Goals: automatically register any derived class of my base class Base only classes I mark as registrable not only direct sub-classes of Base ex: Base -> Device -> Camera -> Webcam this would make using the CRTP like described in this question dificult minimal changes to the classes I want registered - dummies proof would prefer using a registrator

Annotation based ServiceLocatorFactoryBean?

丶灬走出姿态 提交于 2019-11-27 03:15:06
问题 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?? 回答1: Spring Java Configuration ref guide @Configuration Interface Parser.class public interface Parser { void parse(String str); }

WCF Dependency injection and abstract factory

无人久伴 提交于 2019-11-27 00:37:44
问题 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? 回答1: 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

Dynamically register constructor methods in an AbstractFactory at compile time using C++ templates

狂风中的少年 提交于 2019-11-26 22:21:27
问题 When implementing a MessageFactory class to instatiate Message objects I used something like: class MessageFactory { public: static Message *create(int type) { switch(type) { case PING_MSG: return new PingMessage(); case PONG_MSG: return new PongMessage(); .... } } This works ok but every time I add a new message I have to add a new XXX_MSG and modify the switch statement. After some research I found a way to dynamically update the MessageFactory at compile time so I can add as many messages