Skip to main content
45 votes

Is it Good Practice to Only Expose Interfaces

As is the nature of using interfaces, this decouples my users from being concerned with how I variously decide to refactor the implementations. Let's take a step back and make sure we understand what ...
JimmyJames's user avatar
  • 31.5k
25 votes
Accepted

Should I use the Factory Pattern when instantiating objects with very different constructors?

You have a solution looking for a problem, that is why you run into trouble. A factory method is not an end in itself, it is a means to an end. So you need to start identifying the problem you want to ...
Doc Brown's user avatar
  • 222k
23 votes
Accepted

Is it Good Practice to Only Expose Interfaces

While this may be an opinion-based question, and the real answer is the same as with many endeavors in software design and development ("it depends"), I'm going to say yes, do not expose ...
Jesse C. Slicer's user avatar
16 votes
Accepted

What is a SOLID way for creating objects dynamically using a factory?

Adding new methods to a class is a perfectly fine way to evolve code. If this is not considered SOLID then SOLID (or at least this interpretation of SOLID) is the problem, not the code. That said, ...
JacquesB's user avatar
  • 62.4k
11 votes

Is it Good Practice to Only Expose Interfaces

In addition to the points already mentioned by JimmyJames, there's one additional drawback in exposing interfaces rather than classes: Your consumers can make their own classes implement them, e.g. // ...
Heinzi's user avatar
  • 9,868
10 votes

Should I use the Factory Pattern when instantiating objects with very different constructors?

Factory class Use a factory class, which can have several methods. The factory should have its own interface. interface IShapeFactory { IShape CreateRectangle(float width, float height); ...
John Wu's user avatar
  • 27k
10 votes
Accepted

When to use Factory design pattern instead of Dependency Injection?

When to use Factory design pattern instead of Dependency Injection? (Emphasis mine). Never, as they aren't mutually exclusive. A factory provides an instance of an object according to a set of rules....
David Arno's user avatar
  • 39.7k
9 votes
Accepted

Static Factory Methods vs Constructors

Constructors are the expected way to well... construct a new object. If I'm creating a new object, the first thing I'll do is type in ClassName x = new ClassName( and see what my IDE suggests to me as ...
Philip Kendall's user avatar
9 votes

Is it a good idea to return a Builder from a Factory?

As always, you can do what is best suited to your domain and concrete problem. In any case, this looks like mixing a concrete factory with the Bloch builder pattern. In this case, I do not see the ...
sfiss's user avatar
  • 954
8 votes
Accepted

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

I would side with your enemy on this one. The 'create a database and table logic' is clearly technically a separate responsibility from the 'get data from database' logic. You can imagine the creation ...
Ewan's user avatar
  • 86.7k
8 votes
Accepted

What's the benefits to use an abstract factory when using interfaces is already suffice?

I'm not sure yet another answer is needed here, but you asked for your code to be modified to explain the purpose of factories. So perhaps the following will help. Firstly, I'm going to change your ...
David Arno's user avatar
  • 39.7k
8 votes

What is a SOLID way for creating objects dynamically using a factory?

I would need to continue expanding this factory class, which violates SOLID and could lead to quite a lengthy class. Or you could not do that. Lets see how your solutions hold up to some requirements ...
candied_orange's user avatar
7 votes

Does the Factory Pattern violate the Open/Closed Principle?

The pattern itself doesn't violate the Open/Closed Principle (OCP). However, we violate the OCP when we use the pattern incorrectly. The simple answer to this question is as follows: Create your base ...
hfontanez's user avatar
  • 221
7 votes

I need help solving a common architectural problem with multiple concrete classes implementing an interface

One of the most powerful design techniques is to pay attention to what you know when. Following this can help keep your design simple. You might be setting your dependencies in the wrong place. ...
candied_orange's user avatar
7 votes

What's the benefits to use an abstract factory when using interfaces is already suffice?

Factory methods have a number of advantages. Mainly, they avoid the inbuilt limitations of constructors (can only have one name, cannot use caching, etc.). Entire Factories are used mainly to ...
Kilian Foth's user avatar
7 votes
Accepted

Is it a code smell for a factory to have many methods?

With method names like "createWithImproperZipCode" and "createWithPurelyNumericAddress", you have made something pretty specific. Usually logic like this gets used in multiple ...
Greg Burghardt's user avatar
6 votes

Is it a good idea to return a Builder from a Factory?

A Factory normally returns different implementations of a class/interface, while a builder gives a fluent interface for configuring a single type. So if you factory returns a builder you might expect ...
Ewan's user avatar
  • 86.7k
5 votes

Picking a concrete type based on a configuration parameter

Yes, Factory is the right name for this pattern. No, C++ does not offer a way to enumerate types. You can perhaps reduce the necessary boilerplate with some clever macros, but I would not suggest it. ...
Sebastian Redl's user avatar
5 votes
Accepted

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

In your factory implementation you cache all name objects created, returning the same instance if a new name with the same first and last name string is requested. When you create a cache like this ...
Ewan's user avatar
  • 86.7k
5 votes

When to use Factory design pattern instead of Dependency Injection?

David Arno's answer is upgrading you from a static factory to abstract factory. I wont argue against the added polymorphic power that gives you. But I feel compelled to point out that Foo still knows ...
candied_orange's user avatar
5 votes
Accepted

I need help solving a common architectural problem with multiple concrete classes implementing an interface

Breaking down the problem What you've done right is identified that all exporters work the same. As a basic example (which I will expand on), this means something along the lines of: IExporter ...
Flater's user avatar
  • 59.9k
5 votes

What's the benefits to use an abstract factory when using interfaces is already suffice?

There are different types of factories that serve different use cases. Often, you want to create objects of different (sub-) classes depending on runtime arguments. For this case, a simple function or ...
pschill's user avatar
  • 2,050
5 votes

Is it a code smell for a factory to have many methods?

Very few things are necessarily a code smell - the question you should be asking is "what value does this pattern bring to my code?" Does it improve the readability or maintainability of ...
Philip Kendall's user avatar
5 votes
Accepted

Seeking Clarification on the Abstract Factory Pattern

In short It's just an ambiguity in the human language. More arguments Factory methods can be abstract The pattern where a factory creates a single product is the GoF's Factory Method pattern, which ...
Christophe's user avatar
  • 82.5k
4 votes
Accepted

Should a class which has a method to create object A also implement a method to delete A?

With manual resource management, this is a must. You definitely want to provide destructor method. C is a good illustration for this, literally every allocator has accompanying destructor. With ...
Basilevs's user avatar
  • 5,168
4 votes

DDD - Factory or Service?

I suggest you actually attempt to model the actions to which you are alluding above in your application layer. Namely RegisterAccount and Login, because I think it can help bring clarity to your ...
user3347715's user avatar
  • 3,254
4 votes

Static Factory Methods vs Constructors

There are some differences for the consuming client: a factory method could return a new object, or it could lookup and return an old one.  Further, a factory method could return a subclass ...
Erik Eidt's user avatar
  • 34.8k
4 votes

I need help solving a common architectural problem with multiple concrete classes implementing an interface

Is this a sane approach to this problem? It depends. For many situations, this is sufficient and perfectly ok. If you don't create a new type of exporter every two weeks, or don't want to provide an ...
Doc Brown's user avatar
  • 222k
4 votes
Accepted

Software design/architecture of a patient simulator with multiple mathematical models

I have run into this problem many times. When the fields used are expected to change like this the best plan is to stop pretending you have a class. What you have is a key value collection. Single ...
candied_orange's user avatar
4 votes

What's the benefits to use an abstract factory when using interfaces is already suffice?

The benefit of the Factory Method pattern is in expressing the fact that a piece of code does not want/need to know how to obtain an IAnimal. In fact, interfaces are often viewed from the wrong ...
xtofl's user avatar
  • 344

Only top scored, non community-wiki answers of a minimum length are eligible