Stack Exchange Network

Stack Exchange network consists of 174 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
Join us in building a kind, collaborative learning community via our updated Code of Conduct.

In object-oriented programming (OOP), a factory is an object for creating other objects.

3
votes
4answers
367 views

Is Abstracting your code too much a bad use of SOLID Principles?

After an unresolved argument with a friend I decided to ask the stack overflow community. Is there something like abstracting your code too much? Which of the following is the better option. We come ...
0
votes
4answers
132 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* ...
2
votes
1answer
107 views

Domain driven design - factory with cross-cutting concerns

I was recently tasked with creating a factory that processes webhook events. Any authorized application in the solution (1 app per domain) can post any meaningful payload and as long as the event's ...
1
vote
2answers
117 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 ...
1
vote
0answers
62 views

Are constructors with complex initialization logic always bad? [duplicate]

I've recently read this blog post regarding what a constructor should do and I am also reading Eric Evans' book on Domain Driven Design. Both the blog post and the book state that a constructor ...
0
votes
0answers
56 views

Unit of Work, Factory and Lifetime Management

Yesterday, an argument arose in the office, on the usage of a unit of work versus a DbFactory producing these. To give a bit of a context, we have a (loosely) service-oriented architecture, following ...
2
votes
0answers
48 views

Best design pattern/approach for a Factory which needs multiple types of input

I want to develop a Factory of INotifications. public interface INotification { string Text { get; } string UserId { get; } NotificationType NotificationType { get; } } The problem is, ...
2
votes
2answers
125 views

With the Static Factory Constructor design pattern is there a preferred way to remove “all” object references?

Consider the following implementation of a static factory pattern... public class Names{ private static final Map<String, Name> NAME_MAP = new HashMap<>(); private final String firstName, ...
1
vote
2answers
154 views

Picking a concrete type based on a configuration parameter

I have a code base that contains several different instances of the following pattern, the key point of which is that the concrete type of the class is decided at run time based on a string from a ...
0
votes
0answers
78 views

How to manage Factory Command mapping?

I have this project that looks like the JDO API. The API defines very complex object that have relatively simple interfaces (those complex objects manage a storage source like a DB and simply provide ...
2
votes
1answer
200 views

How do I create front end test data based on backend model's schema?

I'm really struggling with creating robust tests for my frontend because of the coupling in data schema between my backend and frontend. To clarify, I have a Django rest API and I have a front end ...
1
vote
2answers
159 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 ...
3
votes
1answer
172 views

What is the generic implementation of design pattern IoC coupled with Factory for DAO? [closed]

I'm learning the java Spring framework. So far, I understood that Spring will make stuff transparent with its configurations so that IoC and Factory are not too complicated to implement... Now I have ...
3
votes
3answers
143 views

Constructor for factory expecting instance used only in products

I have a factory that creates products. To do this I need an instance of some other class, that has nothing to do with the actual factory. However all Products should of course use the same instance ...
0
votes
2answers
146 views

How to handle Factory-like structures with a huge amount of classes?

I have the issue that I want to be able to capture a set of SNMP messages, and some of them I want to parse to create an instance of a user-defined class. I may for example want to identify whether an ...
1
vote
2answers
746 views

Should a factory always create a new object

we have a hierarchical structure where every node is derived from a node base class public Node(INodeFactory nodeFactory). The factory is injected so the node can create its child nodes using Get(int ...
0
votes
2answers
185 views

Add method to interface for only one implementation

I have a factory for an interface with different implementations. But one implementation needs an extra method which the others don't need. How do I solve this without implementing "not implemented ...
0
votes
0answers
185 views

Advice on Factory pattern with DI implementation

I am looking on ways to improve the following code: public interface IExample{ void Do(); } public interface IExampleA: IExample {} public class ExampleA: IExampleA { IDependencyA ...
1
vote
2answers
124 views

How to combine these fill and create methods or utilize generics in factory?

Domain object "Contragent" Let's say I have an hierarchy of classes: public class BaseContragent { public int Id { get; set; } } public class PersonContragent : BaseContragent { public string ...
3
votes
1answer
117 views

Can I create a Factory method which holds an internal reference to an instance of B?

Given two classes: class A { B* b_; A(B* b){ b_ = b; } } class B {} B is a class that should be used only inside A so I would like not to expose its creation to whatever class ...
1
vote
2answers
56 views

Class member declared only to be passed to other class

Given three classes: class A { B* b_; A(B* b){ b_ = b; } void doThings() { C* c = new C(b_); ... // Use c } } class B {} class C { B* b_; ...
0
votes
2answers
90 views

Creational Pattern for creating or returning an object from other objects

A problem I've come across and never really had a good answer for, is: what is the correct design pattern for creating or returning an object from another object, or multiple objects? Traditionally I'...
1
vote
3answers
2k views

Static Method in Interface

I'm working on software to play back log files. We have different types of logs from different providers. I've created an interface that different classes will implement to play different logs. For ...
3
votes
1answer
3k views

Is mixing Builders and Factory pattern a good idea?

I have an object Carconstructed using builder pattern. Issue was a lot of code redundancy. Car.Builder builder = new Car.Builder("Mercedes"); builder.numDoors(carConfig.getNumDoors() ...
1
vote
2answers
160 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 ...
7
votes
3answers
202 views

Are we “allowed” to use methods on JavaScript maps (the data type) and if so are there any new rules?

JavaScript maps (the data type, not the array method) seem set up to accept data (key/value pairs) but not necessarily methods. At least they're not advertised that way. However, we can put methods ...
4
votes
1answer
108 views

What alternatives are there to using an object of a contravariant interface polymorphously?

The gist of what I'm trying to do is get an instance of the appropriate user service, then pass it whatever subtype of User we're working with. Models: public class User { public string ...
7
votes
2answers
220 views

Gateway Pattern and Composition

I often write database intensive applications and discovered the gateway pattern which seemed to fit my needs. My problem now is that many of my models are compositioned of other models. For example ...
8
votes
3answers
407 views

DI/IoC container vs factories: Where do I configure my application and why?

I am trying to figure out when to use the DIC/IoC registry for configuring my software and when to use factories, along with the reasoning behind either approach. I am using StructureMap as my DI ...
0
votes
1answer
446 views

How can you have a Repository with dynamic data-sources?

Usually the Repository pattern looks like : What would be the best practice to make the repository handle more gateways (for Cache Redis, FTS/filtering Elasticsearch, Db Mysql etc.) My ideas so far ...
2
votes
1answer
124 views

Working with Dependency Injection using Factory pattern

I want to build a .jar that encapsulates a vendor api, so we can use our own objects as parameters to communicate with the api. I've read some articles and topics here in SO, but I still am a bit ...
0
votes
1answer
42 views

Factory injection into factory created object

public class Factory { public INode CreateNode(TypeEnum type) { switch(type) { case TypeEnum.Document: return new DocumentNode(this); case ...
2
votes
1answer
103 views

How to keep IDs in sync when using Factory?

abstract class Product { public abstract Product createProduct(); ... } class OneProduct extends Product { ... static { ProductFactory.instance().registerProduct("ID1", new OneProduct(...
3
votes
4answers
2k views

Are factories that only return one type of object bad?

Someone in a Stackoverflow post (I didn't bookmark the question unfortunately) commented, that Factories that only return one type of object are a code smell. I find myself writing these kinds of ...
2
votes
2answers
537 views

Can a single Factory be used to create similar in purpose, but different in focus objects?

I am concerned about whether or not I am using FactoryMethod correctly. Background: I am refactoring legacy code and I have identified a pattern: there is a God Object that is created in memory,...
1
vote
3answers
140 views

SOLID principles and generating many objects from a file

The "D" in SOLID stands for dependency inversion. If I have for example, a Blacksmith class that depends on Hammer, I should create my Hammer externally and pass it to Blacksmith, rather than having ...
1
vote
1answer
388 views

What is the DRY-est factory pattern to instantiate subtypes based on one of their properties' values?

Take the following class hierarchy: Client FacebookClient PinterestClient TwitterClient Each client must define a value for an enum property named ClientType (string property Url in the original ...
3
votes
1answer
1k views

Project structure: Where to put object factories

I am working on a C# programming project in Visual Studio. I have created various VS library projects inside the VS solution containing the various components of the solution. Without giving it too ...
2
votes
3answers
652 views

Pattern to use (if any) to co-ordinate loosely coupled classes with strong interdependencies

I have a collection of cooperative classes whose behaviors are interdependent upon one another. But I wish to keep them loosely coupled, so I've created appropriate interfaces. I want to determine an ...
1
vote
1answer
90 views

Rewriting conditionals in OOP without generics

Let's say I have a set of objects, foo f; bar br; baz bz; I also have a string of JSON data, string JSONstring; Depending on the object type of the JSON string, I need to transform it into ...
1
vote
1answer
216 views

Making more than one instance of factory that is supposed to be singleton

However, there are some classes that should have only one instance. Sometimes they are factories, which you can use to create the other objects in the system. If more than one factory exist, ...
0
votes
0answers
123 views

Type checking in factory, bad practice?

I'm working on an existing code base and am trying to reduce a lot of duplication and gain some polymorphic behavior. Right now I have a type of domain object with an abstract base class and two (at ...
0
votes
1answer
157 views

Could this class be considered as a Factory class? [closed]

I've recently worked on a reusable network service class for a service-aggregator iOS app. This class should retry a failed request if it was caused by expired user token. Plus, this class will be ...
3
votes
2answers
223 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 ...
7
votes
5answers
4k 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 ...
4
votes
1answer
111 views

Should domain model be intelligent or there must be services, which operate it?

I'm building a web-app for a licensing service. My domain models are Licenses, and they can be of two types at the moment: server and client license. They are almost same, except server license has ...
3
votes
1answer
4k views

Usage - Factory Pattern vs Dependency Injection [closed]

Okay, this question is not on which is better. It is more about when to use Dependency Injection vs Factory. I have done a bit of research and it goes like this. DIP: Provides loose coupling, ...
4
votes
1answer
267 views

Understanding factories

I know there are hundreds of questions about this on here. I've probably read fifty different questions, blog posts and textbooks. The problem is I've gotten about 75 different answers. I have seen ...
1
vote
1answer
375 views

Should factory make context or concrete strategies objects in strategy-factory pattern?

In a typical strategy pattern class Strategy { public: virtual int execute() const = 0; } class StrategyA : public Strategy { public: int execute() const override; } class StrategyB : ...
1
vote
1answer
122 views

Should make concrete factories in the same count of families of objects or according to stages in my problem?

I am programming a game. In this game I have 3 stages. In the first stage, there is a static goal and a static shooter in the game. In the second stage, the goal will move. And in the third one both ...