I think there's value in this refactoring because it allows future flexibility and testability. Let's stroll through it, and I welcome your thoughts about how (and whether) this improves the code.
Previously, we had a base class SkilletDinner, which was extended by variants on that theme, such as chicken with onions and bell peppers or the FancyBaconPankoDinner. (If I've learned one thing from my readership, it is that blog posts should mention bacon. Mm, crispy bacon.) As the first step in the refactoring, I'll create an interface, ISkilletCookable that provides the same methods that were previously abstract methods in SkilletDinner. By naming convention, the interface is prefixed with 'I' and is an adjective describing how it can be used (-able).
4 public interface ISkilletCookable
5 {
6 void HeatFat();
7 void SauteSavoryRoot();
8 void SauteProtein();
9 void SauteVegetables();
10 void AddSauceAndGarnish();
11 }
Next, I'll create a SkilletDinner constructor that accepts an ISkilletCookable, and change the SkilletDinner's Cook() method to ask that cookable to do the work. SkilletDinner no longer needs to be abstract.
5 public class SkilletDinner
6 {
7 private readonly ISkilletCookable cookable;
8
9 public SkilletDinner(ISkilletCookable cookable)
10 {
11 this.cookable = cookable;
12 }
13
14 public void Cook()
15 {
16 cookable.HeatFat();
17 cookable.SauteSavoryRoot();
18 cookable.SauteProtein();
19 cookable.SauteVegetables();
20 cookable.AddSauceAndGarnish();
21 }
22 }
Then, FancyBaconPankoDinner implements ISkilletCookable and provides implementations for each of the methods that will be called by the Cook() method.
The first benefit from this refactoring is flexibility. While FancyBaconPankoDinner could not have inherited multiple classes (no multiple inheritance in C#), it can implement multiple interfaces. For example, it could also implement the IShoppable interface, thereby providing a ListIngredients() method that would let me include it in my grocery list.
This refactoring also makes it easier for me to test the quality and completeness of my template method. I can verify—does it cover all of the requisite steps for cooking a skillet dinner?—by creating behavior-verifying tests that assess the SkilletDinner's interactions with the ISkilletCookable interface. When I'm writing unit tests for the SkilletDinner class, I want to test its behavior because the behavior is what's important.
To forestall objections, I tried writing a test around the old version, creating my own mock class that extends the old abstract SkilletDinner. It got pretty lengthy.
4 public class SkilletDinnerSpecs
5 {
6 [TestFixture]
7 public class When_told_to_cook
8 {
9 const string heatFatMethod = "HeatFat";
10 const string sauteSavoryRootMethod = "SauteSavoryRoot";
11 const string sauteProteinMethod = "SauteProtein";
12 const string sauteVegetablesMethod = "SauteVegetables";
13 const string addFinishingTouchesMethod = "AddFinishingTouches";
14
15 [Test]
16 public void Should_follow_dinner_preparation_steps_in_order()
17 {
18 var systemUnderTest = new MockSkilletDinner();
19
20 var expectedMethodCalls = new List<string>();
21 expectedMethodCalls.Add(heatFatMethod);
22 expectedMethodCalls.Add(sauteSavoryRootMethod);
23 expectedMethodCalls.Add(sauteProteinMethod);
24 expectedMethodCalls.Add(sauteVegetablesMethod);
25 expectedMethodCalls.Add(addFinishingTouchesMethod);
26
27 systemUnderTest.Cook();
28
29 Assert.AreEqual(expectedMethodCalls.Count, systemUnderTest.CalledMethods.Count, "Expected number of called methods did not equal actual.");
30
31 for (int i = 0; i < expectedMethodCalls.Count; i++)
32 {
33 Assert.AreEqual(expectedMethodCalls[i], systemUnderTest.CalledMethods[i]);
34 }
35 }
36
37 private class MockSkilletDinner : SkilletDinner
38 {
39 public readonly List<string> CalledMethods = new List<string>();
40
41 protected override void HeatFat()
42 {
43 CalledMethods.Add(heatFatMethod);
44 }
45
46 protected override void SauteSavoryRoot()
47 {
48 CalledMethods.Add(sauteSavoryRootMethod);
49 }
50
51 protected override void SauteProtein()
52 {
53 CalledMethods.Add(sauteProteinMethod);
54 }
55
56 protected override void SauteVegetables()
57 {
58 CalledMethods.Add(sauteVegetablesMethod);
59 }
60
61 protected override void AddFinishingTouches()
62 {
63 CalledMethods.Add(addFinishingTouchesMethod);
64 }
65 }
66 }
67 }
In the new design, I can mock the ISkilletCookable interface with a mocking framework like Rhino.Mocks. The interface is easy to mock because interfaces, being the epitome of abstractions, readily lend themselves to being replaced by faked implementations. Rhino.Mocks takes care of recording and verifying which methods were called.
7 public class SkilletDinnerSpecs
8 {
9 [TestFixture]
10 public class When_told_to_cook
11 {
12 [Test]
13 public void Should_follow_dinner_preparation_steps_in_order()
14 {
15 var mocks = new MockRepository();
16 var cookable = mocks.StrictMock<ISkilletCookable>();
17 var systemUnderTest = new SkilletDinner(cookable);
18
19 using (mocks.Record())
20 {
21 using (mocks.Ordered())
22 {
23 cookable.HeatFat();
24 cookable.SauteSavoryRoot();
25 cookable.SauteProtein();
26 cookable.SauteVegetables();
27 cookable.AddSauceAndGarnish();
28 }
29 }
30 using (mocks.Playback())
31 {
32 systemUnderTest.Cook();
33 }
34 }
35 }
36 }
The test relies on Rhino.Mocks to create a mock implementation of ISkilletCookable, and then verifies that the system under test, the SkilletDinner, interacts correctly with ISkilletCookable by telling it what steps to do in what order.
That test is quite cognizant of the inner workings of the SkilletDinner.Cook() method, but that's specifically what I'm unit testing: Does the template method do the right steps? I don't mind how the steps are done, but you have to start the onions before you add the meat, or else the onions won't caramelize and flavor the oil.
By the way, if you had previously found the learning curve for Rhino.Mocks' record/playback model too steep a hill to climb (or to convince your teammates to climb), check out Rhino.Mocks 3.5's arrange-act-assert style. It creates more readable tests, putting statements in a more intuitive order. I really like it. I could not, however, use it here because I have not found a way to enforce ordering of the expectations (i.e., to assert that method A was called before B, and to fail if B was called before A) in A-A-A-style. So we have a record/playback test, instead.
Here's a summary of the refactoring. I extracted an interface, ISkilletCookable, and composed SkilletDinner with an instance of that interface, liberating us from class inheritance. Because SkilletDinner is now given the worker it depends on (via dependency injection), I can give it a fake worker in my tests, so that my unit tests don't need to perform the time- and resource-consuming operation of firing up the stove. And I managed to write another blog post that mentions bacon. Mm, bacon.