Chapter 17

Object-Oriented Design


Chapter Goals

The Software Life Cycle

Analysis

Design

Implementation

Testing

Deployment

The Waterfall Model

The Spiral Model

Activity Levels in the Rational Unified Process

Extreme Programming

Extreme Programming

Extreme Programming

Extreme Programming

Extreme Programming

Self Check

  1. Suppose you sign a contract, promising that you will, for an agreed-upon price, design, implement, and test a software package exactly as it has been specified in a requirements document. What is the primary risk you and your customer are facing with this business arrangement?
  2. Does Extreme Programming follow a waterfall or a spiral model?
  3. What is the purpose of the "on-site customer" in Extreme Programming?

Answers

  1. It is unlikely that the customer did a perfect job with the requirements document. If you don't accommodate changes, your customer may not like the outcome. If you charge for the changes, your customer may not like the cost.
  2. An "extreme" spiral model, with lots of iterations.
  3. To give frequent feedback as to whether the current iteration of the product fits customer needs.

Object-Oriented Design

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

Discovering Classes

Example: Invoice


Example: Invoice

Finding Classes

CRC Card

Self Check

  1. Suppose the invoice is to be saved to a file. Name a likely collaborator.
  2. Looking at the invoice in Figure 4, what is a likely responsibility of the Customer class?
  3. What do you do if a CRC card has ten responsibilities?

Answers

  1. FileWriter
  2. To produce the shipping address of the customer.
  3. Reword the responsibilities so that they are at a higher level, or come up with more classes to handle the responsibilities.

Relationships Between Classes

Inheritance

Aggregation

Example

class Car extends Vehicle
{
. . .
private Tire[] tires;
}

Dependency

UML Relationship Symbols

Relationship Symbol Line Style Arrow Tip
Inheritance Solid Triangle
Interface Implementation Dotted Triangle
Aggregation Solid Diamond
Dependency Dotted Open

Self Check

  1. Consider the Bank and BankAccount classes of Chapter 7. How are they related?
  2. Consider the BankAccount and SavingsAccount objects of Chapter 12. How are they related?
  3. Consider the BankAccountTester class of Chapter 3. Which classes does it depend on?

Answers

  1. Through aggregation. The bank manages bank account objects.
  2. Through inheritance.
  3. The BankAccount, System, and PrintStream classes.

Advanced Topic: Attributes and Methods in UML Diagrams


Advanced Topic: Multiplicities

Aggregation and Association

Five-Part Development Process

  1. Gather requirements
  2. Use CRC cards to find classes, responsibilities, and collaborators
  3. Use UML diagrams to record class relationships
  4. Use javadoc to document method behavior
  5. Implement your program

Printing an Invoice – Requirements

Sample Invoice

I N V O I C E
Sam's Small Appliances        
100 Main Street        
Anytown, CA 98765        
         
Description   Price
Qty
Total
Toaster   29.95
3
89.85
Hair dryer   24.95
1
24.95
Car vacuum   19.99
2
39.98
         
Amount Due:   $154.78        

Printing an Invoice – CRC Cards

Printing an Invoice – CRC Cards

CRC Cards for Printing Invoice

Invoice and Address must be able to format themselves:
    

CRC Cards for Printing Invoice

Add collaborators to invoice card:

CRC Cards for Printing Invoice

Product and LineItem CRC cards:
    

CRC Cards for Printing Invoice

Invoice must be populated with products and quantities:

Printing an Invoice – UML Diagrams

Printing an Invoice – Method Documentation

Method Documentation – Invoice class

/**
Describes an invoice for a set of purchased products.
*/
public class Invoice
{
/**
Adds a charge for a product to this invoice.
@param aProduct the product that the customer ordered
@param quantity the quantity of the product
*/
public void add(Product aProduct, int quantity)
{
}

/**
Formats the invoice.
@return the formatted invoice
*/
public String format()
{
}
}

Method Documentation – LineItem class

/**
Describes a quantity of an article to purchase and its price.
*/
public class LineItem
{
/**
Computes the total cost of this line item.
@return the total price
*/
public double getTotalPrice()
{
}

/**
Formats this item.
@return a formatted string of this line item
*/
public String format()
{
}
}

Method Documentation – Product class

/**
Describes a product with a description and a price.
*/
public class Product
{
/**
Gets the product description.
@return the description
*/
public String getDescription()
{
}

/**
Gets the product price.
@return the unit price
*/
public double getPrice()
{
}
}

Method Documentation – Address class

/**
Describes a mailing address.
*/
public class Address
{
/**
Formats the address.
@return the address as a string with three lines
*/
public String format()
{
}
}

The Class Documentation in the HTML Format


Printing an Invoice – Implementation

Implementation

Implementation

Implementation

File InvoiceTester.java

File Invoice.java

File LineItem.java

File Product.java

File Address.java

Self Check

  1. Which class is responsible for computing the amount due? What are its collaborators for this task?
  2. Why do the format methods return String objects instead of directly printing to System.out?

Answers

  1. The Invoice class is responsible for computing the amount due. It collaborates with the LineItem class.
  2. This design decision reduces coupling. It enables us to reuse the classes when we want to show the invoice in a dialog box or on a web page.

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – Requirements

An Automatic Teller Machine – CRC

CRC Cards for Automatic Teller Machine

ATM States

  1. START: Enter customer ID
  2. PIN: Enter PIN
  3. ACCOUNT: Select account
  4. TRANSACT: Select transaction

State Diagram for ATM Class

An Automatic Teller Machine – UML Diagrams

Method Documentation ATM Class

/**
An ATM that accesses a bank.
*/
public class ATM
{
/**
Constructs an ATM for a given bank.
@param aBank the bank to which this ATM connects
*/
public ATM(Bank aBank) { }

/**
Sets the current customer number and sets state to PIN.
(Precondition: state is START)
@param number the customer number
*/
public void setCustomerNumber(int number) { }

Method Documentation ATM Class (Continued)

   /**
Finds customer in bank. If found sets state to ACCOUNT, else to START.
(Precondition: state is PIN)
@param pin the PIN of the current customer
*/
public void selectCustomer(int pin) { }

/**
Sets current account to checking or savings. Sets state to TRANSACT.
(Precondition: state is ACCOUNT or TRANSACT)
@param account one of CHECKING or SAVINGS
*/
public void selectAccount(int account) { }

/**
Withdraws amount from current account.
(Precondition: state is TRANSACT)
@param value the amount to withdraw
*/
public void withdraw(double value) { }
. . .
}

An Automatic Teller Machine – Implementation

An Automatic Teller Machine – Implementation

An Automatic Teller Machine – Implementation

File ATM.java

File Bank.java

File Customer.java

File ATMViewer.java

File ATMFrame.java

File KeyPad.java

File ATMTester.java

Output

Enter account number: 1
Enter PIN: 1234
A=Checking, B=Savings, C=Quit: A
Balance=0.0
A=Deposit, B=Withdrawal, C=Cancel: A
Amount: 1000
A=Checking, B=Savings, C=Quit: C
. . .

Self Check

  1. Why does the Bank class in this example not store an array list of bank accounts?
  2. Suppose the requirements change–you need to save the current account balances to a file after every transaction and reload them when the program starts. What is the impact of this change on the design?

Answers

  1. The bank needs to store the list of customers so that customers can log in. We need to locate all bank accounts of a customer, and we chose to simply store them in the customer class. In this program, there is no further need to access bank accounts.
  2. The Bank class needs to have an additional responsibility: to load and save the accounts. The bank can carry out this responsibility because it has access to the customer objects and, through them, to the bank accounts.