Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP: good class design

My question is related to this one: Python tool that builds a dependency diagram for methods of a class.

After not finding any tools I wrote a quick hack myself: I've used the compiler module, I've parsed the source code into an Abstract Source Tree and I've walked it to collect dependencies between class methods. My script generated an input file for graphviz, which was used to generate a dependency graph that looks like this.

At this point I've got stuck. I've realized that I have no idea how to refactor the class to make it less complicated. I simply don't know what should I aim to. For example, in theory of relational databases there are a couple of simple rules that are used to bring a database to a normal form. What about some similar theory concerning the good class design (in terms of dependencies between its methods)? Is this topic covered somewhere so I could study it?

like image 930
Anonymous Avatar asked May 10 '09 19:05

Anonymous


People also ask

What is a good class design?

A great class hides its internals. It won't allow others to easily mutate its state. Great classes expose methods that are meaningful to the class context and what it represents. A bad class on the other hand has no methods besides the typical setters and allows each property or field to be mutated.

What is a good OO design?

Gamma's second principle of good OO design is to: Favor object composition over class inheritance. Systems that follow this rule have fewer classes and more objects. Their power is in the ways that the objects can interact with each other. It would be important to have good dynamic models.

Why is object-oriented design good?

Object-oriented programming is ultimately about taking a huge problem and breaking it down to solvable chunks. For each mini-problem, you write a class that does what you require. And then — best of all — you can reuse those classes, which makes it even quicker to solve the next problem.


2 Answers

We follow the following principles when designing classes:

  • The Single Responsibility Principle: A class (or method) should have only one reason to change.
  • The Open Closed Principle: A class (or method) should be open for extension and closed for modification.
  • The Liskov Substitution Principle: Subtypes must be substitutable for their base types.
  • The Interface Segregation Principle: Clients should not be forced to depend upon methods that they do not use. Interfaces should belong to clients.
  • The Dependency Inversion Principle: Abstractions should not depend on details. Details should depend on abstractions.

Edit: Design patterns are helpful in getting your code to comply with these principles. I have found it very helpful to understand the principles first and then to look at the patterns and understand how the patterns bring your code in line with the principles.

like image 182
Sir Rippov the Maple Avatar answered Oct 07 '22 15:10

Sir Rippov the Maple


It's often not possible to say whats 'correct' or 'wrong' when it comes to class design. There are many guidelines, patterns, recommendation etc. about this topic, but at the end of the day, imho, it's a lot about experience with past projects. My experience is that it's best to not worry to much about it, and gradually improve your code/structure in small steps. Experiment and see how some ideas/changes feel/look like. And it's of course always a good idea to learn from others. read a lot of code and analyse it, try to understand :).

If you wanna read about the theory I can recommend Craig Larmanns 'Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development' Amazon. He covers several parts of your question, gives some rough guidlines and shows them using an example application. I liked the book.

Could you upload your app somewhere? Perhaps on github or so, perhaps you could ask for some concrete advices.

like image 34
reto Avatar answered Oct 07 '22 15:10

reto