Big Java 4

Chapter 12 – 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

Development process methodology by the inventors of UML

Extreme Programming

Extreme Programming

Extreme Programming

Extreme Programming

Extreme Programming

Self Check 12.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?

Self Check 12.2

Does Extreme Programming follow a waterfall or a spiral model?

Self Check 12.3

What is the purpose of the on-site customer in Extreme Programming?

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 12.4

Suppose the invoice is to be saved to a file. Name a likely collaborator.

Self Check 12.5

Looking at the invoice in Figure 4, what is a likely responsibility of the Customer class?

Self Check 12.6

What do you do if a CRC card has ten 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 12.7

Consider the Bank and BankAccount classes of Chapter 7. How are they related?

Self Check 12.8

Consider the BankAccount and SavingsAccount objects of Chapter 10. How are they related?

Self Check 12.9

Consider the BankAccountTester class of Chapter 3. Which classes does it depend on?

Attributes and Methods in UML Diagrams

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

Case Study: Printing an Invoice — Requirements

Case Study: 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

Case Study: Printing an Invoice — CRC Cards

Case Study: 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:

Case Study: 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

A line item needs to store a Product object and quantity:
public class LineItem
{
   . . .
   private int quantity; 
   private Product theProduct;
}

Implementation

ch12/invoice/InvoicePrinter.java

Your browser does not support the <object> tag.

ch12/invoice/Invoice.java

Your browser does not support the <object> tag.

ch12/invoice/LineItem.java

Your browser does not support the <object> tag.

ch12/invoice/Product.java

Your browser does not support the <object> tag.

ch12/invoice/Address.java

Your browser does not support the <object> tag.

Self Check 12.10

Which class is responsible for computing the amount due? What are its collaborators for this task?

Self Check 12.11

Why do the format methods return String objects instead of directly printing to System.out?

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — Requirements

Case Study: An Automatic Teller Machine — CRC Cards

Nouns are possible classes:
ATM
User
Keypad
Display
Display message
Button 
State
Bank account
Checking account
Savings account
Customer
Customer number
PIN
Bank

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

Case Study: 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 (Cont.)

   /**
      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) { }
   . . .
}

Case Study: An Automatic Teller Machine — Implementation

Case Study: An Automatic Teller Machine — Implementation

Case Study: An Automatic Teller Machine — Implementation

ch12/atm/ATM.java

Your browser does not support the <object> tag.

ch12/atm/Bank.java

Your browser does not support the <object> tag.

ch12/atm/Customer.java

Your browser does not support the <object> tag.

ch12/atm/ATMSimulator.java

Your browser does not support the <object> tag. Program Run:

ch12/atm/ATMViewer.java

Your browser does not support the <object> tag.

ch12/atm/ATMFrame.java

Your browser does not support the <object> tag.

ch12/atm/KeyPad.java

Your browser does not support the <object> tag.

Self Check 12.12

Why does the Bank class in this example not store an array list of bank accounts?

Self Check 12.13

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?