All Questions
Tagged with constructors design-patterns
17 questions
3
votes
3
answers
471
views
How to efficiently build objects without default constructors
I am trying to explain and understand a way to create object which cannot have "default" instances. Typically I want classes which represent, as closely as possible, the real-life concept I ...
2
votes
0
answers
480
views
How to handle a bunch of nested ValueObjects?
I'm writing a PHP web application (or actually a Symfony module). One part of it is a nested structure of ValueObjects (meaning: they are immutable and have to be validated on the creating). Such an ...
22
votes
4
answers
5k
views
Constructing an object: should I expose or hide parameters passed to the constructor?
I've a habit I just mechanically do without even thinking too much about it.
Whenever a constructor is waiting for some parameters, I consider this a public information that should be available by ...
6
votes
1
answer
1k
views
Approach for Constructing View Models in Complex MVVM Application
I'm struggling with the design in a WPF MVVM application. In a few courses I've taken, they say that having a lot of parameters in a constructor is a code smell, but they never address how to deal ...
-1
votes
3
answers
514
views
Constructor from interface with minimal repetition
Preface
In an application that is separated by layers or distributed by services it is common to have classes that are very closely related data-wise, but which we wish to have loosely coupled. My ...
6
votes
6
answers
5k
views
Should constructors ever be used only for side-effects?
Summary: Why is it wrong to design a constructor only for its side effects, and then to use the constructor without ever assigning its return value to a variable?
I'm working on a project that ...
3
votes
1
answer
1k
views
Handle Named constructors with factory pattern
In my current project I'm refactoring the code to get a DBAL. I have a class Entity that is the base class for all classes that model a database table. So there are several classes that inherit from ...
26
votes
5
answers
24k
views
Why did it not become a common pattern to use setters in the constructor?
Accessors and modifiers (aka setters and getters) are useful for three main reasons:
They restrict access to the variables.
For example, a variable could be accessed, but not modified.
They validate ...
0
votes
1
answer
128
views
Is it bad practice to simplify constructor dependencies using a simple container when the class represents the entry point for a sub system?
I have a System which depends on Transactions, and a few other things. This list can become very long. Transactions can be (and will be) implemented in two ways or more, so every transaction is an ...
3
votes
5
answers
16k
views
Constructor overloading or allow null?
Which is the preferred design to use, one constructor that allows null, or two constructors where one throws an ArgumentNullException on null?
Two constructors with exception throwing
public class ...
7
votes
2
answers
876
views
Design pattern for too many ctor parameters within class hierarchy
Is there some design pattern for handling situation where class hierarchy constructors parameters force the most bottom classes to have too many parameters?
Ideally this would be in C++, if language ...
27
votes
3
answers
30k
views
Constructor with tons of parameters vs builder pattern
It is well know that if your class have a constructor with many parameters, say more than 4, then it is most probably a code smell. You need to reconsider if the class satisfies SRP.
But what if we ...
27
votes
4
answers
10k
views
Legitimate "real work" in a constructor?
I am working on a design, but keep hitting a roadblock. I have a particular class (ModelDef) that is essentially the owner of a complex node tree built by parsing an XML schema (think DOM). I want to ...
-1
votes
1
answer
512
views
Multiple method calls in the constructor and dependency injection
I was asked to refactor some almost ureadable spaghetti code into object-oriented architecture.
I have some doubts regarding a class that I designed. Here is the class' skeleton:
require_once 'inc/...
2
votes
1
answer
2k
views
Patterns for subclass constructors that vary the parent class constructor slightly
So, my problem is in the context of an MVC-style approach. The code here is PHP, but I'm hoping this is a design issue independent of it.
abstract class Controller
{
private $domain;
private $...