Skip to main content

SOLID - Single Responsibility - Refactoring Using Abstraction


When we talk about abstracting our source code, we usually referring to introduction to an interface somewhere in our code. Lets make use of the previous source code (from Refactoring Using Delegation) and add an interface to it .

1. ConvertToList ( Convert Stream data type to List of Strings )
We first started by create a new interface named IConvertToList with a method definition of Convert
Then we continue by adding a new class that inherited from IConvertToList
Notice that we are making use of the Partial in the class definition. This is very important to take note here, Partial class type allow you to create multiple class with the same name in a single namespace. Why would we do that?. Remember the objective of SOLID, we want a single class to be modified or changed for only a specific reason ( In this case we place it in a separate file ). Later in this article we will face a situation where the requirement require us to change the Convert method to be able to convert with a different data type.

Next lets view how the caller may make use of this method.
The caller will create a new instance of the ConvertToList Class and just call the method implementation.
Now, lets go to the real world implementation.
Just imagine all of sudden there is a requirement changes, the requirement stated that there will be another source of input for the Convert method. To handle this changes, we may just start with adding a new method signature at the IConvertToList like below. Next, we may create a new .cs file, and within that file, we may place a class with the new method implementation but with the same name as ConvertToList class.
And Now At the static Helper class we may add the implementation. Now Lets take a look on how easy things be done if we implement it in our source using the same problem domain as previously in 'Refactoring Using Delegation' As we may notice from the code above, there is 2 data type that has been introduced, for what ever the type is the helper method now will accept both data type, In this article we will ignore the 'ConvertToObject' and 'InjectToDatabase' method, But is enough to show how abstracting our source code will help us in a long run, where there is many changes may occur throughout our software development life cycle.

Published on : 10-Jan-2019
Ref no : DTC-WPUB-000098

About Author

My photo
Wan Mohd Adzha CAPM,MCPD,MCSD,MCSE
Passionate about new technology ( Software Engineering ) and how to build,manage and maintain them

Comments