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 ...
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 ...
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 ...
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, ...
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.
// ...
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);
...
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....
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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.
...
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 ...
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 ...
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 ...
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.
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
factory × 109design-patterns × 49
c# × 25
object-oriented × 16
dependency-injection × 14
design × 13
factory-method × 13
object-oriented-design × 12
java × 10
abstract-factory × 10
domain-driven-design × 9
c++ × 7
builder-pattern × 7
factory-pattern × 7
architecture × 5
php × 4
interfaces × 4
solid × 4
repository × 4
unit-testing × 3
refactoring × 3
patterns-and-practices × 3
single-responsibility × 3
constructors × 3
singleton × 3