Assigning Responsibilities

 

Responsibility

     Contract or obligation

For system operation

     Class obligations

Obligations for objects in terms of their behaviour

     Assignment of responsibility is extremely important in OO Design

Class Responsibilities

     Two types of responsibilities

Doing

Knowing

     Doing

Something itself

Initiating actions in other objects

Controlling and coordinating activities in other objects

     Knowing about

Encapsulated data

Related objects

Things that can be derived or calculated

GRASP Patterns

      Fundamental principles of assigning responsibilities to objects

      General Responsibility Assignment Software Patterns

      GRASP defines nine basic OO design principles or basic building blocks in design

      Let us look at the following subset:

Information Expert

Creator

Low Coupling

High Cohesion

Controller

Information Expert (or Expert)

      Problem

What is the most basic principle by which responsibilities are assigned to objects in OO Design?

      Solution

Assign a responsibility to the information expert

   I.e. the class that has the information necessary to fulfil the responsibility

Expert Example

      In POS, some class needs to know the grand total of the sale

      Who should be responsible for

Knowing the grand total of the sale?

Calculating the grand total of the sale?

Creator

      Problem

Who should be responsible for creating a new instance of some class A?

      Solution

Assign class B the responsibility to create an instance of class A if one of the following is true:

   B aggregates / contains / records instances of A

   B closely uses objects of A

   B has the initialising data that will be passed to A when it is created

    (B is an expert with respect to creating instances of A)
 

Coupling

      Coupling is a measure of how strongly one class is

connected to

has knowledge of

relies upon

other elements

      A class with low (weak) coupling is not dependent on too many other elements

      A class with high (strong) coupling relies upon many other classes

Low Coupling

      Problem

How to support low dependency low change impact, and increased reuse?

      Solution

Assign a responsibility so that coupling remains low

Low Coupling Example (Cont.)

      Since a Register records a Payment, the Creator pattern suggest Register as a candidate for creating the Payment

      The Register instance could send an addPayment message to Sale with the newPayment as a parameter

Cohesion

      Functional cohesion is a measure of how strongly related and focused the responsibilities of an element are

      An element with low cohesion does many unrelated things or does too much work

Hard to comprehend

Hard to reuse

Hard to maintain

High cohesion is recommended

High Cohesion

      Problem

How to keep objects focused, understandable, and manageable?

      Solution

Assign responsibility so that cohesion remains high

Modular Design

      Coupling and cohesion are old principles in software design

      Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules (G. Booch)

      Modular design is promoted by creating classes with high cohesion

Controller

      Problem

Who should be responsible for handling system events?

System Event

   Event generated by an external actor

   Event associated with system operation

      Solution

Assign the responsibility to a class which represent

   the overall system (Facade Controller)

   something in the real world that is active and might be involved in the task (Role Controller)

   artificial handler of all system events for a use case (Use Case Controller)

Controller Rules

      The same controller should be used for all system events of one use case

      Controller should delegate the work that needs to be done while coordinating the activity

      Use Case controller

Name: UseCaseNameHandler

Artificial Construct

Used to improve coupling and cohesion

Controller Example

      In POS, who should be responsible for handling the enterItem event?

      By the Controller pattern, we have the following choices:

A representative of

   overall business Store

   Something in the real world that is active Cashier

Overall system POS

Use case handler BuyItemHandler