Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [factory]

In object-oriented programming (OOP), a factory is an object for creating other objects.

Filter by
Sorted by
Tagged with
5 votes
6 answers
2k views

Is it a good idea to return a Builder from a Factory?

I would want to have a builder for creating a complex object, for example Employee. I would want to be able to create my objects in two ways: Case 1. Get Employee with default values Case 2. Get ...
CuriousGuy's user avatar
0 votes
2 answers
120 views

Where to put factories that depend on the application layer?

I am a frontend developer (currently working with Vue and TS), and I have been searching for better organization of the frontend code and recently I got to know DDD a bit. I know DDD is mostly used ...
Bernardo Benini Fantin's user avatar
2 votes
3 answers
414 views

Is having many build-once factories a sign of bad dependency injection design?

I have a form. It contains things like grids. Users do things with these grids and what they do with them is sent to a SQL server. Clearly, the server is a volatile dependency and should be dependency ...
J. Mini's user avatar
  • 1,007
2 votes
5 answers
546 views

Is it a code smell for a factory to have many methods?

I have a testing project, in Katalon Studio, that uses data classes, called models. This is a conscious design decision. Also a conscious design decision, is the decision to separate the concern of ...
Mike Warren's user avatar
2 votes
2 answers
140 views

OOP Best practices: Is there any reason to separate out Factory functionality from an abstract base class?

Consider the following python3 code: from abc import ABC, abstractmethod class Food(ABC): _food_factory_map = {} _recipes = {} @classmethod def getFood(cls, foodName): return ...
raghavj's user avatar
  • 29
0 votes
1 answer
59 views

Use factory design pattern to create a specific object

I am using this DTO class to pass object between web application layers public class CreateProgressDTO { public int Total { get; set; } public int Created { get; set; } public decimal ...
dafie's user avatar
  • 109
29 votes
3 answers
11k views

Is it Good Practice to Only Expose Interfaces

I''m working on a C# library where the API provides several public interfaces and a single concrete factory class (itself an interface implementation). This factory provides implementations of the ...
Bondolin's user avatar
  • 403
0 votes
4 answers
2k views

Should I design a factory that returns a singleton?

I design a common API for selected printers of different brands in Java. Each printer uses a different underlying SDK with different functions, but any hardware my code runs on will have only one ...
Martin Braun's user avatar
0 votes
3 answers
225 views

If I have a ThingFactory which creates multiple concrete IThings, each with exclusive parameters, how do I avoid ThingFactory knowing about them all?

A common problem I run into is when I have a Factory (let's say ThingFactory) which creates multiple different IThing implementations, and each of these concrete implementation classes requires an ...
vargonian's user avatar
  • 327
0 votes
1 answer
690 views

Unit testing parts that use a Value Object that is being created by a Factory

Here is the conundrum, I have a fairly complex Value Object and I don't want to expose it's internals. It should not be an Entity since there is no need for attaching an identity to it. According to ...
Tetra's user avatar
  • 3
2 votes
3 answers
347 views

Use abstract factory (or an alternative way) to produce an instance of a subclass?

Context for this question I'm currently working with small data storage media (e.g. RFID tags) that contain some fixed number of bytes that can be manipulated. The drivers that allow reading bytes ...
JansthcirlU's user avatar
-1 votes
1 answer
48 views

Create new object based upon another abstract object

Assume the following class structure: class Base; class A : public Base; class B : public Base; class WrapperBase; class AWrapper : public WrapperBase; class BWrapper : public WrapperBase; I ...
Patrick Wright's user avatar
0 votes
3 answers
200 views

What pattern is a function that returns a component?

Ok I have a bunch of components that all have the same logic but have different css classes. So I wanted to create a sort of factory function that takes the names of the classes as its argument and ...
kaan_a's user avatar
  • 155
1 vote
2 answers
2k views

Can a class be considered as a factory even though it only creates one concrete type of objects?

I have a simple class called Link that contains some properties, and use different classes for creating different types of links. My code looks like this: class Link { String reference, label, ...
Harold L. Brown's user avatar
0 votes
2 answers
191 views

Design: Tying behavior to state

I have the following (hypothetical) setup and am wondering if anyone has design suggestions, keywords, or nudges. I have a class Walk that implements a method doActivity. This method keeps track of ...
Randabut's user avatar
-2 votes
1 answer
124 views

Modify method behaviour at runtime

I have a scenario where user has multiple filters in a web application and a displayed list of items. In the gui it has multiple FilterGUI that he/she can tune interacting in the browser (these are ...
c0l1n's user avatar
  • 9
0 votes
0 answers
307 views

How can I use the Factory pattern and expose my factories to consuming code without introducing transitive dependencies on the constructed types?

I have a C# class called FileTransferManager. Previously it took some parameters such as a username and password to connect to a FTP server. However now I have an additional requirement to support ...
ekolis's user avatar
  • 521
0 votes
2 answers
425 views

Overkill to apply abstract factory pattern for a single object creation?

I need to vary the object creation at (*). public class Parser { // Problem code public List<FileOption> methodA() { // Does something ... fileOptions....
Sebastian Nielsen's user avatar
1 vote
1 answer
2k views

DDD Aggregate in PHP — how to retrieve the root?

Status quo I created an aggregate, let's call it Foo. It has two entities within itself, let's call them Foo & Bar. You can mutate things by calling the aggregate's public methods. E.g. $foo->...
nkamm's user avatar
  • 69
3 votes
1 answer
456 views

Design Patterns: Factory Pattern Vs. getInstance Inside Abstract Class

I'm working on an app where we need to use different authentication flows depending on how the user is accessing the app. I want to make sure that there is only one instance of the authentication ...
YSA's user avatar
  • 141
0 votes
1 answer
226 views

Complex objects creation from parsing a file

I have these classes and I want to instantiate each one of them easily. All the data to create them are in a json file and there will be a lot of different objects in the end. Each objects have a lot ...
rXp's user avatar
  • 117
-2 votes
2 answers
1k views

What is the best way to handle classes descended from the same base class in a collection?

My language is C#. I have a set of seven classes that all ultimately derive from a single class. The image of the class diagram is posted below. I will frequently need to iterate through collections ...
Eric's user avatar
  • 105
0 votes
0 answers
228 views

How to create factories for same object type but different ways of creation following Domain Driven Design rules? (C++)

Brief description of a problem - providing factories which are creating same object type in different ways and following rules of DDD (isolated domain model, independent domain objects inside of it). ...
Bohdan's user avatar
  • 1
0 votes
2 answers
469 views

SimpleFactory vs Factory Method

Let's assume a SimpleFactory that creates a group of objects: public SimpleFactory { public Bycicle createBycicle(String type) { if(type.equals("ONE")) return new OneWheelBycicle(); if(...
Asier Naiz's user avatar
5 votes
5 answers
3k views

Factories and static methods

So almost every post I read about oop by purists, they keep stressing about how using static methods is anti pattern and breaks the testability of the code. On the other hand every time I look for ...
lahory's user avatar
  • 315
4 votes
8 answers
4k views

What's the benefits to use an abstract factory when using interfaces is already suffice?

I get the idea of the factory pattern, but I feel that it is really not necessary to use this pattern. For example, below is some code I saw (C#) that use factory method: public interface IAnimal { ...
secondimage's user avatar
0 votes
3 answers
127 views

Software design/architecture of a patient simulator with multiple mathematical models

I am working on a simulator which simulates patients, so specific processes can be validated on "virtual" patients, before testing on real patients. The simulator is essentially a mathematical model ...
Gertjan Brouwer's user avatar
-3 votes
1 answer
212 views

What kind of factory method abstract singleton design pattern is this?

I made a code that seems to mix Singleton design pattern, and Fatory method. But my factory method is in an abstract class inherited by my Singleton ... what the hell have I created ? Does it have a ...
Motiss's user avatar
  • 1
-1 votes
1 answer
208 views

Dependency Injection and Factory resolution

In the below example, assume FooA and FooB each have constructors that have a large amount of dependencies being injected into them. If I have a class that needs to determine which IFoo ...
gilliduck's user avatar
  • 237
2 votes
3 answers
716 views

Abstract Factory with Pure DI

I have an application where I'm using pure DI to construct my object graph. I would like to use a factory so I can create and destroy instances of a certain class at run time. The trouble is that ...
Rob L's user avatar
  • 106
2 votes
2 answers
2k views

Creational design pattern that allows configuration of objects

With the factory pattern we abstract the creation of objects. But what if we need a specific configuration of an object that depends on the calling context? Example: So I have a Builder pattern for ...
Jim's user avatar
  • 327
0 votes
1 answer
567 views

how to use Builder pattern combined with protected access modifier?

I'm reading a book in Design Patterns, and below is some code example used by the author. The author tries to build a html builder as (code in C#): public class HtmlElement { public string Name, ...
slowjams's user avatar
  • 127
0 votes
5 answers
1k views

Is it a bad practice to create a global factory?

I am studing the importance of the design pattern and I am not able to understand if could be a bad behavior to create a global static factory class (maybe using singleton?) I put an example hoping ...
micrus_'s user avatar
  • 19
7 votes
6 answers
1k views

I need help solving a common architectural problem with multiple concrete classes implementing an interface

I have a common issue I encounter which I've solved with a Factory class in the past but it always felt a little "off". Let's say I have multiple Exporter classes which implement IExporter, which has ...
vargonian's user avatar
  • 327
0 votes
1 answer
410 views

Reset state or create new objects

I am developing a desktop application that will be run at each station at a call center. It listens for an incoming call, and whenever a call arrives, there are about 30 different classes that need ...
Rob L's user avatar
  • 106
3 votes
2 answers
5k views

When to use Factory design pattern instead of Dependency Injection?

I'm quite inexperienced in design patterns and while I was studying them I got confused about the application of the Factory pattern. Wouldn't DI decouple the classes more than factory would do it? Or ...
MilitanteDoPT's user avatar
0 votes
1 answer
638 views

Is a Factory calling a Factory an anti-pattern or an extension of the Single Responsibility Principle?

I'm busy playing around with some training material to help teach some of the juniors the factory pattern. Whilst writing the example, I can't help but think this might be an anti-pattern. Lets say ...
Darkestlyrics's user avatar
15 votes
4 answers
21k views

Should I use the Factory Pattern when instantiating objects with very different constructors?

Let's say (just for the sake of example) I have three classes that implement IShape. One is a Square with a constructor of Square(int length). Second is a Triangle with a constructor of Triangle(int ...
Craig's user avatar
  • 463
0 votes
1 answer
575 views

Factories to create same objects with different data sources

I have several handlers classes that implements same interface and factories to create handlers. Handlers: public class Handler1 : IHandler { private readonly IService1 _service1; private ...
Alex Gurskiy's user avatar
1 vote
3 answers
267 views

Responsibility of a Factory

I am trying to refactor a huge chunk of our software, and it requires to set up a factory. However, I am not sure where lies its responsibility. (PS: The code below is shown simply as an illustration ...
Yassine Badache's user avatar
3 votes
2 answers
3k views

Factory that returns multiple implementations of the same interface

A few developers and I are attempting to refactor a class that has grown too large. Currently this class is around 3K lines long. The goal of the refactor is to make the logic more maintainable. The ...
kontained's user avatar
-1 votes
2 answers
224 views

Object composition in a static function

I have a class that provides basic functionality of a modal window. For different situations I need to provide different modals(different number of buttons, titles, etc...). I have a kind of factory ...
7368616d696c's user avatar
-1 votes
1 answer
5k views

Factory Design Pattern Implementation with multiple interfaces and base class

I know it is lot of code. But I am trying to understand the concept of Factory pattern with interfaces and base class. I have a base class of Car that implements an interface which will be implemented ...
Learn AspNet's user avatar
0 votes
4 answers
5k views

Static Factory Methods vs Constructors

Which one of the following way is recommended and why? Date d = Date.from(curr); Date d = new Date(curr); Can you also provide some examples behind the reasoning?
hatellla's user avatar
  • 181
1 vote
1 answer
87 views

Pseudo-Factory Abstract Class

So I have a requirement for something like this: The client shouldn't be aware of how the actual classes are implemented or constructed. The classes implement a common interface So I used Factory ...
Zoso's user avatar
  • 241
1 vote
2 answers
624 views

Does it make sense to implement a Class Factory design in an RPG Character Creation module

I'm a self-taught Engineer, a beginner in Java and I am trying to create a Dungeons and Dragons character creation module for a bigger game to solidify my understanding of core concepts while learning ...
brndng's user avatar
  • 19
4 votes
2 answers
1k views

Passing DI container to a factory – is this always an antipattern?

Generally I would always answer "yes" to this question, because at this point, your DI container has become a service locator, and you're losing the benefits of DI. However, I wonder if it is ...
user avatar
2 votes
1 answer
3k views

DDD - Factory or Service?

I'm new to DDD, and I'm confused at the very start of my DDD project. To setup a context, I'm building the user management part of my app, so I'm building my Account entity, which contains an Id, a ...
TanguyB's user avatar
  • 467
0 votes
1 answer
97 views

Factory pattern with creators

The software should be able to: Draw shapes on a drawing Save drawings Load drawings Adding a new shape should have a limited effect on existing code. There is no user cases, no clues about ...
Stud's user avatar
  • 111
2 votes
1 answer
295 views

MVP Passive View -> Dependency Injection -> Factory Pattern -> Is testing behaviour whilst ignoring state enough?

I am writing an application using the Model/View/Presenter passive view pattern. I have view that contains multiple items in a list. My model is of type AudioDrama. For filtering that list I have ...
selmaohneh's user avatar