OCP : Open Closed Principle

The O in the SOLID principles, Uncle Bob explains this simply

A class and it’s component parts should be Open for extension and Closed for modification.

To follow this principle let’s take the clock object again. We know from SRP what parts are responsible for what and when we mix these we can create new kinds of clocks. Certain things may be internal to a clock part such as the clock face.

The clock face may be composed of several different things such as the display, hands and numbers etc. If the face was to change from say roman dail to micky mouse we don’t want to have one clock face object that we have to keep changing\rewriting for every different kind of face (you risk code duplication) but instead give it (extend it) the component parts it needs. A mickey mouse clock face would have mickey mouse hands, display and numbers but would display the hours\minutes via the rest of the clock object in the same way as the roman dail or any other type of clock face.

In software terms the way we achieve OCP is with either simple inheritance or dependency injection. More on these in later posts.

Advertisements

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