Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.






Refactoring: extract and override factory method

I’m sure you have seen classes that initialize a lot of objects in their constructor. These classes may be hard to test because of those object creations I mentioned. To get around this problem we use Extract and override factory method refactoring so we can extend these classes and override some factory methods.


NB! This blog is moved to

Click here to go to article


Aleksandar said:

It doesn't work like that. You have to make GetTransformer virtual in order to be able to call it. That works in java, where all the functions are virtual, but not in c#. So, it should look like this:

class BaseClass


       private string a;

       public BaseClass()


          a =  CreateMethod();


       protected virtual string  CreateMethod()


           return "aaa";



   class DerivedClass : BaseClass


       protected override string CreateMethod()


           return "bbb";



# December 9, 2009 5:24 PM