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 gunnarpeipman.com

Click here to go to article

1 Comment

  • 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";
    }
    }

Comments have been disabled for this content.