Part 7 of 7 · 3 min read
Exercises
Easy
Easy 1: Name the actors (20 minutes)
A Report class has calculateTotals() for the accounts team, formatAsPdf() for the design team and emailTo(String) for the operations team. Name the actor of each method.
Done when: you have three methods matched to three actors and a one-line verdict on whether SRP holds.
Hint
Ask who would complain if the method's output changed.
Easy 2: Run the example (20 minutes)
Type the Employee class from section 2 and run it for 45 hours at a rate of 20. Then change 40 to 38 inside regularHours() and run again.
Done when: you have printed pay and report text before and after the change, and can point to the wrong line.
Hint
Only one number changes in the file, and the report method is affected too.
Easy 3: Write the sentence (20 minutes)
Write a twenty-five word description, without "and" or "or", for each of three classes you wrote earlier (for example Booking, Room, BookingService).
Done when: each class has a clean sentence, or a note saying which class would not fit one.
Hint
A sentence with "and" often points to the cut line.
Medium
Medium 1: Split Employee (45 minutes)
Create Pay, HourReport and EmployeeRepository classes with an EmployeeFacade in front. Keep an EmployeeData class with only fields and getters.
Done when: the facade has three methods that each forward in one line, and the program prints 970 and "40 regular, 5 overtime".
Hint
The facade holds no if statements and no arithmetic.
Medium 2: Find the hidden actor (45 minutes)
Take the BookingService of Level 0, module 4. List every reason it might be edited (pricing, availability rules, emails, storage). Group those reasons by actor.
Done when: you have a table of reasons and actors and a decision about which groups need their own class.
Hint
Pricing and availability usually come from different stakeholders.
Medium 3: Good duplication (45 minutes)
Two methods in a class both compute price * 0.18: one for invoices and one for a tax report. Decide whether to share the code. Write down the actors involved and your reasoning.
Done when: you have a one-paragraph decision that names each actor and says what change would make the two lines differ.
Hint
Ask who owns the rate for each line, and whether they can differ.
Hard
Hard 1: Refactor a god class (90 minutes)
Write a LibraryManager class with methods for adding books, charging fines, printing member cards and saving to a file. Then refactor it so that each actor has one class.
Done when: before and after versions both compile, and a list shows which actor owns each class.
Hint
Start by listing actors, not methods.
Hard 2: Break it on purpose (90 minutes)
Build a shared helper used by two actors, then change it for one actor in a way that silently breaks the other. Write a test that would catch the break.
Done when: you have a failing test, a fix by splitting, and a passing test.
Hint
The test should call both actors' methods and compare with expected numbers.
Hard 3: Argue both sides (90 minutes)
Take a class of about 150 lines from any project you know. Argue in writing for splitting it, then argue against. Make a final call.
Done when: both arguments name actors and costs, and the final call is justified in three sentences.
Hint
Count merge conflicts, class size and the number of callers affected.