As a recap, we've talked about how numbers work, how characters work, how characters are combined into text via lexers and parsers, and how numbers and text are combined via operators in custom classes:
Numbers ------------------------> Operators -> ?
Characters --(Lexers/Parsers) --> Operators -> ?
Today's homework is to think about how to design classes, or what the industry calls "object-oriented design." You have to be careful to minimize complexity when designing public interfaces and APIs to your code, otherwise this happens:
Remember fizzbuzz? This is a satirical version built using every object-oriented pattern in the book. There is an actual book on the subject, which is widely respected throughout the industry, and in some cases misused to overcomplicate code:
https://en.wikipedia.org/wiki/Design_Patterns
The assignment for today is to think about some everyday thing you interact with, such as an elevator, a pet, or anything which has tangible and measurable inputs and outputs. There are three files to look at in the simulation directory in this repository:
- SimulationRunner: a program to run simulation classes.
- Simulation: the interface definition for a simulation.
- HotdogSimulation: a class simulating a hot dog.
Design two versions of this object with the following shared criteria:
- Define the start, stop, tick, get_uses, and use methods.
- Ensure that the object expires when a reasonable number of ticks have passed.
One version of the object should be very simple, such as the hot dog example.
The other version of the object should be overly complicated, with verbs such as weigh to get its mass, mood to get its mood, price_tag to have it return its estimated value, and go_to_moon to send the object to the moon (in the case of the pet, it would probably explode and use() should return False). Try to define at least 20 different verbs you could use on the object, with associated instance variables to track its state so the verbs interact correctly. For example, a throw verb on a hot dog should reduce its mass, because some of its condiments will end up on the target.
Although it would be preferred to create a Python file that can be run under the simulator, a text specification is also acceptable. The important thing is for the object to have a consistent model of its state which accurately represents what would happen in real life when methods (verbs) are applied to it.