Cohesion, Coupling, and SOLID Lab
Retrieval Prompts
- State cohesion and coupling in one sentence each, from memory.
- Write the Single Responsibility Principle using "reasons to change," not "does one thing."
- State the Open-Closed Principle and name the minimum evidence that justifies applying it.
- Write the Liskov Substitution Principle as a substitutability claim, not as a rule about inheritance.
- 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:
- "I split the class into two files, so the cohesion is higher now."
- "This getter returns the internal
ArrayList, but the field isprivate, so the class is encapsulated." - "
Square extends Rectanglecompiles and the tests pass, so Liskov is satisfied." - "I added an interface with one implementation so we can swap it later."
- "This method used to be 150 lines; I split it into 15 ten-line methods named
step1throughstep15."
Mini Application
For each scenario, answer all four:
- Which design property is failing (cohesion, coupling, encapsulation, SRP, OCP, LSP, ISP, DIP)?
- What symptom made it visible?
- What is the smallest refactor that would fix it?
- What evidence would justify not doing that refactor (YAGNI)?
Scenarios:
- An
Orderclass has methodstotal(),saveToDatabase(conn), andrenderInvoiceHtml(). - A
Reportclass depends onMySqlConnectionImplvia directnewcalls in several methods. - A
PaymentProcessorhas a switch overtypethat is duplicated inRefunder,Reporter, andAuditLogger. - A
Printerinterface hasprint,fax,scan, andstaple;OldPrinterthrowsUnsupportedOperationExceptionfor three of them. - A
Birdbase class hasfly();PenguinextendsBirdand throws fromfly().
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.
- Define classes, constructors, fields, and methods for the smallest useful model.
- Mark which state is private and which behavior is public. Explain the invariant each private field protects.
- Add one inheritance relationship only if substitution is valid; otherwise prefer composition and explain why.
- Add one polymorphic operation, such as notification delivery by email, SMS, or in-app message.
- 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.