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.
3,054
questions
0
votes
1
answer
27
views
Unit testing OverflowException : Maximum retries of 10000 reached without finding a unique value
So I am working on some unit testing before I implement a new feature. I run my test and it fails with OverflowException : Maximum retries of 10000 reached without finding a unique value This is the ...
0
votes
0
answers
51
views
Generic factory for type T<E> for Java Collections
I've been a software developer for more than a year now and I wanted to try and create my own generic factory, to instantiate new objects.
Overall the goal is, to create a generic solution to ...
0
votes
1
answer
22
views
In Rails 4 and FactoryGirl, how do I override the base factory's "after(:build)" method?
I’m using Rails 4.2.3 with FactoryGirl. I have this factory for my users
FactoryGirl.define do
…
factory :user do
after(:build) do |user, vars|
print "in main user after build.\n&...
0
votes
0
answers
9
views
Database Factory records don't get pushed to the DB in Laravel
I have created Factory and Seeder for my Posts in Laravel. The code works fine in Tinker as it creates records successfully. Also when I migrate:fresh --seed also displays successful action. However, ...
0
votes
1
answer
22
views
How do I mock a callback in my FactoryGirl factory, using Rails 4.2.3?
I’m using Rails 4.2.3. I have this callback in my user model …
after_create :publish
…
def publish
Mymodule::Publisher.new_user(user: self, x_forward: {})
end
I would like to mock this ...
0
votes
0
answers
47
views
Using the factory design pattern to create a specific instance of one type
I'm using C++ to write a program for an arduino with a touchscreen display. In this I have a Display class which in a basic sense allows me to work with the data read from the pins on the board, and ...
0
votes
2
answers
23
views
Python strange namespace bahavior for class factory
I tried to write simple class factory using the "type" method.
I called myclass_factory twice and It returned identical namespaces
(case1 and case2). But the values of myclass attribute in ...
0
votes
0
answers
21
views
Abstract factory for dart (or similar construct)
In Dart, I'm managing collections that share a common property (e.g., all have the same value for attribute x). I also want to have access to some extra data regarding x. I wanted to achieve this ...
2
votes
1
answer
58
views
compiler error with C++ template says that is not the member of struct
I'm a newer of using C++ template and I got trouble with template compiling.
I want to write a similar factory method with template but compiler error says that 'ip is not the member of ...
1
vote
1
answer
34
views
Abstract class selectively expose methods based on derived type
I am writing an program that implements a factory TimeManager to generate two types of objects; a Timer and a StopWatch. Both of these objects are derived from an abstract class TimePiece, and share ...
0
votes
1
answer
31
views
Factory & Composite Design Patterns combo in Python & circular imports
I've built a couple projects now using the composite pattern where the objects hierarchy is built from a configuration file. My problem is that I's like to save each subclass in a separate file, to ...
0
votes
0
answers
29
views
Factory method and CDI: Heavyweight instructions with assignable and execute
I have a requirement to implement a Factory/Builder that does not know which object it has to create, so it delegates for each object to say if they are responsible for handling a parameter, this is a ...
0
votes
1
answer
50
views
In scala 3, how to get a given Factory for both something like Iteratable and an Array?
I'm trying to create a trait for sorting functions like this:
trait Sorting:
def sort[A, B >: A, C <: IterableOnce[A]](list: C)(using ord: Ordering[B], factory: Factory[A, C]): C
and here is ...
-1
votes
0
answers
31
views
Golang Generics: Factory Implementation [duplicate]
I am trying to implement a factory pattern in Golang. I have an interface, that is typed with generics. In my case I want to process an item. That item type is determined by the caller. So.. I have ...
0
votes
0
answers
36
views
What pattern should be used when creating objects like a factory, but the initializers require different input?
My exact goal is to create certain objects at runtime based on strings read in from a configuration file, but generally it is to create objects with different initializers at runtime.
Examples of ...
0
votes
1
answer
99
views
How to write an enum factory in dart?
I have this enum:
/// Firmware update BLE OTA transfert speed
enum FirmwareUpdateBleOtaTransfertSpeed { fast, medium, moderate }
With the following extension:
extension ...
0
votes
0
answers
44
views
Remove switch in more generic way
I encountered this class:
public class CommandFactory
{
private readonly Dictionary<string, ICommand> commands;
private readonly IPrinterEmulator printerEmulator;
public ...
0
votes
1
answer
45
views
Difference between using getit and declaring your own instances
I am trying to understand the best practices in Dart and I saw in couple of instances that people declare singletons without the getit package and also the factory declarations are also a bit ...
0
votes
0
answers
22
views
Can't pass value from UITextField to another ViewController
I use factory pattern for check user login.
I have UITextField in my ViewController and want to check value in Presenter
My Presenter:
func showNextScreen() {
if confirmViewController?...
0
votes
0
answers
36
views
Better factory pattern usage with inheritance (Typescript)
I am trying to build an async factory method that will build and return a new object using typescript.
The parent class has this factory method as each of his children can use the method to create an ...
0
votes
0
answers
28
views
C++ what is the most efficient and expressive way to construct factory methods?
Ideally what I would like to write would be manager.add({arg}) where
class Manager {
public:
Something pool[10];
int count = 0;
void add(Something ...); <- method interface to be ...
0
votes
0
answers
14
views
Task Factory Advanced Execute Package Task not detecting when child package is done
I'm using Task Factory's Advanced Execute Package task (2019.1.3) to call another package that exists outside of the current project. When I configure the Child Package Location as File System, it ...
0
votes
1
answer
37
views
Set a faker value less than another faker field
Hello I'm creating a Factory for a discount model. I have two fields ('original_price' and 'discounted_price'). I'm using randomFloat in factory to create a random number with 2 decimals for the '...
0
votes
0
answers
42
views
Set Type at Runtime using C++ 17
I am new to C++ (in C# this is child's play) I would like to set the kernel used in a method at runtime depending on configuration. The method in question is
enum PoseSolverType
{
...
-1
votes
1
answer
46
views
What is the correct syntax of JavaScript Factory Function? JavaScript Factory Function Syntax confusion
Both function getMyCar1 & getMyCar2 has same result but which one is the correct way of doing?
getMycar2: Why value have to use instead of key? key:value (carBrand:brand).
function selectCar(brand,...
0
votes
1
answer
56
views
How to prevent references from a constructed object back into a blueprint like source object which got assigned to the former target structure?
This is an example from a Pokemon-like game. I am constructing an Object, and inside it i am trying to make a new Object "en" and "to", that is two different attacks. The problem ...
4
votes
1
answer
396
views
Find the minimum number of steps to half the sum of elements in a list where each step halves an item in the list in O(N)
I came across an interview question that went like this:
There are factories in an area which produce a pollutive gas and filters are to be installed at each factory to reduce the pollution. Each ...
2
votes
0
answers
57
views
Factory method in C++: why additional Creator interface and does factory method need a class?
I am currently unclear what constitutes the Factory Pattern in C++ given many implementations and examples in the wild.
https://refactoring.guru/design-patterns/factory-method/cpp/example gives an ...
0
votes
0
answers
19
views
can I use a façade design pattern with a factory design pattern?
I have an animal interface and subclasses like birds, cats..etc that extends from the Animal class and I have a factory class.. my question is can I use facade design with the factory class? and how?
1
vote
0
answers
23
views
Custom utility types (generic types) for classes `IsClass` of TypeScript
I am trying to create a generic type to make sure the first parameter to be a class. However, the factory function parameter cannot be replaced by a generic type.
The following upper parts were my ...
0
votes
1
answer
32
views
Python can't infer static type of subclass when using a handler factory
I would like to use a general HandlerFactory class like the one described here (see Solution 2: Metaprogramming).
Let me use an example:
Suppose we have the following classes:
class Person:
name: ...
3
votes
0
answers
37
views
Typescript factory of generic interface in
I'm creating a factory like this:
interface IOperator<T> {
parse(arg: string): T;
doSomething(value: T): void;
}
2 operators:
class StringOperator implements IOperator<string> {
...
0
votes
0
answers
26
views
How do i provide property missing?
I try to create a factory function for graphql resolver but i get error for the function return.
enter image description here
I don't want turn false tsconfig "declaration". Please existing ...
0
votes
0
answers
18
views
Factory function for mongoose transaction session on Typescript
I have a lot of function like these
currency.service.ts
async createCurrencyOnSession(createCurrencyDto: CreateCurrencyDto): Promise<ResponseCurrencyDto> {
const session = await this....
0
votes
0
answers
55
views
Test if Javascript Tic Tac Toe Game with mini max algorithm working correctly
I have created a Tic Tac Toe Game that allows the user to play against the computer. The computer is supposed to implement the mini max algorithm where it chooses the best move to make in relation to ...
0
votes
0
answers
32
views
nestjs inject a class or another based on parent class
In a nestjs project I have a ServiceValidationService and a ControllerValidationService, both implement the same interface so they have same methods with same signature. They just do something ...
2
votes
1
answer
89
views
How to create a "factory function" for a templated class?
How would someone go about implementing a factory function for a templated class? Either my google searches aren't looking for the right thing, or I am misunderstanding the results. As an example:
...
0
votes
1
answer
11
views
Kohana combine orm factories
I have two factories "News" and "Photoset". Can I somehow combine them?
$news = ORM::factory('News')
->where('title_'.I18n::$lang, '<>', '')
->and_where(...
0
votes
1
answer
58
views
Factory - A value of type 'B' can't be returned from the constructor 'A.factoryConstructor' because it has a return type of 'A<T>'
I have this simplified code:
enum Types {
b,
c,
}
class A<T> {
A();
factory A.factoryConstructor(Types type) {
switch(type) {
case Types.b:
return B(); // <- A ...
0
votes
0
answers
68
views
Azure Data Factory Dataflow trouble with ToDecimal
Im kinda new with ADF so thats why im struggling with the following task:
My source file is a csv file and has a column TotalCost and TotalSales. These columns has a string type. In My destionation ...
0
votes
1
answer
56
views
Checking instanceof with a class
I have the following code:
function Complex(real, imaginary) {
let c = Object.create(Complex.methods);
[c.real, c.imaginary] = [real, imaginary];
return c;
}
Complex.methods = {
...
0
votes
0
answers
33
views
Is it possible to create factory which creates objects by class name in typescript?
Hi I want to create factory which will return objects of some supertype based on string parameter(it's a class name)
I found next working solution:
export class ActionFactory {
private constructor() ...
1
vote
1
answer
39
views
Populate a column from one table with the column value from another table with factorys
Table ItemAA
id_aa
name_aa
Table ItemBB
id_bb
name_bb
RelashionShips One To One
Goal to be achieved: populate the name_aa column (ItemAA table) with the same value as the name_bb column (ItemBB ...
1
vote
1
answer
58
views
Defining a class with or without the .prototype
In the absence of using the class keyword, what is the difference between the following two ways to construct an inherited object? In the first one I'm attaching things on the .prototype and in the ...
0
votes
1
answer
42
views
Java static factory for classes that implement interface with generic method parameter
I have the following static factory implementation:
public class HandlersFactory {
private static Map<ProviderType, Handler<? extends Request>> handlers;
public static Handler<? ...
1
vote
0
answers
95
views
Is there a better way to achieve multiple connection to database in Symfony 6?
I'm having some trouble trying to achieve multiple connection to database in some clean way.
Keep in mind that this is my first symfony project ever, and i'm only a young developer.
In my project, the ...
1
vote
1
answer
34
views
Factory using CDI returning capture of type instead of concrete type
I want to create a processor factory that uses CDI to get all available processors. The factory should select the desired processor based on some parameter. So I have my parameter:
public abstract ...
1
vote
1
answer
65
views
The motivation behind 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 ...
0
votes
1
answer
130
views
Javascript Tic Tac Toe Computer Player Does Not Play Consistently
I have created a tic tac toe game using javascript, which allows the user to play against the computer. The problem I am faced with is that the computer only plays some of the time when the user plays....
0
votes
1
answer
34
views
Any workaround to pass multiple arguments to defaultdict's default_factory?
I have a factory method called create_food, which takes in multiple string parameters like below:
def create_food(self, meat: str, vege: str) -> str:
I have another dictionary called __kitchen ...