Index > Software Engineering > 2021-09-07: OOP 1

2021-09-07: OOP 1

What does stack overflow have to say about coupling and cohesion? https://stackoverflow.com/questions/14000762/what-does-low-in-coupling-and-high-in-cohesion-mean

There’s two design principles, SOLID and GRASP, that are commonly used.

SOLID:

Italics items are covered today. We’ll also hit Law of Demeter, and High cohesion and low coupling from GRASP.

Single Responsibility

An object should have one job. No “god” classes that do everything - no Swiss army knives.

Classes should be small, simple, focused, and numerous.

This helps a lot with team coordination and unit testing.

High Cohesion

The fields and methods of a class should be closely related. A class with too many responsibilities should be broken apart.

“This design goal should have a higher priority than the others” - prof

All these tiny classes will need to interact. You will increase coupling by pursuing this goal - that’s okay, to an extent.

“Everything should be as simple as possible, but not simpler” - Einstein

Some classes will be complex, and they should be - only break them if there are two jobs present.

Information Expert

“Assign responsibility to the class that has the information needed to fulfill the responsibility”

Low Coupling

This attempts to minimize the impact of changes in the system.

We want to minimize unnecessary coupling. There is a real balance here between number of classes and relationships between classes.

Law of Demeter

Limit the range of classes that a class talks to - essentially boils down to “Only talk to your friends”.

If you’re doing board.getPiece(x,y).getAttribute(), you might not need to know about piece - try board.getPieceAttributeAt(x,y).

I think this is kinda horrible, since board now needs “getters” for attributes of someone else.

Dependency Inversion

“High-level modules should rely only on abstractions (interfaces).”

“Abstractions should not rely on details - details should rely on abstractions.”

A “copy” object (a high-level module) should not depend on a keyboardReader or screenWriter, but a Reader abstraction and a Writer abstraction (interfaces) that these things implement.

Administrative

Do not come to class on Thursday! The lecture will be recorded and posted. Prof. will be available throughout the day to answer questions.

When the class site and myCourses disagree, use myCourses as the source of truth.


Index > Software Engineering > 2021-09-07: OOP 1