A daily dose of software engineering wisdom - inspiring, thought provoking, and sometimes just plain dangerous.

Tags: design principles

*Open-Closed Principle*

We should write our modules so that they can be extended without requiring them to be modified. In other words we want to be able to change what the modules do without changing the source code of the modules.

This is achieved through *abstraction*.

Make objects interact with interfaces instead of concrete classes so that if we want different behaviour we can provide a different implementation of the interface, without changing any of the code that uses the interface.

If we need to extend / modify a concrete class that we own we extract an interface, make the clients use that, and then provide whatever behaviour we want.

If we need to extend / modify a concrete class that we do not own (say from an external library) we can sub-class it.

The Open Closed principle means that changes don't propagate into existing code that already works: If we don't have to change working code we aren't likely to break it.

Last published: Thursday 15th December 2011

<<Previous     Next>>

carriagereturn.org home