DBTechCon – Spring 2011
DBTechCon is in full swing. Already I have a backlog of sessions I want to watch…there is so much great content. Be sure to check your schedule today as day two of DBTechCon continues..
Do you write SOLID code? – Day 2
SOLID is an acronym from Robert Martin defining five basic principles of object oriented programming. The principles apply to any object oriented language, and even to some that are hybrid or non-object oriented languages.
Here are the five principles of SOLID:
- Single Responsibility
- Open/Closed
- Liskov Substitution
- Interface Segregation
- Dependency Inversion
The goal for applying these principles to your software development practices is that you will write software that is stable, robust, and extensible over time. Today we continue this discussion on the second principle…the Open/Closed Principle.
The Open/Closed principle is also one that acts much like it sounds. Following this principle, an object that has been completed and released is closed for modification. However, it is open for extension.
What “Closed” means in practicality is that once released, an object may only be fixed if there are bugs, or it may be optimized for performance. However, it does not allow for new properties or public methods to be exposed. Using this technique all unit tests for public methods and properties should continue to pass. All consumers of a closed object should continue to work since the interface of the object never changes.
“Open” means that you may create a new object that extends the original object while not modifying it in any fashion. Using this technique both the old and new objects may co-exist allowing previous software to work as originally designed, and new features to be added without re-writing all the original code.
Extending an object can be performed using many different OO techniques such as inheritance. You could extend the functionality by creating a completely new object sharing nothing, including an interface. You could extend the original object through direct inheritance of that object, inheriting from the same interface, or inheriting from a common abstract object.
This principle has evolved to the point of being defined by many OO Patterns. There are many ways to implement an Extension pattern. A Static Extension pattern utilizes a shared interface or abstract class hard coding the extension. This is most often used for software that is completely self contained.
A second pattern is a Dynamic Extension Pattern. This pattern is more common for software allowing extension to occur unknown at compile time. A good example of this pattern is the ability of software IDEs, such as Eclipse or Visual Studio, to implement “Add In” software. The IDE application has an interface allowing for third party developers to dynamically extend the functionality of the base application after it is fully compiled without modifying the underlying code.
I highly recommend finding resources to make this principle a part of your code practice. A quick google search using “extension pattern” will return some good examples and further links.
Do you have experience with the Open/Closed principle? Share your insights with us. Drop me a note at btaylor@sswug.org.
Cheers,
Ben
$$SWYNK$$
Featured Article(s)
Getting Started with the Silverlight 5 Beta
A walk-through of getting setup with the Silverlight 5 Beta, which includes a compilation of the required tools to build a first Silverlight 5 Project and an overview of the IDE.
Featured White Paper(s)
Upgrading to Microsoft SQL Server 2008 R2 from Microsoft SQL Server 2008, SQL Server 2005, and SQL Server 2000
More than ever, organizations rely on data storage and analysis for business operations. Companies need the ability to deploy… (read more)
Featured Script
Script _Out_Data
VBScript that generates INSERT Statements based on existing table contents for the purpose of migrating data when BCP or DTS … (read more)