Questions tagged [class-design]

Refers to structural definition of class unit in object-oriented languages.

1125 questions
11
votes
16 answers

How many constructors should a class have?

I'm currently modifying a class that has 9 different constructors. Now overall I believe this class is very poorly designed... so I'm wondering if it is poor design for a class to have so many constructors. A problem has arisen because I recently…
mezoid
  • 28,090
  • 37
  • 107
  • 148
11
votes
5 answers

In C#, use of value types vs. reference types

My questions are: When should we use value types and when reference types? What are the advantages and disadvantages of one over other? What if one uses reference types everywhere? Is there any harm in it? Please also discuss advantages and…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
11
votes
2 answers

PHP OOP :: Building an API Wrapper class

I have an app that is essentially a wrapper for a 3rd party API. The app does not use a database and only stores a single cookie which is the session ID that the API requires. The API is a shopping system which allows users to -login/register/edit…
James Agnew
  • 375
  • 1
  • 4
  • 17
11
votes
13 answers

Object oriented programming - class design confusion

I am trying to wrap my head around object oriented programming. My understanding is that we have objects so we can design our programs to mirror real-life objects. Let's take a class hierarchy: class Fruit { void Eat() { } } class Apple…
links77
  • 215
  • 3
  • 6
10
votes
3 answers

Setting default value for properties of Interface?

I have an Interface which contains one property. I need to set the default value for that property. How to do that?. Also is it good practice to have a default value for a property in Interface? or here using an abstract class instead is a apt one?
Mohan Kumar
  • 6,008
  • 6
  • 28
  • 36
10
votes
2 answers

Why can't you have require* statements in a class definition?

Possibly Related: Why don't PHP attributes allow functions? Pardon me if this has been asked before, but why can you not have something like the following: class foo { require_once 'defines.php'; private $_server = DB_SERVER; private…
barfoon
  • 27,481
  • 26
  • 92
  • 138
10
votes
2 answers

C++: Bad practice to use friend classes instead of writing getters / setters?

I have two classes which in one aspect work together tightly. They both use functionality of each other that should be only used by them and not by any other class. Is it bad practice if I make those two classes friends so they can directly access…
Jarx
  • 101
  • 1
  • 1
  • 3
10
votes
5 answers

Should entities implement interfaces?

I personally don't have my entities implement interfaces. For a Task class I wouldn't have ITask that just had the same properties defined on it. I've seen it done a few times though, so I'm wondering where that advice comes from, and what benefits…
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
10
votes
5 answers

C++, preventing class instance from being created on the stack (during compiltaion)

I know there are methods to prevent a class from being created on the heap, by preventing the user from using the new and delete operator. I am trying to do just the opposite. I have a class that I want to prevent the user from creating an instance…
eladidan
  • 2,634
  • 2
  • 26
  • 39
10
votes
9 answers

Is there any point to an interface if only one class implements it?

Looking at the (mature) codebase at my new job, there is an interface, and only one class implements it (as far as I can tell). Can/should I get rid of the interface?
user343587
  • 537
  • 5
  • 9
10
votes
3 answers

Python: extending int and MRO for __init__

In Python, I'm trying to extend the builtin 'int' type. In doing so I want to pass in some keywoard arguments to the constructor, so I do this: class C(int): def __init__(self, val, **kwargs): super(C, self).__init__(val) # Do…
Michael Liddle
10
votes
7 answers

Class design: arrays vs multiple variables

I have a bit of a theoretical question, however it is a problem I sometimes face when designing classes and I see it done differently when reading others code. Which of the following would be better and why: example 1: class Color { public: …
jaho
  • 4,852
  • 6
  • 40
  • 66
9
votes
4 answers

How to design a class with "annotated" fields?

Imagine we have some sort of protocol with hundreds of message types, each of which we want to model by a C++ class. Since each class should be able to process each field automatically, a natural solution is to just have an std::tuple with all the…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
9
votes
1 answer

Using self in class method

I came across this code in ShareKit, and I don't understand what the writer had in mind, using self in a class method. There is a warning: Incompatible pointer types sending 'Class' to parameter type id. I want to clean up these…
Jim
  • 5,940
  • 9
  • 44
  • 91
9
votes
8 answers

Is it true I should not do "long running" things in a property accessor?

And if so, why? and what constitutes "long running"? Doing magic in a property accessor seems like my prerogative as a class designer. I always thought that is why the designers of C# put those things in there - so I could do what I want. Of…
Cheeso
  • 189,189
  • 101
  • 473
  • 713