Questions tagged [class-design]

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

1125 questions
8
votes
2 answers

Writing mocks/stubs for an object before you have written the class for that object?

I'm designing a class that has two dependencies. One of the dependency classes has been written and tested. The other has not yet been written. It has occurred to me because the remaining dependency will be written to facilitate the class that will…
Lewis Bassett
  • 1,299
  • 1
  • 11
  • 17
8
votes
4 answers

How to emulate __destruct() in a static class?

I've coded a simple configuration class for my own framework. There are simple functions like get(), set() or loadFile(). But all functions and variables are static. And now I want to implement an autosave mechanism. I had the idea to create an…
ComFreek
  • 29,044
  • 18
  • 104
  • 156
8
votes
4 answers

Design dilemma: who should handle disposable parameter?

If my class uses disposable resource in it's constructor (DbConnection if it matters) should I implement IDisposable in my class and dispose DbConnection object, or let user handle disposal of DbConnection? Currently I implement IDisposable in my…
Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96
8
votes
14 answers

Coding guides: How do you split up your large source files?

The project I'm working on has just hit 4200 lines in the main C# file, which is causing IntelliSense to take a few seconds (sometimes up to 6 or so) to respond, during which Visual Studio locks up. I'm wondering how everyone else splits their files…
Nick Devereaux
  • 813
  • 1
  • 7
  • 22
8
votes
3 answers

Dealing with id's in entity object design

For a while I have been thinking about how to deal with objects which are assigned identifiers by the database. A typical object representing a table entity may look like: public class Test { public int Id { get; private set; } public string…
DEHAAS
  • 1,294
  • 11
  • 17
8
votes
3 answers

Sending Arguments Via Event Handler?

So I'm not actually sending arguments, but setting a class variable to a certain value, then using it again in another method. Is this the "best practice" way to do things? If not, I'd be interested in learning the correct way. Thanks! Can/Should…
sooprise
  • 22,657
  • 67
  • 188
  • 276
8
votes
1 answer

TypeScript: Namespace vs Class

Well it was harder then I thought but I figured out how to encapsulate my code using a namespace I already know how to do it with a class (I’m coming from C# world) And for the question, I had a small class that just needed an initiation and then…
Yitzchak
  • 3,303
  • 3
  • 30
  • 50
8
votes
2 answers

Why does Exception take Throwable as a constructor parameter instead of Exception?

I noticed recently that Exception has several constructors which take Throwable as a parameter. Throwable has two subclasses, Error and Exception, and generally all documentation indicates that you should not attempt to catch or handle an Error.…
8
votes
5 answers

Performance of Singleton Class Instance Method vs. Static Class Method in PHP?

I'm interested in objective analysis of which is more performant; calling instance methods of a singleton class or methods of a static class. I've already seen this so I'm not looking for a discussion about the difference between the two or a…
MikeSchinkel
  • 4,947
  • 4
  • 38
  • 46
8
votes
2 answers

Object oriented n-tier design. Am I abstracting too much? Or not enough?

I'm building my first enterprise grade solution (at least I'm attempting to make it enterprise grade). I'm trying to follow best practice design patterns but am starting to worry that I might be going too far with abstraction. I'm trying to build my…
max
  • 179
  • 2
  • 10
8
votes
14 answers

Is it considered bad design to do lengthy operations in a constructor?

I am implementing a class to compare directory trees (in C#). At first I implemented the actual comparison in the class's constructor. Like this: DirectoryComparer c = new DirectoryComparer("C:\\Dir1", "C:\\Dir2"); But it doesn't feel "right" to do…
Mikael Sundberg
  • 4,607
  • 4
  • 32
  • 36
8
votes
5 answers

Calling method that exists in child classes but not in parent class

public class Parent { .... } public class Child1 extends Parent { .... public void foo() { .... } } public class Child2 extends Parent { .... public void foo() { .... } } Here method foo() only exists…
Learner
  • 1,503
  • 6
  • 23
  • 44
8
votes
8 answers

Is it good practice override methods with a higher visibility?

Answering this question: How to GUI - Using paintcomponent() to initialize a GUI and then to add GUI based on mouse I've stated this: You don't override paintComponent() properly. This is a protected method, not public. If you add @Override…
dic19
  • 17,821
  • 6
  • 40
  • 69
8
votes
4 answers

Implementing IEquatable where T is an interface

I have a domain model that is trivially represented by the following. IMyInterface ClassA : IMyInterface ClassB : IMyInterface What I want is to implement IEquatable in such a way that I can write code something…
Matthew Vines
  • 27,253
  • 7
  • 76
  • 97
8
votes
7 answers

Variable-length objects: Ever a good idea?

My application uses a large amount of Panda objects. Each Panda has a list of Bamboo objects. This list does not change once the Panda is initialized (no Bamboo objects are added or removed). Currently, my class is implemented as follows: class…
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281