Skip to main content

Cohesion, Coupling, and SOLID Lab

Retrieval Prompts

  1. State cohesion and coupling in one sentence each, from memory.
  2. Write the Single Responsibility Principle using "reasons to change," not "does one thing."
  3. State the Open-Closed Principle and name the minimum evidence that justifies applying it.
  4. Write the Liskov Substitution Principle as a substitutability claim, not as a rule about inheritance.
  5. Write the Dependency Inversion Principle and explain in one sentence why "high-level" and "low-level" matter.

Compare and Distinguish

Separate these pairs clearly:

  • cohesion vs coupling
  • "private field" vs "encapsulated decision"
  • "class does one thing" vs "class has one reason to change"
  • closed for modification vs immutable
  • DIP (direction of dependency) vs dependency injection (mechanism)

Common Mistake Check

For each statement, identify the design error:

  1. "I split the class into two files, so the cohesion is higher now."
  2. "This getter returns the internal ArrayList, but the field is private, so the class is encapsulated."
  3. "Square extends Rectangle compiles and the tests pass, so Liskov is satisfied."
  4. "I added an interface with one implementation so we can swap it later."
  5. "This method used to be 150 lines; I split it into 15 ten-line methods named step1 through step15."

Mini Application

For each scenario, answer all four:

  1. Which design property is failing (cohesion, coupling, encapsulation, SRP, OCP, LSP, ISP, DIP)?
  2. What symptom made it visible?
  3. What is the smallest refactor that would fix it?
  4. What evidence would justify not doing that refactor (YAGNI)?

Scenarios:

  1. An Order class has methods total(), saveToDatabase(conn), and renderInvoiceHtml().
  2. A Report class depends on MySqlConnectionImpl via direct new calls in several methods.
  3. A PaymentProcessor has a switch over type that is duplicated in Refunder, Reporter, and AuditLogger.
  4. A Printer interface has print, fax, scan, and staple; OldPrinter throws UnsupportedOperationException for three of them.
  5. A Bird base class has fly(); Penguin extends Bird and throws from fly().

Evidence Check

This page is complete only if you can, for each scenario, state the principle and describe the concrete symptom, not just name the letter.

Integrated OOP Mechanics Drill

Model a small meeting scheduler with users, calendars, meeting requests, rooms, and invitations.

  1. Define classes, constructors, fields, and methods for the smallest useful model.
  2. Mark which state is private and which behavior is public. Explain the invariant each private field protects.
  3. Add one inheritance relationship only if substitution is valid; otherwise prefer composition and explain why.
  4. Add one polymorphic operation, such as notification delivery by email, SMS, or in-app message.
  5. Label relationships as association, aggregation, composition, or dependency.

Evidence check: include a class diagram or text equivalent, two tests for object behavior, and one paragraph explaining an inheritance choice you rejected.

Additional Completion Note

This page is complete only if you can, for each scenario, state the principle and describe the concrete symptom -- not just name the letter.