I am a huge fan of software design principles such as SOLID and DRY. What other principles exist for OO software design?
Note. I’m
Chose composition over inheritance, is one.
Many people, especially those new to OO will start extending classes when all they really need to is to use composition. Really if you should ask your self, is the new class B a Class A? If not then you shouldn't extend.
For example, let's say I have a Person
Class, a Car
Class, and I would like to make a new class called a DrivenCar
class. A naive implementation would be to say (let's pretend we got multiple inheritance)
class DrivenCar extends Person, Car { ... }
Is the DrivenCar a type of Person? No so it shouldn't be extending Person. Is the DrivenCar a Car? yes so it makes sense to extend
Using composition the implmentation would look like
class DrivenCar extends Car {
private Person driver;
}
The GRASP patterns. Yes, they seem rather trivial. More like distillation down to core qualities that other, more involved patterns demonstrate.
Interface. Most design patterns are based on separation of interface & implementation.
KISS
YAGNI
When your API are expected to grow, use Abstract class instead of Interface. Adding a new method in Interface requires to change ALL the classes which implement it.