Skip to main content

Questions tagged [factory-method]

Filter by
Sorted by
Tagged with
4 votes
3 answers
3k views

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 ...
zwoolli's user avatar
  • 151
0 votes
0 answers
157 views

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 ...
kommradHomer's user avatar
0 votes
0 answers
1k views

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__(...
ledermauss's user avatar
2 votes
2 answers
729 views

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 ...
YoavKlein's user avatar
  • 172
0 votes
3 answers
770 views

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 ...
Mike Warren's user avatar
1 vote
2 answers
3k views

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, ...
Harold L. Brown's user avatar
-2 votes
1 answer
684 views

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 ...
Navjot Singh's user avatar
0 votes
2 answers
145 views

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 ...
Norm Schaeffer's user avatar
0 votes
2 answers
602 views

I need to vary the object creation at (*). public class Parser { // Problem code public List<FileOption> methodA() { // Does something ... fileOptions....
Sebastian Nielsen's user avatar
1 vote
1 answer
287 views

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 ...
Aaron's user avatar
  • 19
3 votes
1 answer
2k views

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 ...
Veverke's user avatar
  • 541
2 votes
1 answer
578 views

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 ...
user3132457's user avatar
21 votes
6 answers
13k views

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 ...
Reynier Booysen's user avatar
0 votes
2 answers
499 views

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(...
Asier Naiz's user avatar
5 votes
5 answers
4k views

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 ...
lahory's user avatar
  • 315
1 vote
1 answer
2k views

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, ...
celsowm's user avatar
  • 253
7 votes
3 answers
4k views

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 ...
Rui's user avatar
  • 1,935
-3 votes
1 answer
243 views

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 ...
Motiss's user avatar
  • 1
2 votes
3 answers
550 views

On Wiki page for Factory method pattern, there is following example: public interface IPerson { string GetName(); } public class Villager : IPerson { public string GetName() { ...
Navjot Singh's user avatar
16 votes
4 answers
26k views

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 ...
Craig's user avatar
  • 473
4 votes
4 answers
282 views

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. ...
X.Otano's user avatar
  • 622
0 votes
1 answer
631 views

I have several handlers classes that implements same interface and factories to create handlers. Handlers: public class Handler1 : IHandler { private readonly IService1 _service1; private ...
Alex Gurskiy's user avatar
2 votes
1 answer
1k views

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/...
Istiaque Ahmed's user avatar
1 vote
1 answer
848 views

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 ...
pinkpanther's user avatar
-1 votes
3 answers
273 views

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 ...
ocomfd's user avatar
  • 5,760
0 votes
4 answers
176 views

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* ...
ocomfd's user avatar
  • 5,760
1 vote
0 answers
124 views

Let's take the following Javascript, but the language is not really relevant: module.exports = (user) => { return { createPrimaryConfig: () => { return new Config('a', 'b', ...
Vaughan Hilts's user avatar
2 votes
1 answer
7k views

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)...
AplusKminus's user avatar
5 votes
3 answers
2k views

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 ...
FZE's user avatar
  • 469
6 votes
4 answers
2k views

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(...
David Hovsepyan's user avatar
2 votes
1 answer
647 views

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 ...
blfuentes's user avatar
  • 165
0 votes
4 answers
2k views

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 ...
Max's user avatar
  • 1
4 votes
3 answers
15k views

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. ...
BgrWorker's user avatar
  • 1,704
15 votes
3 answers
495 views

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 ...
candied_orange's user avatar
12 votes
4 answers
9k views

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 ...
Maria Ines Parnisari's user avatar
1 vote
2 answers
187 views

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 ...
Aquarius_Girl's user avatar
5 votes
3 answers
5k views

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 { ...
Lijin Durairaj's user avatar
24 votes
10 answers
7k views

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 ...
53777A's user avatar
  • 1,718
3 votes
4 answers
2k views

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($...
Arnold Daniels's user avatar
6 votes
2 answers
4k views

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 ...
Dennis's user avatar
  • 8,267
3 votes
5 answers
18k views

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 ...
Leron's user avatar
  • 217
4 votes
1 answer
1k views

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 "...
alexw's user avatar
  • 339
10 votes
2 answers
13k views

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 ...
q126y's user avatar
  • 1,733
2 votes
1 answer
239 views

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 $...
marcosh's user avatar
  • 123
-1 votes
1 answer
216 views

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 ...
TheHans255's user avatar
3 votes
3 answers
2k views

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 ...
rory.ap's user avatar
  • 929
1 vote
2 answers
286 views

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, ...
q126y's user avatar
  • 1,733
14 votes
2 answers
28k views

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 ...
lugge86's user avatar
  • 445
5 votes
2 answers
2k views

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 ...
lugge86's user avatar
  • 445
4 votes
2 answers
394 views

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 ...
kojiro's user avatar
  • 2,095