Questions tagged [class-design]

General guidelines on how to design classes with best known industry practices.

It is strongly recommended to follow design guidelines when developing classes and components that extend the Framework or Language features. There are different guidelines that may or may not meet your project standards. However, inconsistent design adversely affects developer productivity.

440 questions
213
votes
16 answers

When using the Single Responsibility Principle, what constitutes a "responsibility?"

It seems pretty clear that "Single Responsibility Principle" does not mean "only does one thing." That's what methods are for. public Interface CustomerCRUD { public void Create(Customer customer); public Customer Read(int CustomerID); …
Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
152
votes
9 answers

Is there any "real" reason multiple inheritance is hated?

I've always liked the idea of having multiple inheritance supported in a language. Most often though it's intentionally forgone, and the supposed "replacement" is interfaces. Interfaces simply do not cover all the same ground multiple inheritance…
96
votes
13 answers

How to warn other programmers of class implementation

I'm writing classes that "must be used in a specific way" (I guess all classes must...). For example, I create the fooManager class, which requires a call to, say, Initialize(string,string). And, to push the example a little further, the class would…
Gil Sand
  • 2,173
  • 3
  • 18
  • 22
63
votes
8 answers

Prefer class members or passing arguments between internal methods?

Suppose within the private portion of a class there is a value which is utilized by multiple private methods. Do people prefer having this defined as a member variable for the class or passing it as an argument to each of the methods - and why? On…
geoffjentry
  • 870
  • 1
  • 6
  • 8
61
votes
12 answers

Is it bad practice to pass instances through several layers?

In my program design, I often come to the point where I have to pass object instances through several classes. For example, if I have a controller that loads an audio file, and then passes it to a player, and the player passes it to the…
Puckl
  • 1,525
  • 2
  • 13
  • 17
57
votes
6 answers

Should a getter throw an exception if its object has invalid state?

I often run into this problem, especially in Java, even if I think it's a general OOP issue. That is: raising an exception reveals a design problem. Suppose that I have a class that has a String name field and a String surname field. Then it uses…
AgostinoX
  • 841
  • 1
  • 6
  • 12
48
votes
6 answers

What would be the disadvantage to defining a class as a subclass of a list of itself?

In a recent project of mine, I defined a class with the following header: public class Node extends ArrayList { ... } However, after discussing with my CS professor, he stated that the class would both be "horrible for memory" and "bad…
Addison Crump
  • 723
  • 1
  • 6
  • 10
48
votes
5 answers

When and why to use Nested Classes?

Using Object Oriented Programming we have the power to create a class inside a class (a nested class), but I have never created a nested class in my 4 years of coding experience. What are nested classes good for? I know that a class can be marked as…
46
votes
4 answers

Why prefer non-static inner classes over static ones?

This question is about whether to make a nested class in Java to be a static nested class or an inner nested class. I searched around here and on Stack Overflow, but couldn't really find any questions regarding the design implications of this…
Frank
  • 14,407
  • 3
  • 41
  • 66
37
votes
9 answers

Is creating subclasses for specific instances a bad practice?

Consider the following design public class Person { public virtual string Name { get; } public Person (string name) { this.Name = name; } } public class Karl : Person { public override string Name { get …
36
votes
5 answers

Is it a good practice to create a ClassCollection of another Class?

Lets says I have a Carclass: public class Car { public string Engine { get; set; } public string Seat { get; set; } public string Tires { get; set; } } Lets say we're making a system about a parking lot, I'm going to use a lot of the…
33
votes
3 answers

Optional parameters or overloaded constructors

I am implementing a DelegateCommand, and when I was about to implement the constructor(s), I came up with the following two design choices: 1: Having multiple overloaded constructors public DelegateCommand(Action execute) : this(execute, null) {…
user277671
25
votes
10 answers

Can we live without constructors?

Let's say in some reason all objects are created this way $obj = CLASS::getInstance(). Then we inject dependencies using setters and perform starting initialization using $obj->initInstance(); Are there any real troubles or situations, which can't…
25
votes
5 answers

Should a class know about its subclasses?

Should a class know about its subclasses? Should a class do something that is specific for a given subclass for instance? My instincts tells me that is a bad design, it seems like an anti-pattern of some sort.
24
votes
4 answers

C++ - Constructor or Initialize Method to Startup

Possible Duplicate: Avoid having an initialization method I want to determine when to do non-trivial initialization of a class. I see two times to do initialization: constructor and other method. I want to figure out when to use each. Choice…
Bob Fincheimer
  • 428
  • 1
  • 3
  • 10
1
2 3
29 30