Docsity

Object-Oriented Design: Classes, Responsibilities, and Relationships, Study notes of Computer Science

Partial preview of the text

Download Object-Oriented Design: Classes, Responsibilities, and Relationships and more Study notes Computer Science in PDF only on Docsity!

1

CS162: Introduction to

Computer Science II

Object Oriented Design

2

Object-Oriented Design

Your client gives you the specifications for the software. How do you design software once you have the specs?

  1. Discover classes
  2. Determine the responsibilities of each class
  3. Describe the relationships between the classes

3

Step 1: Discovering Classes

A simple rule: Look for nouns in the task description

  • Classes should represent a single concept
  • Look for entities with the same behavior – these should be objects of the same class
  • Balance between simplicity and complexity (eg. could make every noun an object but may be too complex)
  • Make use of inheritance

4

Step 2: Determine

Responsibilities of Each Class

  • Define behavior for each class
  • Look for verbs in the task description
  • Determine which class is responsible for this action

7

Achieving Steps 1 and 2 with

CRC cards

  • Component, Responsibility, Collaborator cards
  • Often done on 3x5 index cards

Class Responsibilities

Collaborators

Other components with which the component must interact

8

CRC Cards

SavingsAccount Allows user to deposit money into savings account Allows user to withdraw money from the savings account

Collaborators ATM CheckingAccount

9

CRC Cards

  • Why index cards?
    • Encourages experimentation (index cards are cheap)
    • Physical separation of components encourages logical separation
    • Encourages simplicity: if design can’t fit on a card, it’s too complex

10

CRC Cards

  • A component with one behavior and internal state should be made into a function
  • Components with many tasks and internal state should be made into classes - Responsibilities along with their arguments converted to methods - Internal state converted to member variables

13

Step 3: Determine Relationships

Between Classes

  • Dependency
    • A class depends on another if one of its methods uses an object of the other class
    • Aggregation is a stronger form of dependency
  • Interface Implementation

14

Step 3: Determine Relationships

Between Classes

  • Start with is-a relationships
  • Then consider has-a and dependency relationships

15

Coupling

  • CashRegister class depends on coin class (think of this as CashRegister uses the coin class)
  • In a class diagram, we draw a dependency in the form of a dashed arrow

Cash Register Coin

16

Coupling

  • Note that Coin does not depend on CashRegister
  • If you get a cycle in a class diagram, it is a sign of bad design

Cash Register Coin

19

Why is high coupling bad?

Inhibits ease of development, modification, and reuse

  • eg., if lots of classes depend on class X and you change X, you need to change all the dependent classes
  • eg. If you want to use X, you need to include all the classes that depend on X

X

Make sure you remove unnecessary couplings!

20

Special types of classes

  • Utility class: A class that is a collection of only static methods and constants eg. the Math class
  • A class with only a main method (only purpose is to start a program)