Object-oriented design principles
A few design principles to get you going (which are in NO special order). In addition to the basic OO principles, here’s my pin up post on design principles to strive toward (I’ll add to this as we go along).
- Hide details of the implementation: Code to contracts and not implementations. Contracts are either abstract classes or pure interfaces.
- Keep coupling to a minimum : a class can depend on native types (basic ints,bools etc) but should only depend on a select few peer classes. If the dependencies are high, consider this a smell and refactor to reduce the dependency. This can include creating new specialized types that are composed of the dependencies, but again this must be managed correctly.
- Maintain maximum cohesion: a class should focus on a single concept.
- Favour composition over inheritance: there’s a few different ways to get objects to collaborate – inheritance, composition, subscription through event handling.
- Adhere to the Open closed principle: an object should be opened for extension (able to extend its behaviour )but closed for modification (while extending the behaviour you should not modify its source code)
- Follow LSP where possible. Liskov Substitution principle: objects that have references to base types should be able to use derived types without knowing the difference. The basic concept behind polymorphism. TIP: design objects around behaviour and not data.
- Use Inversion of control / Dependency Inversion principle :a formal definition: High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.
- Employ the Interface Segregation principle: Objects should not be forced to depend on interfaces that they don’t use.
- Consider using the Law of Demeter : Only talk to your immediate friends, so the following would be considered a violation …
- var payment= Client.Wallet.GetMoney(10.00);
- This will be an interesting one to enforce if you plan on sticking to Domain driven design principles which suggest that you should get to entities through their aggregates.
- Encapsulate the variations: when encountering variants encapsulate those bits that vary and make them first class citizens.
- Consider using Factories for object creation.
The above are principles and guidelines and violating them in okay, but only if you understand why doing so is better than following them.
Hope this helps!
Super post, Need to mark it on Digg
AlexAxe
Comment by AlexAxe — April 18, 2009 @ 1:03 pm
Hey!
Thanx Alex.
Comment by admin — April 18, 2009 @ 5:15 pm