Refers to structural definition of class unit in object-oriented languages.
Questions tagged [class-design]
1125 questions
7
votes
7 answers
Avoid excessive function parameters: class-centered or function-centered approach?
How would you fix the following bad code that passes too many parameters around?
void helper1(int p1, int p3, int p5, int p7, int p9, int p10) {
// ...
}
void helper2(int p1, int p2, int p3, int p5, int p6, int p7, int p9, int p10) {
//…

Frank
- 64,140
- 93
- 237
- 324
7
votes
5 answers
Class usage in Python
I write a lot of scripts in Python to analyze and plot experimental data as well as write simple simulations to test how theories fit the data. The scripts tend to be very procedural; calculate some property, calculate some other property, plot…

Nope
- 34,682
- 42
- 94
- 119
7
votes
5 answers
Implementing clone() for immutable classes
I am developping a class library.
I have an abstract base class Matrix for matrices that provides implementations for some of the basic methods.
Derived from Matrix are concrete subclasses for different types of matrices.
I have the requirement…

Axel
- 13,939
- 5
- 50
- 79
7
votes
1 answer
How to design class around null values from database?
In my class I have these setters/getters:
public int Id { get; set; }
public String ProjectName { get; set; }
public String ProjectType { get; set; }
public String Description { get; set; }
public String Status { get; set; }
public DateTime…
user290043
6
votes
3 answers
How to rewrite C++ code that uses mutable in D?
If you needed to rewrite the following C++ code in D, how would you do it?
struct A{
const S* _s;
B _b;
C _c;
mutable C _c1, _c2;
A(const B& b, const C& c, const S* s){ /*...*/ }
void compute(const R& r) const
{
…

Arlen
- 6,641
- 4
- 29
- 61
6
votes
3 answers
Varieties of @interface declarations, some with parentheses
I've noticed a variety of @interface declarations for Objective-c classes. I'd like to understand why developers declare @interface in the following ways:
// in the .h file
@interface MyClass : NSObject
// ...
@end
// in the .m file (what's the…

SundayMonday
- 19,147
- 29
- 100
- 154
6
votes
1 answer
"is A" VS "is Like A" relationships, what does each one mean and how do they differ?
First an example to discuss:
class Foo
{
// Attributes:
int attribute1, attribute2;
// Methods:
virtual void Foo1()
{ /* With or without Implementation */ }
virtual void Foo2()
{ /* Also with or without…

Tamer Shlash
- 9,314
- 5
- 44
- 82
6
votes
3 answers
Critique of immutable classes with circular references design, and better options
I have a factory class that creates objects with circular references. I'd like them to be immutable (in some sense of the word) too. So I use the following technique, using a closure of sorts:
[]
type Parent() =
abstract Children :…

Daniel
- 47,404
- 11
- 101
- 179
6
votes
1 answer
Passing the context around in a C# class library, looking for an "easy" way without using static
For a library (.NET Standard 2.0), I designed some classes that look roughly like this:
public class MyContext
{
// wraps something important.
// It can be a native resource, a connection to an external system, you name it.
}
public…

Teem Porary
- 202
- 1
- 10
6
votes
3 answers
Accessing a static property of a child in a parent method - Design considerations
I have a similar problem to the post Accessing a static property of a child in a parent method. The preferred answer hints that the design of the classes is faulty and more information is needed to discuss the problem.
Here is the situation I want…

Andreas Walter
- 826
- 1
- 10
- 24
6
votes
4 answers
How to deal with the idea of "many small functions" for classes, without passing lots of parameters?
Over time I have come to appreciate the mindset of many small functions ,and I really do like it a lot, but I'm having a hard time losing my shyness to apply it to classes, especially ones with more than a handful of nonpublic member…

Erius
- 1,021
- 8
- 21
6
votes
5 answers
Create one object instance per class in hierarchy
I was asked this question in an interview.
There are 3 classes A, B extends A & C extends B. We have to design these classes conforming to these constraints
Client can instantiate only one instance of A, one instance of B & one instance of C using…

Master Chief
- 2,520
- 19
- 28
6
votes
5 answers
Interview question on Class design
Recently I Attended an interview. This question was asked.
This is the scenario.
We have two type of employees. Regular and contract employee.
Regular employees will be paid on a fixed basis at the end of the month.
Contract employees will be paid…

Ananth
- 63
- 1
- 3
6
votes
2 answers
Is using @annotations to tag listener methods rather than an interface w/ methods a good idea?
Firstly yes - this is subjective.
I have noticed lately a few libraries seem to be using the let the user tag listener methods in some class as listeners using different annotations to note different events. Infinispan and WELD come to mind as…

mP.
- 18,002
- 10
- 71
- 105
6
votes
3 answers
How to model cycles between immutable class instances?
Immutable classes are great but there is one big problem i cant think of a sensible way to solve - cycles.
class Friend {
Set friends();
}
How does one model Me having You as a friend who in turn has me as a Friend back…

mP.
- 18,002
- 10
- 71
- 105