Questions tagged [factory]

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

Filter by
Sorted by
Tagged with
0 votes
3 answers
194 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 ...
user avatar
  • 327
-1 votes
0 answers
55 views

Multiple classes with multiple implementations - Factory pattern

I am trying to imagine what architecture I could use to build my program with the following elements : I have a hierarchy of classes, called detectors, all inheriting from a BaseDetector class. They ...
user avatar
  • 1
0 votes
1 answer
144 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 ...
user avatar
  • 3
2 votes
3 answers
154 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 ...
user avatar
-1 votes
1 answer
42 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 ...
user avatar
0 votes
3 answers
119 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 ...
user avatar
  • 155
0 votes
2 answers
381 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, ...
user avatar
0 votes
2 answers
186 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 ...
user avatar
-2 votes
1 answer
103 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 ...
user avatar
  • 9
0 votes
0 answers
112 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 ...
user avatar
  • 491
0 votes
2 answers
226 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....
user avatar
0 votes
1 answer
700 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->...
user avatar
  • 59
3 votes
1 answer
221 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 ...
user avatar
  • 141
0 votes
1 answer
109 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 ...
user avatar
  • 117
-2 votes
2 answers
303 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 ...
user avatar
  • 105
0 votes
0 answers
111 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). ...
user avatar
  • 1
0 votes
2 answers
406 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(...
user avatar
3 votes
4 answers
670 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 ...
user avatar
  • 285
4 votes
8 answers
3k 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 { ...
user avatar
0 votes
3 answers
114 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 ...
user avatar
-3 votes
1 answer
159 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 ...
user avatar
  • 1
-1 votes
1 answer
93 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 ...
user avatar
  • 227
2 votes
3 answers
444 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 ...
user avatar
  • 106
2 votes
2 answers
773 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 ...
user avatar
  • 297
0 votes
1 answer
313 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, ...
user avatar
  • 127
0 votes
5 answers
688 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 ...
user avatar
  • 19
7 votes
6 answers
732 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 ...
user avatar
  • 327
0 votes
1 answer
117 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 ...
user avatar
  • 106
3 votes
2 answers
3k 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 ...
user avatar
0 votes
1 answer
363 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 ...
user avatar
11 votes
4 answers
11k 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 ...
user avatar
  • 423
0 votes
1 answer
340 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 ...
user avatar
1 vote
3 answers
262 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 ...
user avatar
2 votes
2 answers
2k 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 ...
user avatar
-1 votes
2 answers
173 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 ...
user avatar
-1 votes
1 answer
4k 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 ...
user avatar
0 votes
4 answers
4k 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?
user avatar
  • 161
1 vote
1 answer
78 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 ...
user avatar
  • 199
1 vote
2 answers
500 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 ...
user avatar
  • 19
4 votes
2 answers
850 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
2k 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 ...
user avatar
  • 427
0 votes
1 answer
91 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 ...
user avatar
  • 111
2 votes
1 answer
225 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 ...
user avatar
1 vote
3 answers
288 views

Is the Factory pattern good for this situation?

Im developping for my company a software where clients can requests app deployements and upgrades. Each request has 3 status (Validated, Started and closed). For every type of request (deployment ...
user avatar
  • 21
1 vote
2 answers
81 views

Factories and event binding

Factories are used to create objects. I was wondering if it's against the purpose of a factory, if I would bind couple of objects together in the factory. Let me illustrate this with an example. Let'...
user avatar
  • 13
6 votes
1 answer
1k views

Approach for Constructing View Models in Complex MVVM Application

I'm struggling with the design in a WPF MVVM application. In a few courses I've taken, they say that having a lot of parameters in a constructor is a code smell, but they never address how to deal ...
user avatar
  • 169
3 votes
4 answers
847 views

Is Abstracting your code too much a bad use of SOLID Principles?

After an unresolved argument with a friend I decided to ask the stack overflow community. Is there something like abstracting your code too much? Which of the following is the better option. We come ...
user avatar
0 votes
4 answers
148 views

Should a class which has a method to create object A also implement a method to delete A?

For example, suppose I have a class to create a button with specific styles common to my app: class ButtonFactory{ public: static Button* createAppButton(std::string st,int font size){ Button* ...
user avatar
  • 5,582
0 votes
1 answer
630 views

Domain driven design - factory with cross-cutting concerns

I was recently tasked with creating a factory that processes webhook events. Any authorized application in the solution (1 app per domain) can post any meaningful payload and as long as the event's ...
user avatar
1 vote
2 answers
130 views

Does only a create method classify a class as a factory?

In my current project, I find my self making factories, but for two very distinct reasons: Reason #1: To assist my IoC container if a particular class needs an parameter that is only known when the ...
user avatar