Questions tagged [class-factory]

Anything related to the class-factory design pattern, i.e. a design pattern in object oriented programming where a class is used to create objects of derived classes.

Anything related to the class-factory design pattern, i.e. a design pattern in object oriented programming where a class is used to create objects of derived classes.

24 questions
13
votes
8 answers

Class factory to produce simple struct-like classes?

While investigating Ruby I came across this to create a simple Struct-like class: Person = Struct.new(:forname, :surname) person1 = Person.new('John', 'Doe') puts person1 # Which raised a few Python…
kjfletch
  • 5,394
  • 3
  • 32
  • 38
9
votes
5 answers

pros and cons of using factory vs regular constructor

(Using Python 3.2, though I doubt it matters.) I have class Data, class Rules, and class Result. I use lowercase to denote an instance of the class. A rules object contains rules that, if applied to a data object, can create a result object. I'm…
max
  • 49,282
  • 56
  • 208
  • 355
4
votes
4 answers

How to make a class factory to create the required derived class

I often use the class-factory pattern whereby a class has a private constructor and a static method to create the class. This allows for the situation where the class cannot be constructed for some reason, and a null is returned - very handy. I…
Dave
  • 3,429
  • 2
  • 26
  • 29
3
votes
2 answers

How to Reverse Dependency for Class Factory moved to a Library

The following code works very well when all involved classes are in the same project (determineSubClass is a member of BaseClass): protected static BaseClass determineSubClass(String p1, int p2, Boolean p3) { BaseClass baseObj = null; if (…
3
votes
0 answers

Custom COM class factory for managed in-proc server

I'm looking at implementing custom COM activation logic for a managed class library, in MkParseDisplayName/BindToObject manner. This way, creating an object from VB might look like this: obj =…
noseratio
  • 59,932
  • 34
  • 208
  • 486
3
votes
4 answers

Python - how do I force the use of a factory method to instantiate an object?

I have a set of related classes that all inherit from one base class. I would like to use a factory method to instantiate objects for these classes. I want to do this because then I can store the objects in a dictionary keyed by the class name…
tadasajon
  • 14,276
  • 29
  • 92
  • 144
2
votes
5 answers

Can I create class factory for a constructor with parameter?

I am using a class factory to create objects dynamically. I used this answer for its simplicity (and because I am using Qt). But now I realize I must add an argument to my constructor Item(bool newItem /* = true*/); instead of Item(); for the…
Thalia
  • 13,637
  • 22
  • 96
  • 190
1
vote
1 answer

Do I need a class factory function when using a descriptor that require initialization?

Consider this toy example where I use a descriptor to validate that a particular value doesn't exceed certain maximum value class MaxValidator: def __init__(self, max=10): self.max = max def __set__(self, obj, value): if…
jmborr
  • 1,215
  • 2
  • 13
  • 23
1
vote
1 answer

How to access class within a method in Python

I have a function make_package returning a class Package. In another file, I want to import class Package, so I can do type check. My question is, how to import a class that's inside a function? Following is not the exact code, but similar in…
Phoenix-C
  • 23
  • 1
  • 3
1
vote
0 answers

How do you implement a generic class factory in Swift?

I want to "make" a number of class instances similar to a base class but different in underlying type. (Not quite the same as the typical "Animal" class factory examples seen all over the net!) The code below is close to working but it requires the…
WholeCheese
  • 437
  • 3
  • 14
1
vote
1 answer

Create a class dynamically without instantiating it - no metaclasses?

Using WTForms form definition classes as an example: class RegistrationForm(Form): username = StringField('Username', [validators.Length(min=4, max=25)]) email = StringField('Email Address', [validators.Length(min=6, max=35)]) …
1
vote
0 answers

Initialization of static object in Windows (for a class factory)

I'm writing a class factory (in Visual Studio 2008) where the classes are registering themselves. The class factory is a singleton and the classes register themselfes like this: in Factory.h class Factory {...}; template class…
Christian
  • 2,214
  • 4
  • 26
  • 37
1
vote
1 answer

UICololor class factory methods - when are they released

I'm reading about class factory methods in objective-c as a design pattern and have some confusion. I"m using UIColor's as my example for the question for simplicity but if things vary with other class factory methods let me know. The basic…
LanternMike
  • 664
  • 1
  • 5
  • 16
1
vote
2 answers

Instantiate a inherited class using a static method from a base class

I have a abstract base class that I have many inherited classes coming off of. What I would like to do is a static member takes in a string, the first class that can parse the string (only one of the inherited classes should be able to) and return a…
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
1
vote
2 answers

Document functions inherited from generated class

I have a class factory that generates a class with a function bound to another class. When a class inherits from the generated class it gains the appropriate function. Now I want to document the generated class' function else one would have to…
siebz0r
  • 18,867
  • 14
  • 64
  • 107
1
2