The constructors tag has no usage guidance.
2
votes
5answers
202 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 ...
0
votes
1answer
60 views
How do you write super() in the UML format?Is it correct to write super(parameter: type): return type in the UML methods section?
Suppose you write a subclass that extends to a certain class and in that subclass, you use the super() method for your constructor.
Would you write "super(parameter: type): return type" in the UML?
2
votes
3answers
83 views
Calling a constructor from a parent class in a derived class
I'm trying to create a parent class with a constructor that takes a single int as a parameter. I also need to derive a child class that creates two instances of the parent class using a constructor ...
5
votes
2answers
91 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 ...
0
votes
3answers
269 views
Java constructors confusion? [closed]
public class example
{
private String one;
private String two;
public example(String one, String two)
{
this.one = one;
this.two = two;
}
public static void ...
1
vote
1answer
63 views
Why does JavaScript console.log of objects sometimes show prototype/constructor pattern forever
I've always noticed this, but never actually understood what's happening here. I have a fairly simple object that I've put in a console.log. It has a seemingly never ending pattern of prototype -> ...
7
votes
1answer
96 views
Is the complexity needed to prevent downcasting from constructor to overridden method worth it?
Invoking non-final instance methods in constructors risks downcasting from a constructor to an overridden method as such:
public class Start {
public static void main(String[] args) {
try ...
3
votes
1answer
162 views
Why are SameAsClassName() constructors deprecated in PHP7?
According to the PHP5 manual, __construct() was introduced in PHP5 as a replacement for SameAsClassName() constructors, and from PHP7, the old style is marked as deprecated, so in future versions, ...
0
votes
3answers
133 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 ...
0
votes
2answers
79 views
Should “default constructor” mean the compiler-generated one and “no-argument constructor” mean one you create?
To me, the word "default" means what happens if I do nothing. So I feel that the "default constructor" should refer only to the one that the compiler provides if I do not write any. That makes it ...
2
votes
3answers
210 views
C++ Virtual destructors used only when there are virtual functions
This is from Effective C++ (Meyers):
Classes not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors
I don't understand why ...
1
vote
1answer
135 views
How do constructor parameters of a MVC Controller get set?
How does the construction injection work?
I have the following code:
public class AdvancedSearchController : Controller
{
private EmployeeController _employeeController;
public ...
2
votes
0answers
18 views
What reasons would someone separate instantiation and data loading? [duplicate]
I was wondering what the purpose of separating the instantiation logic and the data loading logic of a class that loads data into memory if the class is a one time use.
Here is an example of what I ...
15
votes
3answers
1k 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 ...
5
votes
3answers
132 views
Stubbing Properties with private setters for tests
We have the object
public class MyObject{
protected MyObject(){}
public string Property1 {get;private set;}
public string Property2 {get;private set;}
public string Property3 ...
0
votes
1answer
109 views
Is it always better to use __construct() in CodeIgniter's Models and Views, even if it's not needed? [closed]
Sometimes we have Models and Controllers where the function __construct() just doesn't do anything, but is sitting on the top of all the methods in CodeIgniter's models and controllers. A lot of ...
7
votes
3answers
755 views
How to deal with constructors in large data classes [duplicate]
Several times now I have come across the situations where you have some kind of settings class that simply contains a mass of data. Often these classes are simply not valid without at least most of ...
0
votes
4answers
78 views
Public and Private Constructors with Equivalent Signatures
Here is the problem illustrated using an example of an immutable class. A Book must have at least one of a title and an ISBN.
public class Book
{
private readonly string _title;
private ...
3
votes
2answers
246 views
When to use Constructor and when to use Collection Initializer?
I am having a .Net class which has 10 properties like given below: [the datatypes of individual property is just a placeholder here and it can be anything from a primitive type to an object to a list ...
3
votes
2answers
82 views
How can I roll-back the execution of a constructor while building a complex data structure?
I'm building a complex tree of objects. There are a total of five types, A, B, C, D, and E. There is a single instance of A, which is the root node. A has one or more Bs as children, each B has one or ...
7
votes
1answer
204 views
Is a large static initializer a code smell?
I am extending SimpleExpandableListAdapter in Android. I don't think Android's adapter is implemented very well, in that its constructors have a large number of rather complicated arguments and it has ...
3
votes
4answers
519 views
Why can't I call a constructor in itself?
I am currently porting the class NumberRange from Java to C#.
I am writing this constructor and I wonder whether I can call a constructor in itself. Something like this:
public NumberRange(Double ...
1
vote
1answer
82 views
Pass data into a constructor or instead create virtual methods?
In an object oriented module describing a database, should I pass DB description data structures to the constructor in the constructors of derived classes, or should I instead create ("virtual" that ...
0
votes
1answer
97 views
Should I put the parameters in constructor or in method? (Python 3)
I have the following code:
def __init__(self, vocable_file_path, xsd_file_path, word_list_file_path):
self.vocable_file_path = vocable_file_path
self.xsd_file_path = xsd_file_path
...
28
votes
5answers
4k views
Should I initialize C structs via parameter, or by return value? [closed]
The company I work at is initializing all of their data structures through an initialize function like so:
//the structure
typedef struct{
int a,b,c;
} Foo;
//the initialize function
...
7
votes
4answers
326 views
Can I make my constructors less repetitive?
I'm extending a class with 10 different constructors. The new subclass, SpecialImage, is used like this:
SpecialImage specialImage = new SpecialImage(..);
// Leverage the Rotate() method of ...
2
votes
2answers
252 views
interpretation of the statement in java
I am confused over the interpretation of the following statement
Fruit x= new Fruit();
My interpretation is that the new operator along with Fruit(); creates a new object somewhere in the memory.
...
1
vote
2answers
139 views
Should constructors for API classes ever be public, or can a factory always be used? [closed]
I'm surprised this doesn't seem to have been asked before, or at least I can't find it anywhere. I know this is more of a philosophical question, but is there any particular reason not to use a ...
0
votes
2answers
150 views
Usage of objects in multiple containers in C++
In cases where there is a desire for objects to be "shared" between multiple containers, I would like to know what is the best practice in C++. For example, when implementing some path algorithms ...
-2
votes
1answer
272 views
pass a variable as parameter to constructor c# [closed]
pass a variable as parameter to constructor as i want to change the variable value with changeable value
i'm new to c# so that i tries to implement one thing in many way i did what i want to do with ...
0
votes
1answer
242 views
C# Constructor and private LINQ to SQL members
I am extending an application I have developed so that it is more broadly useful for multiple jobs, rather than the single job I created it for. There are a number of tables that I get from SQL ...
0
votes
1answer
154 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 ...
0
votes
1answer
137 views
How can I condense a case class with ~20 constructor arguments into 5 or 6 arguments in Scala?
Let's say I have a case class with ~20 constructor arguments? This is obviously very clunky to type. What would be the best way to condense these arguments into maybe 5 or 6 arguments? There are some ...
14
votes
4answers
2k views
How does one keep argument counts low and still keep third party dependencies separate?
I use a third party library. They pass me a POJO that, for our intents and purposes, is probably implemented like this:
public class OurData {
private String foo;
private String bar;
private ...
-3
votes
1answer
131 views
How should I change in get method without calling getList() of Deque? [closed]
import java.util.List;
import java.util.ArrayList;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
...
2
votes
2answers
141 views
Initialize physical resources in constructor
Is it an acceptable practice to initialize physical/external resources from a constructor when the resource is needed for the object to do it's work?
For instance, let's say I wanted to create an ...
1
vote
1answer
394 views
Constructor vs casting operator
I'm programming a library (so I have complete access to all the mentioned classes). Two classes (A and B) are essentially the same and differ only by their implementation, so they can be easily ...
0
votes
1answer
127 views
Query on usage of 'Window()' default constructor from java.awt
I had been through this query before asking this question.
In class Window we have constructor with default direct access level package private but not private as shown below:
Window() throws ...
1
vote
1answer
347 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 ...
56
votes
9answers
18k views
Why should I use a factory class instead of direct object construction?
I have seen the history of several С# and Java class library projects on GitHub and CodePlex, and I see a trend of switching to factory classes as opposed to direct object instantiation.
Why should I ...
5
votes
1answer
409 views
Decoupling when constructors have non-contractual parameters
By non-contractual parameters, I mean parameters that are not interfaces or service dependencies, something like class Person(string name).
I am writing a webpage scraping application, and so far ...
2
votes
2answers
219 views
Wrapping constructor arguments
Given a base class with around 25 or so subclasses, I have found that adding an argument to the base constructor is painful.
protected AbstractController(Service1 s1, Service2 s2, Service3 s3){ ... }
...
0
votes
1answer
113 views
Constructors, Ignore Arguments
Is it possible to have a constructor in a class, that you can ignore certain arguments/parameters?
For example, I have this constructor
Car(color, make, model, wheels, type, doors)
Could I call ...
14
votes
1answer
910 views
Is it fine to make a default constructor unusable?
Specifically asking about the default constructor
Given that the constructor initializes all the data for an object, if I create a class that can't be used without proper initialization, is it not ...
18
votes
5answers
2k views
How can you decompose a constructor?
Lets say I have a Enemy class, and the constructor would look something like:
public Enemy(String name, float width, float height, Vector2 position,
float speed, int maxHp, int ...
6
votes
4answers
440 views
Where should an object in CQRS+ES be fully initialized: in the constructor, or when applying the first event?
There appears to be widespread agreement in the OOP community that the class constructor should not leave an object partly or even fully uninitialized.
What do I mean by "initialization"? Roughly ...
10
votes
2answers
2k views
how complex a constructor should be
I am having a discussion with my co-worker on how much work a constructor can do. I have a class, B that internally requires another object A. Object A is one of a few members that class B needs to do ...
1
vote
3answers
892 views
Explicitly writing default empty constructor
Does it make sense to write default constructor when it has no arguments, empty body and no other constructors exist?
The only benefit I can think of is reducing the risk of forgetting to add the ...
5
votes
2answers
352 views
Constructor Injection, Single Responsibility and List initialisation
Given the class:
public class Foo : IFoo
{
private IBarRepository repository
public Foo(IBarRepository repository)
{
this.repository = repository
}
public IList<IBar> Bars { ...
3
votes
1answer
2k views
Initializing properties that aren't parameters in a constructor: alternatives
A class loads properties from a file and uses those as it performs its duties. For testing purposes I want to be able to modify said properties directly without having to use a test properties file. ...