Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [factory-method]

The tag has no usage guidance.

5
votes
3answers
366 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 ...
3
votes
4answers
217 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
1answer
135 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
1answer
221 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/...
0
votes
1answer
219 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
3answers
198 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
4answers
138 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
0answers
101 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
1answer
2k 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)...
1
vote
2answers
709 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 ...
3
votes
2answers
220 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(...
3
votes
1answer
252 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
4answers
805 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 ...
2
votes
3answers
4k 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. ...
14
votes
3answers
372 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 ...
7
votes
3answers
3k 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 ...
1
vote
2answers
167 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 ...
3
votes
3answers
2k 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
10answers
6k 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 ...
4
votes
4answers
863 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($...
3
votes
2answers
924 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 ...
2
votes
6answers
7k 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 ...
3
votes
0answers
398 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 "...
4
votes
2answers
5k 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 ...
0
votes
1answer
118 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 $...
0
votes
1answer
183 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
1answer
585 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
2answers
215 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, ...
10
votes
3answers
13k 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
2answers
835 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 ...
3
votes
2answers
241 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 ...
12
votes
5answers
6k views

Does the Factory Pattern violate the Open/Closed Principle?

Why does this ShapeFactory use conditional statements to determine what object to instantiate. Dont we have to modify ShapeFactory if we want to add other classes in the future? Why doesnt this ...
1
vote
1answer
673 views

What's the difference between a Factory method and Entity Translator

The entity translator provides a function that takes one or more data contract classes as parameters and returns a specific business entity. Within the implementation of the function, a new ...
4
votes
2answers
6k views

Factory for creating a singleton instance

We have some legacy code that has a bunch of singletons all over the place (written in C#). The singleton is a fairly "classic" implementation of the pattern: public class SomeSingleton { ...
1
vote
1answer
171 views

Event action abstraction and trigger control

I am thinking of abstracting Event action in order to make further alteration/implementation/maintenance easy since the project I am currently working on has volatile requirement. So I thought of the ...
2
votes
1answer
445 views

How to design database tables for a Factory class?

How can I design database table structure for a class that varies in its core parameters, when it is created by a factory method? I have a Factory like this: class ProductFactory { public ...
0
votes
4answers
2k views

Client vs Creator in Factory Method and Abstract Factory patterns

This page descibes one important difference between Factory Method and Abstract Factory: http://architects.dzone.com/articles/factory-method-vs-abstract The difference, according to this page, is ...
1
vote
2answers
80 views

Why is entangling injectable with class that uses it a bad practice?

I have had this argument for a while, because I have noticed some people prefer "readable" code over properly structured one. So in the example I am showing, basically I have this Mapper class in ...
7
votes
1answer
6k views

How can one presenter be used for multiple views in MVP

I am using MVP for creating an android application, which takes data from server and sets to activity. I am forced to create one presenter for each view. Each view is unique because each view has ...
1
vote
3answers
2k views

Why static members can't be abstract and don't implement polymorphism? [duplicate]

I'm a Java/C#/PHP developer, through my OOP programming experience I find myself asking the same question: Why static members can't be abstract and don't implement polymorphism especially in ...
3
votes
2answers
5k views

What are the disadvantages of a “simple factory”?

I am reading the book »Head First Design Patterns« from O'Reilly. Before explaining the Factory Method Pattern, they introduce a Simple Factory first. They are using the example of a pizzeria. In a ...
5
votes
2answers
1k views

Does this factory method pattern example violate open-close?

In Head-First Design Patterns, they use a pizza shop example to demonstrate the factory method pattern. public abstract class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza; ...
2
votes
2answers
2k views

When is it worth NOT using a Factory?

I'm employing TDD quite a bit these days and really enjoying myself - everything seems to flow better and be naturally better constructed and organized. However, while writing a bit of IO code, ...
0
votes
1answer
105 views

Using factory pattern when classes have little in common

I have used Factory pattern where I have multiple classes which does similar sort of work, but I don't want client to worry about class instantiating and return types. So Instantiate the necessary ...
1
vote
1answer
1k views

Differentiating between Factory Method and Abstract Factory

I've been confused for a while about the differences between the patterns Factory Method and Abstract Factory. Been doing a lot of research, still confused. I have one question: Is the only ...
2
votes
2answers
3k views

Factory Method vs. Factory Method design pattern

I am currently studying design patterns from the book (which, although I didn't try anything else, I find excellent) Head First Design Patterns. I'm confused about two concepts: The Factory Method, ...
1
vote
1answer
1k views

Why can't the Factory Method pattern create a family of objects? [duplicate]

There are two main differences between the design patterns Factory Method and Abstract Factory. Difference 1 is that Factory Method is mainly based on inheritance. A class in a way uses it's subclass ...
4
votes
3answers
2k views

Why should one subclass instead of composite when using the Factory Method design pattern?

I'm currently learning about design patterns. I learned about the Factory Method pattern. This pattern means that in order to implement a factory to create objects, one should subclass the class that ...
23
votes
3answers
34k views

Static factory vs factory as a singleton

In some of my code, I have a static factory similar to this: public class SomeFactory { // Static class private SomeFactory() {...} public static Foo createFoo() {...} public ...
2
votes
1answer
368 views

Is a Factory class still a Factory class if the objects it returns already exist?

I can't decide what to name my class. So far I've labelled it up as a Factory, but I am not sure. Here is the class. As you can see, it exists to return a concrete type of an Interface (...