CPSC 210

D1: Cohesion & Coupling

Programming vs. Engineering

  • So far, we have learned about object-oriented programming
  • Now, we will learn about object-oriented software engineering

Well-designed software modules

Learning Goals

  • Describe the principle of cohesion
  • Describe the principle of coupling
  • Analyze a system and improve on coupling and cohesion

Cohesion

  • Every class should have a
    single responsibility
  • Assessing cohesion:
    • Graph showing methods
      using fields 
    • Graph has clusters? Low cohesion! Should be split in multiple classes!

Coupling

Tight Coupling

Loose Coupling

  • Measure of degree to which one part of the system depends on other parts
    • Coupling is too tight when you make a change in one class and many other classes break!
    • e.g. you change a method in a class and many other classes will stop compiling

Coupling (2)

  • Moderate Coupling (detected at compile time)
    • Change in class causes error in other class
  • Semantic Coupling (detected at runtime)

    • Class depends on impl. in another class

    • Change in class causes bug in other class

  • Remember Single Point of Control in CPSC 110

    • Changes should be needed only in one place

Let's ask StackOverflow

https://stackoverflow.com/questions/3085285/difference-between-cohesion-and-coupling
https://stackoverflow.com/questions/14000762/what-does-low-in-coupling-and-high-in-cohesion-mean

Let's ask StackOverflow (2)

Lecture Ticket (1)

  • Account  
  • PurchaseManager
  • Purchase
  • BonusManager

Lecture Ticket (2)

  • We need a helper method like generateDateString()
  • A new class for dates should be introduced
  • Semantic coupling between Client and Account

Lecture Lab

  • AceCell: cellular network company
  • Each phone assigned to one customer
  • Account can have multiple phones
  • Each account needs one payee for bills
  • Each phone has a call log
  • Monthly billing based on call logs of all phones

D1: Cohesion & Coupling

The End - Thank You!

Made with Slides.com