Tuesday, June 16, 2015

The open/closed principle


It defined as “Open for extension and closed for modification ”

Exception to “Closed for modification” rule :
  •  Client awareness :Any change in source code is allowed to existing code as long as it does     not also require a change to any client of that code.
  • Bug Fixes :Steps for fixing bugs (follows “Arrange, Act, Assert” and  “Red, green, refactor”  patterns)    
                            1. Write a failing unit test and/or integration test 
                   that specifically targets the bug
                        2. The source code is modified so that the unit test 
                   passes
  • At this juncture ,It is necessary to modify the source code which is exception to OCP rules

Extension Points : Classes that honor the OCP should be open to extension by containing defined extension points where future functionality can hook into the existing code and provide new behaviors and there are 3 extension points as below.
  • Virtual methods : Any class that marks one of its members as virtual is open to extension. This type of extension is via implementation inheritance.
  • Abstract methods : This is an example of the Template Method pattern, in which an algorithm is modeled but its general steps are customizable because of delegation to abstract methods. In effect, the base class delegates the individual steps of  the process to subclasses.
  • Interface inheritance : the client’s dependency on a class is replaced with interfaces

Note : Extension points should carefully be created for cases where requirements are unclear, changeable, or difficult to implement

Protected variation : Identify points of predicted variation and create a stable interface around them and suggests that you identify parts of the requirements that are likely to change or that are particularly troublesome to implement, and factor these out behind extension points
  • Predicted variation : The requirements of an individual class should be linked directly to a business client’s requirement
  • Stable Interfaces : all interfaces chosen to represent an extension point should be stable. The likelihood and frequency of interface changes should be low, otherwise you will need to amend all clients to use the new version.



No comments:

Post a Comment