Back to Programming in C# language.

Recall a farming simulator game called Stardew Valley from the lecture. You started with the implementation of an Apple and an Apple tree as class Apple and class Tree using an class AppleFactory. The idea of decomposition Tree and AppleFactory was to separate two possibly difficult algorithms (although now they are quite simple):

This is captured by this code. Following on that, during the lecture, you created a special GoldenApple by deriving from Apple and overriding now virtual method Eat to provide additional feature. In the same manner, the creation of an Apple (responsibility of AppleFactory) was extended to sometimes produce GoldenApples. That is done by these modifications. Note that we didn’t have to modify Tree at all as the logic of a tree being a tree hasn’t changed. This is where the example ended during the lecture.

Now, for yourself, think how would you implement Peach as a fruit, growing on some Tree. Start by thinking how to represent Peach as a fruit in the code. Answer


Note that in our (game) world a definition of Fruit is different than botanical definition of Fruit. In our case, Fruits are all things growing on trees and being harvestable. Whichever approach you prefer more, try to answer the following questions:

Hint, Answer

On the side note, lookup the semantic difference between eatable and edible. In the same manner, try to think for a good word that would describe something that can be given to animals for eating (e.g. acorns). Answer


Now we have an implementation of Peach. How can we make peaches be able to grow on trees? Hint, Answer 1/2 Answer 2/2


We just extended the Factory design pattern into an Abstract Factory design pattern. Factory allowed us to separate two (possibly hard to implement) features. Abstract factory allows one of the parts to have various implementations.

Here I would ask a question if you know what is Frňákovník (my best translation attempt would be Proboscis tree). This is a term from a czech fairy tale. Please watch this video to understand what the Proboscis tree is (make sure to select best available quality and english subtitles).

So what is Proboscis tree? What fruit does it yield? How does it look like? What is it like? Answer 1/2, Answer 2/2


This looks all good, but what if a tree that always grows NoseGrowingApples is very rare and more often in our game we would find a tree that mostly grows regular Apples and only occasionally a NoseGrowingApple (e.g. similarly to GoldenApples being grown occasionally on regular apple tree). Can we somehow reuse the idea of AppleFactory in our OccasionalNoseGrowingAppleFactory? Recall that copy-pasting a code is almost never a good idea while programming. This is very difficult to invent, so don’t worry if you can’t get it right. Hint, Answer 1/3, Answer 2/3, Answer 3/3


This approach is usually referred to as Strategy design pattern.

Now would be a good time to write unit tests for every class and method we have. But is there even any code that can be tested? By good decomposition, we managed to develop these ideas in very small and isolated components that barely have any code to be tested (except the PeriodicSpecialFruitDispensingStrategy).

The link to runnable projects in all variants is present on the previous page.