Questions tagged [factory]
In object-oriented programming (OOP), a factory is an object for creating other objects.
106
questions
3
votes
1
answer
166
views
Seeking Clarification on the Abstract Factory Pattern
I am seeking clarification on the exact purpose and definition of the Abstract Factory pattern.
According to the GoF (Gang of Four) book, the intent of the pattern is to:
Provide an interface for ...
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 ...
0
votes
2
answers
172
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 ...
2
votes
3
answers
431
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 ...
2
votes
5
answers
549
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 ...
2
votes
2
answers
151
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 ...
0
votes
1
answer
60
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 ...
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 ...
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 ...
0
votes
3
answers
226
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 ...
0
votes
1
answer
777
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 ...
2
votes
3
answers
359
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 ...
-1
votes
1
answer
50
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 ...
0
votes
3
answers
206
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 ...
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, ...
0
votes
2
answers
192
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 ...
-2
votes
1
answer
139
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 ...
0
votes
0
answers
321
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 ...
0
votes
2
answers
474
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....
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->...
3
votes
1
answer
504
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 ...
0
votes
1
answer
254
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 ...
-2
votes
2
answers
2k
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 ...
0
votes
0
answers
233
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).
...
0
votes
2
answers
474
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(...
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 ...
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
{
...
0
votes
3
answers
129
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 ...
-3
votes
1
answer
221
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 ...
-1
votes
1
answer
238
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 ...
2
votes
3
answers
756
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 ...
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 ...
0
votes
1
answer
614
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, ...
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 ...
7
votes
6
answers
2k
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 ...
0
votes
1
answer
463
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 ...
4
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 ...
0
votes
1
answer
673
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 ...
16
votes
4
answers
23k
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 ...
0
votes
1
answer
599
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 ...
1
vote
3
answers
268
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 ...
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 ...
-1
votes
2
answers
230
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 ...
-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 ...
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?
1
vote
1
answer
88
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 ...
1
vote
2
answers
641
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 ...
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 ...
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 ...
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 ...