factory

How do you implement constructor injection in a factory?

When you're using a factory pattern, how do you inject dependencies into constructors at runtime? I'm building Foos with different formats - boolean, array, freetext, matrix, etc. That format list will grow as we find different uses for Foo. Here's my basic core domain: public interface IFoo { FooFormat Format { get; } } public ...

Registering derived classes in C++

EDIT: minor fixes (virtual Print; return mpInstance) following remarks in the answers. I am trying to create a system in which I can derive a Child class from any Base class, and its implementation should replace the implementation of the base class. All the objects that create and use the base class objects shouldn't change the way th...

Trouble with Factory and dynamic allocation in C++

I have a factory that builds the objects with longest lifetime in my application. These have types, lets say, ClientA and ClientB, which depend on Provider (an abstract class with many possible implementations), so both clients have a reference to Provider as member. According to the command-line arguments, the factory chooses one imple...

Factory Pattern Advice Needed Please

I'm working on wrapping my head around the factory pattern by using it in a simple data storage project in my free time. The idea is to take simple data and save it to a database using a simple factory pattern in VB.NET. I think I have a basic understanding of the pattern itself, however, what I'm struggling with is how to fit the fact...

Factory method implementation - C++

I have the following code for "factory" design pattern implementation. class Pen{ public: virtual void Draw() = 0; }; class RedPen : public Pen{ public: virtual void Draw(){ cout << "Drawing with red pen" << endl; } }; class BluePen : public Pen{ public: virtual void Draw(){ cout << "Drawing with ...

Using Factories in Presenters in a Model View Presenter and Domain Driven Design Project

In domain driven design, it appears to be a good practice to use Factories to create your domain objects in your domain layer (as opposed to using a direct constructor or IoC). But what about using the domain object factories in a presenter layer. For instance, say that I was creating a domain object from user input obtained from the p...

How do you use Intefaces with the Factory Pattern in Domain-Driven Design?

Does it make sense to use interfaces for your domain object factories by default, or should interfaces be reserved for the factory classes only when you need them? public IUserFactory { User CreateNewUser(); } public UserFactory : IUserFactory { public User CreateNewUser() { return new User(); } } ...

Validation (): Attribute 'Factory' is not a valid attribute of element 'ServiceHost'.

I'm getting Validation (): Attribute 'Factory' is not a valid attribute of element 'ServiceHost'. from VS2008 in an empty WCF Service Application after I add Factory="System.ServiceModel.Activation.WebServiceHostFactory" to the @Service directive in the .svc file. I've added System.ServiceModel.Web reference to the code-behind. I'm...

Class factory in Python

I'm new to Python and need some advice implementing the scenario below. I have two classes for managing domains at two different registrars. Both have the same interface, e.g. class RegistrarA: __init__(self, domain): self.domain = domain def lookup(self): ... def register(self, info): ... and class RegistrarB: ...

Factory Model in C#

I have a bunch of MDI child nodes that are all created in the same way and to reduce redundant code I'd like to be able to call a method, pass it a string (name of child node), have it create the node and add it to the parent. I can do all the stuff except create the class from a string of a class name, how can I do this? ...

Merging two domain objects

In the project I'm working on, we have an aggregate domain object. The factory object handles the creation of the unique id for the object. But there is a separate import process which creates the same object initially without the id. To add the imported object to the system, we are now forced to do a field by field copy to a new objec...

Is anyone using meta-meta-classes / meta-meta-meta-classes in Python/ other languages?

I recently discovered metaclasses in python. Basically a metaclass in python is a class that creates a class. There are many useful reasons why you would want to do this - any kind of class initialisation for example. Registering classes on factories, complex validation of attributes, altering how inheritance works, etc. All of this be...

Is there a good example of using a factory method / pattern for creating game objects / characters?

I want a clean way of creating game object such as NPCs, bullets and power-ups, reducing the amount of inter-dependence on specific classes. I believe this is what the factory pattern is used for? I would love to see a good implementation from a real game. I'm not an expert on design patterns, and never use them if I don't understand t...

Castle Windsor: How to prevent circular references in factory-created objects were the created objects refers back to the factory.

I am using windsor castle as my IoC container, and has run in to a bit of a problem. This is best explained in code, so I´ll give it a try. I have a factory class, that should provide me with implementations of a certain interface: public interface IObjectCreatorFactory { IObjectCreator GetObjectCreator(Type objectType); } public in...

What is your threshold to use factory instead of a constructor to create an object?

What is your threshold to use factory instead of a constructor to create an object? You always use factory. You use factories only if you have invariant checks other than checking for nulls. You always use constructors You rarely use factories... what are those cases?? pros and cons Update: I am applying factory pattern from Domain ...

Factory method returning an concrete instantiation of a C++ template class

I have a class template <unsigned int N> class StaticVector { // stuff }; How can I declare and define in this class a static factory method returning a StaticVector<3> object, sth like StaticVector<3> create3dVec(double x1, double x2, double x2); ? ...

Factory methods for implementations of Java interfaces wrapped with Scala implicits?

I'm using Scala implicits to define a rich wrapper for a Java interface: class RichThing { def richStuff: Unit = {} } In the companion object I define the implicit conversion and an apply factory method: object RichThing { implicit def rich( thing: JavaThing ) = new RichThing() def apply() = new RichThing() } With this, I...

Factory pattern in C#: How to ensure an object instance can only be created by a factory class?

Recently I've been thinking about securing some of my code. I'm curious how one could make sure an object can never be created directly, but only via some method of a factory class. Let us say I have some "business object" class and I want to make sure any instance of this class will have a valid internal state. In order to achieve this ...

Factory object vs factory function

I have an ABC with several derived classes. To create these derived classes I use the factory pattern: .h file: class derivedFactory { public: base* createInstance(); }; .cpp file: base* derivedFactory::createInstance() { new derived(); } Is there any advantage to this over just having a free function: .h file: base* de...

Named constructor and inheritance

I'm working on C++ framework and would like to apply automatic memory management to a number of core classes. So far, I have the standard approach which is class Foo { public: static shared_ptr<Foo> init() { return shared_ptr<Foo>(new Foo); } ~Foo() { } protected: Foo() { } }; // Example of use shared_...