Refactoring Dinner: Interfaces instead of Inheritance

Last time, in Cooking Up a Good Template Method, I had a template method cooking our dinner. An abstract base class defined the template—the high level steps for preparing a one-skillet dinner—and a derived class provided the implementation for those steps. I'm currently reading Ken Pugh's Interface Oriented Design (more on that after I finish the book), and it got me thinking of a way to change the design to use interfaces instead of inheritance.

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).

4publicinterfaceISkilletCookable

5 {

6void HeatFat();

7void SauteSavoryRoot();

8void SauteProtein();

9void SauteVegetables();

10void 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.

5publicclassSkilletDinner

6 {

7privatereadonlyISkilletCookable cookable;

8

9public SkilletDinner(ISkilletCookable cookable)

10 {

11this.cookable = cookable;

12 }

13

14publicvoid 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.

4publicclassSkilletDinnerSpecs

5 {

6 [TestFixture]

7publicclassWhen_told_to_cook

8 {

9conststring heatFatMethod = "HeatFat";

10conststring sauteSavoryRootMethod = "SauteSavoryRoot";

11conststring sauteProteinMethod = "SauteProtein";

12conststring sauteVegetablesMethod = "SauteVegetables";

13conststring addFinishingTouchesMethod = "AddFinishingTouches";

14

15 [Test]

16publicvoid Should_follow_dinner_preparation_steps_in_order()

17 {

18var systemUnderTest = newMockSkilletDinner();

19

20var expectedMethodCalls = newList<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

29Assert.AreEqual(expectedMethodCalls.Count, systemUnderTest.CalledMethods.Count, "Expected number of called methods did not equal actual.");

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.

7publicclassSkilletDinnerSpecs

8 {

9 [TestFixture]

10publicclassWhen_told_to_cook

11 {

12 [Test]

13publicvoid Should_follow_dinner_preparation_steps_in_order()

14 {

15var mocks = newMockRepository();

16var cookable = mocks.StrictMock<ISkilletCookable>();

17var systemUnderTest = newSkilletDinner(cookable);

18

19using (mocks.Record())

20 {

21using (mocks.Ordered())

22 {

23 cookable.HeatFat();

24 cookable.SauteSavoryRoot();

25 cookable.SauteProtein();

26 cookable.SauteVegetables();

27 cookable.AddSauceAndGarnish();

28 }

29 }

30using (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.