Questions tagged [factory-pattern]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-3 votes
1 answer
97 views

Is it a bad practice to pass instance as function parameter? [closed]

So I am learning how to use the factory function in javascript/typescript. I created a to-do list project where I had to pass the instance as a parameter to the function. I wanted to ask is it a bad ...
user avatar
2 votes
2 answers
337 views

The motivation behind the Factory Method design pattern

I'm learning about the Factory Method Desing Pattern and I'm having a hard time to understand exactly what it tries to solve and how. Let's first introduce the example that Wikipedia uses to have a ...
user avatar
  • 172
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
1 vote
1 answer
151 views

C# how to implement a factory class which doesn't require an argument passed to indicate objects type?

I currently working on a parser project in C# and have run into problem. I have an entity folder within my project and within it I have: Entity IEntity.cs (defines a contract for entity classes) ...
user avatar
0 votes
3 answers
405 views

Should validation logic be inside a factory method or inside the object's constructor?

Say I have a hypothetical factory method whose single responsibility is to create MyObjects. However, MyObject should only ever be constructed with an ordered list. Further, MyObjects without an ...
user avatar
  • 155
2 votes
1 answer
117 views

Builder that creates a repository for a specific entity - is this an established pattern?

Suppose I have the following entities: class Employee { public string Id { get; set; } public ICollection<EmployeeBadge> Badges { get; set; } } class Badge { public string Id { get; ...
user avatar
  • 23
-2 votes
1 answer
79 views

Is there a proper way of implementing runtime control of dependencies using DI? Is factory pattern okay? [closed]

I'm currently brushing up and learning about a bunch of techniques to hopefully begin implementing in my own workflow; one of which is IoC (and DI in particular). I'm hoping someone could clear up my ...
user avatar
  • 117
-2 votes
1 answer
88 views

How to implement factory pattern in following case?

I have a program which downloads web pages and then scrapes html to create domain specific collection objects e.g. ProductCollection, CatalogCollection, NewsCollection and more. The idea is to create ...
user avatar
1 vote
2 answers
1k views

Is a python `abstract property` that returns an abstract class an example of the Factory Pattern?

I need to document my design, in particular, the design patterns used, and would like to use the standard terminology. From Refactoring Guru, "Factory Method defines a method, which should be ...
user avatar
  • 137
2 votes
0 answers
196 views

How to handle a bunch of nested ValueObjects?

I'm writing a PHP web application (or actually a Symfony module). One part of it is a nested structure of ValueObjects (meaning: they are immutable and have to be validated on the creating). Such an ...
user avatar
0 votes
2 answers
593 views

Combining synchronous and asynchronous commands when using the command pattern

Let's say I'm building a simple console app which has three commands: Create category. Download recipe from API to category. Display all recipes in a category. Assuming the app will grow, I use the ...
user avatar
  • 139
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
1 vote
3 answers
1k views

Where to validate user input - Constructor, Validation object, or Factory method?

Let's say your application takes in a user request and you don't trust the front-end validation (because you never trust the front-end validation). In your controller or other handler, you want to ...
user avatar
2 votes
1 answer
99 views

How to simplify a complex factory pattern?

I have a function which takes the incoming request, parses the data and performs an action and posts the results to a webhook. This is running as background as a Celery Task. This function is a common ...
user avatar
  • 139
0 votes
2 answers
104 views

Should I refactor this class to use a Factory?

Given the following class in a Legacy code base without any UT's. So any refactoring done should be done on the smallest possible scale, just in order to be able to write UT's. public class Person { ...
user avatar
0 votes
1 answer
843 views

How to avoid "mock returning mock" when using factory pattern

I have recently encountered multiple articles with title Everytime a mock returns a mock a fairy dies And I ran into exact same situation while using factory class in my code. I am writing a sample ...
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
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
3 answers
986 views

Can Aggregate Root Factory create the root, and it's internal classes?

Currently, my aggregates and value objects have protected constructors and some of them are being created by static factory methods inside the aggregate with descriptive names. It creates a nice DSL ...
user avatar
  • 209
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
1 vote
1 answer
159 views

Separation of concerns in object creation with factory and model

Thinking about the overall architecture of the application logic: Current status: The model contains all resources that are used at runtime. In a factory you register a builder which is used to ...
user avatar
5 votes
1 answer
215 views

Is this contract on an API user is inevitable?

I am designing a UI framework in Java. Every UI component, represented by the class Component, in the framework is identified by a non null unchangeable String key. So I get the key in the constructor ...
user avatar
1 vote
1 answer
239 views

Implementing a factory design pattern for GA selection

I've been working on different genetic algorithm selection methods recently. At the moment I have a RawFitnessSelction class, a RouletteWheelSelection class and a TournamentSelection class. I've been ...
user avatar
  • 75
0 votes
4 answers
2k views

Can factory pattern avoid code smell?

With two existing implementations, UFOEnemyShip & RocketEnemyShip, To introduce every new implementation implementing EnemyShip interface, shown below, changes are needed in factory class ...
user avatar
0 votes
1 answer
94 views

What does the details of instantiation encapsulation mean in factory design pattern?

I am trying to understand the factory design pattern and how it can be implemented in Javascript. So far what I understood is factory design pattern helps to create an instance of more specific class ...
user avatar
  • 111
2 votes
2 answers
355 views

What is the proper (or preferred way) to configure a static factory class?

First SE stack, I am working on a large chunk of old Java code and am finding tons of duplication and inconsistently configured/created objects because of different authors, skill levels, etc. I ...
user avatar
-1 votes
2 answers
158 views

How to Avoid Missed Behaviors when Separating Code from Data

I've come across something that I find decently frustrating while adding new functionality to our large existing code base. Preface We have a variety of classes (ItemA, ItemB, ItemC...) that ...
user avatar
  • 1,549