SOLID: principles with an acronym

SOLID is a set of five principles for object oriented program design. The principles are carefully selected to result in a cool acronym.

Single Responsibility Principle

Every class should have a single responsibility. Many have realized this. One put a name on it.

Open Closed Principle

Classes should be open for extension but closed for modification. Originally this meant that new functionality should be put in new classes that inherit from older classes. However, since this is likely to cause a mess the principle was redefined. It now refers to some sort of design pattern involving abstract classes.

Liskov Substitution Principle

If you create a derived class from a base class the derived class should be able to stand in for the base class in all existing code. This means that if you create a car that teleports to the moon when you turn left you probably shouldn’t go around calling it a car. This principle is always explained by giving examples of where it isn’t applied.

Interface Segregation Principle

Clients shouldn’t have to depend on interfaces they don’t use. This is almost the same as the single responsibility principle but it refers to interfaces instead of classes. If you are creative it is possible to violate one but not the other.

Dependency Inversion Principle

High-level modules should not depend on low-level modules, both should depend on abstractions. The ubiquitous example of this is Dependency Injection where you pass a dependency to a dependent module instead of letting the module create it itself. When taken to its extreme you get an IoC Container.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s