Questions tagged [factory-method]
The factory-method tag has no summary.
73 questions
4
votes
3
answers
3k
views
What is a SOLID way for creating objects dynamically using a factory?
Description
I want to create an instance of an object at runtime, however, the parameters used to create the object are not always the same. To help explain, I have created a simplified example (in ...
0
votes
0
answers
157
views
factory methods in JPA entity classes. anti pattern?
I started using factory methods in JPA Entity classes. But I'm not sure if it's an anti-pattern. If it is , then where do I place them instead ?
A similar sample is below
@Builder
@Entity
Class ...
0
votes
0
answers
1k
views
Abstract Factory for methods/constructor with different arguments
I have the following classes in my system (python):
class Metric(ABC):
def compute(self): -> float
class Visualization(ABC)
def visualize(self)
class FirstMetric(Metric)
def __init__(...
2
votes
2
answers
729
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 ...
0
votes
3
answers
770
views
Is it a code smell to have a static factory method on the base class?
Suppose we have a BaseModel, which has a type enum, and derived models with same constructor signatures to each other, whose implementations are like :
public DerivedModelJ(Object arg1, ..., Object ...
1
vote
2
answers
3k
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, ...
-2
votes
1
answer
684
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 ...
0
votes
2
answers
145
views
Is my analogy of an Abstract Factory valid?
After working through several tutorials and reading various responses on this site, I believe the Abstract Factory pattern would work well for a current project. I am seeking the opinions of those ...
0
votes
2
answers
602
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
287
views
Is this a good use of template specialization? Or should the factory method be used?
I have a program that involves two different data structures, and so I created a class that acts as a generalized data structure that either of the original two can be represented as. (Because the ...
3
votes
1
answer
2k
views
Factory Method Pattern - the problem and what does it do to solve it
I always had problems in grasping the full benefits/motif behind using the Factory design pattern (for this post, I will stick to Factory Method pattern, specifically). True, there are (really) lots ...
2
votes
1
answer
578
views
Should I use Factory Method design pattern for this problem?
I'm working on an application which needs to open a database file. There are 2 "versions" of this database: one of them is more general data storage, and the other contains "less" information. That ...
21
votes
6
answers
13k
views
Strategy vs Factory design pattern
I am new to design patterns and working my way through the Factory Method and Strategy patterns. I understand that Factory is a creational pattern and Strategy is behavioral but I struggle to ...
0
votes
2
answers
499
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
4k
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 ...
1
vote
1
answer
2k
views
What is the difference between simple factory and factory method?
I am a little bit confused about simple factory and factory method. My main difficult is the abrut difference between the examples code on the internet, even on wikipedia, where have lots of them, ...
7
votes
3
answers
4k
views
Should a standard factory always generate a new instance?
According to my understanding on factory-method, factory should always return a new instance, meaning no cache, So in essence, every time when the factory method is called, there should always be a ...
-3
votes
1
answer
243
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 ...
2
votes
3
answers
550
views
Is this a good enough example of Factory method pattern?
On Wiki page for Factory method pattern, there is following example:
public interface IPerson
{
string GetName();
}
public class Villager : IPerson
{
public string GetName()
{
...
16
votes
4
answers
26k
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 ...
4
votes
4
answers
282
views
How to get a decoupled design without injecting repositories inside entities
I am facing the following situation:
I have to develop a system that has to calculate the price of a car so i need to calculate the prices of all the Pieces that make up the car and then total them.
...
0
votes
1
answer
631
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 ...
2
votes
1
answer
1k
views
why do the factory method and creator class not always need to be abstract
While going through the book 'Head First design patterns ' on Factory Method Pattern chapter , I came across the following question and answer at pp.135 (print publication date of the book : 2004/10/...
1
vote
1
answer
848
views
How can a factory method stay true to "Accept interfaces, and return structs" in Golang?
Let's us say I have a package which contains different types of TV structs. Now, based on the parameters passed I would like to return a specific TV type.
How is it possible to return the specific ...
-1
votes
3
answers
273
views
Should a simple factory method include the "new" operation?
For example, suppose I have a class to create a button with specific styles common to my app, I can have either
Return a new modified object:
public class ButtonFactory {
public static Button ...
0
votes
4
answers
176
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* ...
1
vote
0
answers
124
views
If you name the functions on a "factory class" and then invoke them statically, is still a factory?
Let's take the following Javascript, but the language is not really relevant:
module.exports = (user) => {
return {
createPrimaryConfig: () => {
return new Config('a', 'b', ...
2
votes
1
answer
7k
views
Builder with constructor or factory method?
Let's say I have a class Dot with a builder:
public class Dot {
private final Double x;
private final Double y;
private final Color color;
private Dot(Double x, Double y, Color color)...
5
votes
3
answers
2k
views
Why Named Constructors are getting popular shouldn't be an antipattern?
According to following article Named Constructors The Author suggests using static factory pattern to construct objects is way better than instantinate with new keyword. At the begining the idea is ...
6
votes
4
answers
2k
views
Difference between composing Factory and inheriting
I'm reading about "Factory method" design pattern from "Head First Design Patterns". So, there is a class
public class PizzaStore {
SimplePizzaFactory factory;
public PizzaStore(...
2
votes
1
answer
647
views
Class diagram. Combining several creators
So I would like to model the class diagram for the following scenario:
the client has to manage three types of accounts(Pocket, Postbank, DKB). More types of accounts may be added later.
the client ...
0
votes
4
answers
2k
views
In which way does the factory pattern decouple classes dependency?
I understand that the main code uses the factory to return an abstract pointer of the object desired, but it doesn't change the heritability of classes.
Can you explain in which context does the ...
4
votes
3
answers
15k
views
Factory method for objects with multiple complex constructors
I'm refactoring a legacy codebase.
I have 4 very similar objects that I decided would be a good target for becoming polymorphic, so I moved all the common code to a base class and added an interface.
...
15
votes
3
answers
495
views
Multitudes constructing one implementation. DI hopeless? Use service locator?
Say we have 1001 clients that construct their dependencies directly rather than accept injections. Refactoring the 1001 is not an option according to our boss. We're actually not even allowed access ...
12
votes
4
answers
9k
views
"Factory Method is a specialization of Template Method". How?
Similarities and differences between the two:
Template Method
Relies on inheritance.
Defines the steps of an algorithm, and leaves the task of implementing them to subclasses.
Factory Method
Relies ...
1
vote
2
answers
187
views
Aim of Factory pattern is to stop us from over-riding or re-writing the functions which instantiate?
http://www.cs.unc.edu/~stotts/GOF/hires/pat3cfso.htm
CreateMaze is the function which instantiates the objects. IMO, according to factory pattern we are not supposed to overload or modify or re-write ...
5
votes
3
answers
5k
views
What is the difference between Bridge and Factory Pattern in C#?
What is the difference between Bridge and Factory Pattern? both the pattern seems to instanitae the class depending upon the logic from the client side.
Factory Pattern
interface IFactory {
...
24
votes
10
answers
7k
views
Is it an anti-pattern if a class property creates and returns a new instance of a class?
I have a class called Heading that does a few things, but it should also be a able to return the opposite of the current heading value, which finally has to be used via creating a new instance of the ...
3
votes
4
answers
2k
views
Are factories required when doing dependency injection?
I'm trying to refactor some code, so it uses dependency injection.
Take this (non-sense) example class:
class Foo {
protected $min;
protected $max;
public $bar;
public function __construct($...
6
votes
2
answers
4k
views
Am I breaking SRP when I inject Factory pattern with Repository layer?
In the context of MVC sometimes I find myself creating a Factory and injecting the factory with Repository.
While it is certainly possible to use Repository as layer inside the Factory, I wonder if ...
3
votes
5
answers
18k
views
Refactoring if-else logic to reflect the OOP principles
I have read some of the related questions regarding how we can refactor a code based on if/else if statements to follow closely the OOP principles but I have difficulties to apply this to a concrete ...
4
votes
1
answer
1k
views
Factory pattern versus dynamic class instantiation in PHP
I am developing a web framework in PHP, and I want to make it fully extendable. My goal is to make it possible for a developer to change the behavior of any component without having to modify the "...
10
votes
2
answers
13k
views
Static factory method in base class
An increasingly popular definition of factory method is: a static
method of a class that returns an object of that class' type. But
unlike a constructor, the actual object it returns might be an
...
2
votes
1
answer
239
views
Conditional factory
I need to perform a payment and I have two payment methods: FirstPayment and TokenPayment, which both implement PaymentInterface
interface PaymentInterface
{
public function pay(PaymentRequest $...
-1
votes
1
answer
216
views
What would be the merits/disadvantages of an OOP language that forced the generator pattern?
In OOP languages, programs can define static methods that can generate objects with different parameters (including subclassed or pre-generated objects), but they are not as commonly used because we ...
3
votes
3
answers
2k
views
Am I using the factory method design pattern correctly, or which creational pattern should I use?
I've been studying creational design patterns for the past week or so because I have a common use case that keeps coming up, and I can't figure out which pattern fits the bill.
Here is a simplified ...
1
vote
2
answers
286
views
Parameterization vs subclassing
Example taken from : Agile software development : principles, patterns and practices
A new employee is added by the receipt of an AddEmp transaction. This transaction contains the employee's
name, ...
14
votes
2
answers
28k
views
Avoiding constructors with many arguments
So I have a factory which creates objects of different classes.
The possible classes are all derived from an abstract ancestor.
The factory has a configuration file (JSON syntax) and decides which ...
5
votes
2
answers
2k
views
Factories, vectors and smart pointers - Design Question
So, my Business Code needs some Objects.
It does not know how much objects it needs and it does not know the exact types (because polymorphism is involved).
For me, that sounds for a good reason to go ...
4
votes
2
answers
394
views
Is there a Factory Pattern distinct from Abstract Factory and Factory Method?
During a recent phone screen I was asked to describe the "Factory Pattern". I asked if the screener meant "Factory Method" or "Abstract Factory". He said, "No, just the Factory Pattern".
I don't know ...