Big Java 4

Chapter 12 – Object-Oriented Design

Chapter Goals

people designing

Discovering Classes

Example: Invoice

an invoice

Figure 1 An Invoice

Example: Invoice

The CRC Card Method

lecture hall

 

In a class scheduling system, potential classes from the problem domain include Class, LectureHall, Instructor, and Student.

The CRC Card Method

The CRC Card Method

Self Check 12.1

What is the rule of thumb for finding classes?

Self Check 12.2

Your job is to write a program that plays chess. Might ChessBoard be an appropriate class? How about MovePiece?

Self Check 12.3

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

Self Check 12.4

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

Self Check 12.5

What do you do if a CRC card has ten responsibilities?

Relationships Between Classes

The most common types of relationships:

Dependency

Dependency

Aggregation

Aggregation

acar aggregates parts

 

A car has a motor and tires. In object-oriented design, this “has-a” relationship is called aggregation.


Inheritance

Inheritance

UML Relationship Symbols

Relationship Symbol Line Style Arrow Tip
Inheritance inheritance arrow Solid Triangle
Interface Implementation interface arrow Dotted Triangle
Aggregation aggregation arrow Solid Diamond
Dependency dependency arrow Dotted Open

Self Check 12.6

Consider the CashRegisterTester class of Section 8.2. On which classes does it depend?

Self Check 12.7

Consider the Question and ChoiceQuestion objects of Chapter 9. How are they related?

Self Check 12.8

Consider the Quiz class described in Section 12.2.2. Suppose a quiz contains a mixture of Question and ChoiceQuestion objects. Which classes does the Quiz class depend on?

Self Check 12.9

Why should coupling be minimized between classes?

Self Check 12.10

In an e-mail system, messages are stored in a mailbox. Draw a UML diagram that shows the appropriate aggregation relationship.

Self Check 12.11

You are implementing a system to manage a library, keeping track of which books are checked out by whom. Should the Book class aggregate Patron or the other way around?

Self Check 12.12

In a library management system, what would be the relationship between classes Patron and Author?

Attributes and Methods in UML Diagrams

UML attributes and methods

Multiplicities

Aggregation and Association, and Composition

Application: Printing an Invoice

Five-part program 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

Application: Printing an Invoice — Requirements

Application: Printing an Invoice

Application: Printing an Invoice — CRC Cards

Application: Printing an Invoice — CRC Cards

CRC Cards for Printing Invoice

Invoice and Address must be able to format themselves:

invoice CRC 1 address CRC

CRC Cards for Printing Invoice

Add collaborators to Invoice card:

invoice CRC 2

CRC Cards for Printing Invoice

Product and LineItem CRC cards:

product CRC line item CRC

CRC Cards for Printing Invoice

Invoice must be populated with products and quantities:

invoice CRC 3

Application: Printing an Invoice — UML Diagrams

UML for Invoice classes

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

javadoc

Figure 8 Class Documentation in 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

section_3/InvoicePrinter.java

Your browser does not support the <object> tag.

section_3/Invoice.java

Your browser does not support the <object> tag.

section_3/LineItem.java

Your browser does not support the <object> tag.

section_3/Product.java

Your browser does not support the <object> tag.

section_3/Address.java

Your browser does not support the <object> tag.

Self Check 12.13

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

Self Check 12.14

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