Design: OpenClosePrinciple

September 5, 2008

Classes should be open for extentions but close to modification.

Once you have a class that works, and is being used, you really don’t want to make changs to it unless you have to. But remember, CHANGE is the great constant in software development. With the OCP, we allow for change through extention, rather that having go back and modify your existing code.

It’s really a combination between encapsulation and abstraction. You are finding the behavior that stays the same away into a base class, abstracting into a base class (so you locks the code from modification), but when you need modification you can extend creating a subclass. That’s where encapsulation come in: you are encapsulating what varies(behavior in the subclass) away from what stays the same, the common behavior in the base class.

So,  abstract common behavior and encapsulate what change!!!

Advertisement

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.