What is the Mark IV CoffeeMaker? It's an OOD problem from Bob Martin's Agile Principles, Patterns and Practices in C#.
Well how hard could it be... actually quite a bit...a bout of designers' block. I thrashed about for a while on paper and drew up the following diagrams to start up with.. which fit the bill.
Fig: The brew coffee seq dia.
Fig: L - Keep Coffee Warm, R - Control Relief valve
Some more thinking, I landed up with the following object cubes (from ObjectThinking.. lets see if this helps me). To summarize,
- Side1: Classic CRC card
- Side2: Description + applicable stereotypes if any
- Side3: Contracts (N.A. this early in the design process.. usually private, public, protected, ISP derived interfaces etc.
- Side4: Knowledge required by the class to fulfill its responsibility (Either a [V]ariable, [A]rgument in service request, from [C]ollaborator or [M]anufacture on demand.)
- Side5: Protocol / Methods
- Side6: Events
Fig: Object cubes - first cut.
Note:CMA - CoffeeMakerAPI implementation from spec. Also it should be marked as (C) instead of (V) in Side4.. my mistake.
But wait.. flashes of clarity.
- IndicatorLight and Valve are see-thru delegators to CMA and don't do any real work. A tell-tale sign is that they do not have any methods that they claim as their own. Eliminate them.
- et tu Boiler? The only thing Boiler seems to have against being axed on the same lines.. is an if clause to prevent starting the boiler if there is no water. Off with its head.. I'll move that logic into CoffeeMaker
- What about CoffeeMaker?.. it has the Brew method. This is sort of a Controller class that Object Thinking warns us against... a puppet master for the device.. but we'll live with it for the sake of this series of posts ;)
- The x_Sensor classes are way too similar except that they watch 2 different status values. I can club them into a single class.. SensorObserver (a bad name in hindsight.. its more of an ObservableCoffeeMakerDevice)
Good so we're down to 2 classes from the original 7 (excluding CMA). That was time well spent.. or is it? The only way to validate if this design is good.. Code it up.
Turn the page...
No comments:
Post a Comment