Questions tagged [factory]

"Factory" is a general term for object-oriented programming patterns which create objects. Instead of this tag, please use tags for the specific Gang of Four patterns [factory-method] or [abstract-factory] when appropriate.

"Factory" is a general term for object-oriented programming patterns which create objects. Instead of this tag, please use tags for the specific Gang of Four patterns or when appropriate.

3171 questions
524
votes
19 answers

What is the difference in case of intent and application between these two Patterns?

Factory and Abstract Factory are both creational patterns. What is the difference in case of intent and application between these two Patterns?
user366312
  • 16,949
  • 65
  • 235
  • 452
335
votes
17 answers

Factory Pattern. When to use factory methods?

When is it a good idea to use factory methods within an object instead of a Factory class?
jjshell
252
votes
9 answers

Design Patterns: Factory vs Factory method vs Abstract Factory

I was reading design patterns from a website There I read about Factory, Factory method and Abstract factory but they are so confusing, am not clear on the definition. According to definitions Factory - Creates objects without exposing the…
188
votes
10 answers

How to name factory like methods?

I guess that most factory-like methods start with create. But why are they called "create"? Why not "make", "produce", "build", "generate" or something else? Is it only a matter of taste? A convention? Or is there a special meaning in…
deamon
  • 89,107
  • 111
  • 320
  • 448
160
votes
12 answers

Is there a way to instantiate objects from a string holding their class name?

I have a file: Base.h class Base; class DerivedA : public Base; class DerivedB : public Base; /*etc...*/ and another file: BaseFactory.h #include "Base.h" class BaseFactory { public: BaseFactory(const string &sClassName){msClassName =…
Gal Goldman
  • 8,641
  • 11
  • 45
  • 45
124
votes
3 answers

What does java:comp/env/ do?

I just spent too much time of my day trying to figure out some errors when hooking up some JNDI factory bean. The problem turned out to be that instead of this...
Danny
  • 3,670
  • 12
  • 36
  • 45
104
votes
17 answers

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…
User
  • 30,403
  • 22
  • 79
  • 107
102
votes
8 answers

Pass arguments to Constructor in VBA

How can you construct objects passing arguments directly to your own classes? Something like this: Dim this_employee as Employee Set this_employee = new Employee(name:="Johnny", age:=69) Not being able to do this is very annoying, and you end up…
bgusach
  • 14,527
  • 14
  • 51
  • 68
90
votes
8 answers

Non-Singleton Services in AngularJS

AngularJS clearly states in its documentation that Services are Singletons: AngularJS services are singletons Counterintuitively, module.factory also returns a Singleton instance. Given that there are plenty of use-cases for non-singleton services,…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
84
votes
5 answers

AngularJS: factory $http.get JSON file

I am looking to develop locally with just a hardcoded JSON file. My JSON file is as follows (valid when put into JSON validator): { "contentItem": [ { "contentID" : "1", "contentVideo" : "file.mov", …
jstacks
  • 2,437
  • 8
  • 32
  • 48
75
votes
9 answers

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(Object): def __init__(self, domain): …
drjeep
55
votes
7 answers

Factory in Java when concrete objects take different constructor parameters

I'm trying to implement a Factory pattern in Java. I have a class called Shape which Circle and Triangle extends. The problem is that Shape constructor gets only 2 parameters while Circle gets 3 parameters and so is Triangle (which I won't show in…
Jjang
  • 11,250
  • 11
  • 51
  • 87
53
votes
7 answers

Factory classes

Personally I've never understood the idea of factory classes because it seems a whole lot more useful to just instantiate an Object directly. My question is simple, in what situation is the use of a factory class pattern the best option, for what…
Neil Locketz
  • 4,228
  • 1
  • 23
  • 34
52
votes
3 answers

How to pass arguments to Laravel factories?

I have a users table and a one-to-zero/one relation with a businesses table (users.user_id => businesses.user_id). On my users table I have a discriminator which tells me if the user is of type business and therefore I need to have details on the…
Cristian
  • 2,390
  • 6
  • 27
  • 40
50
votes
6 answers

Laravel - Seeding Many-to-Many Relationship

I have a users table and a roles table that has a many-to-many relationship. These two tables are connected to a junction table called role_user. This is a model of the tables and its connections. Below are the Models in my Laravel…
Bilal Khawar
  • 511
  • 1
  • 4
  • 7
1
2 3
99 100