CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 6 of 6 · 2 min read

Exercises

Easy

Easy 1: Field map (20 minutes)

Take a class of your own with at least four methods. Draw a grid of methods against fields and mark which methods use which fields.

Done when: the grid is complete and you have judged the cohesion as high or low.

Hint

A field that no method uses is a smell on its own.

Easy 2: Count the knowledge (20 minutes)

For booking.customer().address().city(), list the classes the caller must know about.

Done when: you have the list (three: Booking, Customer, Address) and the count after the fix (one).

Hint

The fix adds one forwarding method on each of two classes.

Easy 3: Label the cohesion (20 minutes)

Label each of these with a kind from section 3: StringUtils, a PriceCalculator, a class with start-up tasks, a Misc class.

Done when: you have four labels and one sentence of reason for each.

Hint

Table in section 3 has the definitions.

Medium

Medium 1: Split a class (45 minutes)

Build the BookingManager of section 2 with three groups of fields and methods. Split it into three classes.

Done when: each new class has all its fields used by most of its methods.

Hint

Start with fields, then move methods that use them.

Medium 2: Fix a train wreck (45 minutes)

Write a four-link chain such as order.customer().account().wallet().balance(). Add methods so that the caller makes one call.

Done when: the caller line has one dot, and each class knows only its own neighbour.

Hint

Each class gets a method that forwards to the next.

Medium 3: Where Demeter does not apply (45 minutes)

Write a record Point(int x, int y) and a class Line that holds two points. Read line.start().x() and decide whether this breaks the law, with reasons.

Done when: your note separates data structures from objects and gives a verdict.

Hint

A record exists to expose values.

Hard

Hard 1: Rate a design (90 minutes)

Take the meeting room design from Level 0. List which classes call which other classes. Count the dependencies and mark any chain of three or more calls.

Done when: a table lists every dependency and you propose at least two changes.

Hint

Draw one arrow for each class that mentions another.

Hard 2: Too far the other way (90 minutes)

Apply forwarding methods to every chain in a small program until the first class has twenty forwarders. Argue whether this is better.

Done when: you describe what was gained and lost and pick a middle design.

Hint

Count methods that only forward, with no logic.

Hard 3: Link to the rules (90 minutes)

For each of SRP, OCP, LSP, ISP and DIP, write one paragraph on how it lowers coupling or raises cohesion, with an example from modules 1 to 5.

Done when: five paragraphs each name one measure and one example.

Hint

SRP raises cohesion, DIP lowers coupling to details.