Top Banner
Chapter 9 Object-Oriented Software Development Software Development Process Analyze Relationships Among Objects Class Development Class Design Guidelines Wrapper Classes Generic Sort Class Generic Matrix Class Generic Linked List Class
44

Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Jan 02, 2016

Download

Documents

Beverly Freeman
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Chapter 9Object-Oriented Software Development

Software Development Process Analyze Relationships Among Objects Class Development Class Design Guidelines Wrapper Classes Generic Sort Class Generic Matrix Class Generic Linked List Class

Page 2: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Software Development

Process

Requirement Specification

System Analysis

System Design

Implementation

Testing

Deployment

Maintenance

Page 3: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Relationships among Classes

Association

Aggregation

Inheritance

Page 4: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Association

Association represents a general

binary relationship that describes an

activity between two classes.

Student FacultyCourse*5..60Take Teach

0..3 1Teacher

Page 5: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Representing Association in ClassesAn association is usually represented as a data field in the class.

public class Student {

/** Data fields */

/** Constructors */

/** Methods */

}

public class Course {

private Student

classList;

private Faculty faculty;

/** Data fields */

/** Constructors */

/** Methods */

}

public class Faculty {

/** Data fields */

/** Constructors */

/** Methods */

}

or

public class Student {

private Course[]

courseList;

/** Data fields */

/** Constructors */

/** Methods */

}

public class Course {

/** Data fields */

/** Constructors */

/** Methods */

}

public class Faculty {

private Course[]

courseList;

/** Data fields */

/** Constructors */

/** Methods */

}

Page 6: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Aggregation

Aggregation is a special form of association,

which represents an ownership relationship

between two classes. Aggregation models the

relationship like has-a, part-of, owns, and

employed-by.

Magazine ConsultantPublisher1*

Owned by Employed by**

Expert

Page 7: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Representing Composition in Classes

A composing class is usually represented

as a data field in the composed class.

public class

Publisher {

 

/**Constructors*

/

  /**Methods*/

}

public class

Consultant {

  private

Publisher[] p;

/**Constructors*/

/**Date

fields*/

  /**Methods*/

}

public class

Magzine {

private

Publisher p;

 

/**Constructors

*/

/**Date

fields*/

  /**Methods*/

}

Page 8: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Inheritance

Inheritance models the is-a relationship

between two classes.

Person

Faculty

Student

Page 9: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Inheritancepublic class Student extends Person {

/**Constructors*/

  /**Methods*/

}

public class Faculty extends Person {

/**Constructors*/

/**Methods*/

}

Page 10: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Weak Inheritance RelationshipA weak is-a relationship can be

represented using interfaces. For

example, the weak is-a

relationship “students are

comparable based on their grades”

can be represented by

implementing the Comparable

interface, as follows:

Page 11: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Weak Inheritance Relationship, cont.public class Student extends Person

implements Comparable {

/** Data fields */

/** Constructors */

/** Methods */

/** Implement the compareTo method */

public int compareTo(Object object) {

// ...

}

}

Page 12: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Class Development

1. Identify classes for the system.

2. Describe attributes and methods in each

class.

3. Establish relationships among classes.

4. Create classes.

Page 13: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Example 9.1 Borrowing Mortgages

NameName BorrowerBorrowerPersonPersonMortgageMortgage

Name

1 1 Has Live 1 1

Mortgage Borrow

Borrower -mortgage: Mortgage +Borrower() +Borrower(name: Name, address: Address) +getMortgage(): Mortgage +setMortgage(mortgage: Mortgage): void +toString(): String

Address -street: String -city: String -state: String -zip: String +Address() +Address(street: String, city: String, state: String, zip: String) getStreet(): String +getCity(): String +getState(): String +getZip(): String +setStreet(street: String): void +setCity(city: String): void +setState(state: String): void +setZip(zip: String): void +getFullAddress(): String

Defined in Example 6.7

Defined in Example 8.6

Person -name: Name -address: Address +Person() +Person(name: Name, address: Address) +getName(): Name +setName(name: Name): void +getAddress(): Address +setAddress(address: Address): void +toString(): String

AddressAddress

Page 14: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Example 9.1 Borrowing Mortgages, cont.

The following is a test program that uses the classes Name, Person, Address, Borrower, and Mortgage.

BorrowMortgageBorrowMortgage RunRun

Page 15: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Example 9.2 The Rational Class

RationalRational RunRunTestRationalClassTestRationalClass

1

1 Add, Subtract, Multiply, Divide

Rational -numerator: long -denominator: long +Rational() +Rational(numerator: long, Denomination: long) +getNumerator(): long +getDenominator(): long +add(secondRational: Rational): Rational +multiply(Rational secondRational): Rational +subtract(Rational secondRational): Rational +divide(Rational secondRational): Rational +toString(): String -gcd(n: long, d: long): long

java.lang.Number +byteValue(): byte +shortValue(): short +intValue(): int +longVlaue(): long +floatValue(): float +doubleValue():double

java.lang.Comparable +int compareTo(Object)

Page 16: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Class Design Guidelines

Designing a Single Class.

Using Modifiers public, protected, private and static

Using Inheritance or Composition

Using Interfaces or Abstract Classes

Page 17: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Designing a Single Class

A class should describe a single entity or a set of similar operations. A single entity with too many responsibilities can be broken into several classes to separate responsibilities. The String class, StringBuffer class, and StringTokenizer class all deal with strings, for example, but have different responsibilities.

Page 18: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Designing a Single Class, cont.

Classes are usually designed for use by many different customers. To make a class useful in a wide range of applications, the class should provide a variety of ways for customization through properties and methods.

Page 19: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Designing a Single Class, cont. Classes are designed for reuse. Users can incorporate classes in many different combinations, orders, and environments. Therefore, you should design a class that imposes no restrictions on what or when the user can do with it, design the properties to ensure that the user can set properties in any order, with any combination of values, and design methods to function independently of their order of occurrence.

Page 20: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Designing a Single Class, cont.

Provide a public default constructor and override the equals method and the toString method defined in the Object class whenever possible.

Page 21: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Designing a Single Class, cont.

Follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. Always place the data declaration before the constructor, and place constructors before methods. Always provide a constructor and initialize variables to avoid programming errors.

Page 22: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Modifiers public, protected, private and static

Each class can present two contracts – one for the users of the class and one for the extenders of the class. Make the fields private and accessor methods public if they are intended for the users of the class. Make the fields or method protected if they are intended for extenders of the class. The contract for the extenders encompasses the contract for the users. The extended class may increase the visibility of an instance method from protected to public, or change its implementation, but you should never change the implementation in a way that violates that contract.

Page 23: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Modifiers public, protected, private and static, cont.

A class should use the private modifier to hide its data from direct access by clients. You can use get methods and set methods to provide users with access to the private data, but only to private data you want the user to see or to modify. A class should also hide methods not intended for client use. The gcd method in the Rational class in Example 9.2, “The Rational Class,” is private, for example, because it is only for internal use within the class.

Page 24: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Modifiers public, protected, private and static, cont.

A property that is shared by all the instances of the class should be declared as a static property.

Page 25: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Inheritance or Composition

In general, the difference between inheritance and composition is the difference between the is-a relationship and the has-a relationship. For example, an apple is fruit; thus, you would use inheritance to model the relationship between the classes Apple and Fruit. A person has a name; thus, you would use composition to model the relationship between the classes Person and Name.

Page 26: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Inheritance or Composition, cont.

Sometimes, the choice between inheritance and composition is not obvious. For example, you have used inheritance to model the relationship between the classes Circle and Cylinder. One could argue that a cylinder consists of circles; thus, you might use composition to define the Cylinder class as follows:

Page 27: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Inheritance or Composition, cont.

public class Cylinder { private Circle circle;  /** Constructors */  /** Methods */}

Page 28: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Inheritance or Composition, cont.

Both designs are fine. Which one is preferred? If polymorphism is desirable, you need to use the inheritance design. If you don’t care about polymorphism, the composition design gives more flexibility because the classes are less dependent using composition than using inheritance.

Page 29: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Interfaces or Abstract Classes

Both interfaces and abstract classes can be used to generalize common features. How do you decide whether to use an interface or a class? In general, a strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes.

Page 30: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Interfaces or Abstract Classes, cont.

For example, since an orange is a fruit, their relationship should be modeled using class inheritance. A weak is-a relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property. A weak is-a relationship can be modeled using interfaces. For example, all strings are comparable, so the String class implements the Comparable interface. A circle or a rectangle is a geometric object, for example, so Circle can be designed as a subclass of GeometricObject. Circles are different and comparable based on their radius, for example, so Circle can implement the Comparable interface.

Page 31: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Using Interfaces or Abstract Classes, cont.

Interfaces are more flexible than abstract classes, because a subclass can extend only one superclass, but implement any number of interfaces. However, interfaces cannot contain concrete methods. You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface. So you can use the interface or its companion class whichever is more convenient.

Page 32: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Wrapper Classes

Boolean

Character

Short

Byte

Integer Long

Float

Double

Object

-

Double -

Float -

Long -

Integer -

Short -

Byte -

Character -

Boolean -

Number -

Comparable -

Page 33: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

The Integer Classand The Double Class

Constructors

Class Constants MAX_VALUE, MIN_VALUE

Conversion Methods

Page 34: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Example 9.3 Sorting an Array of Objects

Objective: The example presents a generic method for sorting an array of objects. The objects are instances of the Comparable interface and they are compared using the compareTo method.

GenericSortGenericSort RunRun

Page 35: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Example 9.4Designing Generic Classes

Objective: This example gives a generic class for matrix arithmetic. This class implements matrix addition and multiplication common for all types of matrices.

GenericMatrixGenericMatrix

Page 36: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Example 9.4, cont.

GenericMatrix

-matrix: Object[][] #GenericMatrix(matrix: Object[][]) +getMatrix(): Object[][] +setMatrix(matrix: Object[][]): void +addMatrix(secondMatrix: Object[][]): Object[][] +multiplyMatrix(secondMatrix: Object[][]): Object[][] +printResult(m1: GenericMatrix, m2: GenericMatrix, m3: GenericMatrix, op: char): void #createGenericMatrix():GenericMatrix #add(o1: Object, o2: Object): Object #multiply(o1: Object, o2: Object): Object #zero():Object

IntegerMatrix

RationalMatrix

Page 37: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Example 9.4, cont.

Objective: This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic.

TestIntegerMatrixTestIntegerMatrix RunRun

TestRationalMatrixTestRationalMatrix RunRunRational MatrixRational Matrix

IntegerMatrixIntegerMatrix

Page 38: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Linked ListArrays are useful for storing and managing a set of elements of the same type. However, since the length of an array is fixed once the array is created, you need to know the length of the array before you create it. A linked list can grow or shrink dynamically as needed.

A linked list consists of nodes, as shown in Figure 9.14. Each node contains an element and each node is linked to its next neighbor. Thus, a node can be defined as a class as follows:

Page 39: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Linked List Structure

public class Node { Object element; Node next;  public Node(Object o) { element = o; }}

first element

next

element next

element next

last …

node1 node2 node n

Page 40: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Create a Linked List with three Nodes

Node n1 = new Node(new String(“Welcome to Java!”));

Node n2 = new Node(new JButton(“OK”));

Node n3 = new Node(new Rational(1, 2));

n1.next = n2;

n2.next = n3;

Node first = n1;

Node last = n3;

Page 41: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Generic Linked List

… element next

element next

current

first element next

element next:null

last …

element next

New node inserted here

temp

Add a new node

Page 42: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Generic Linked List

Remove a new node

… element next

element next

previous

first element next

element next:null

last …

Node to be deleted

current

After the node isdeleted

Page 43: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Example 9.5 Using Linked List

This example creates a linked list using GenericLinkedList. It then uses the add method to add strings to the list and uses the remove method to remove strings from the list.

GenericLinkedListGenericLinkedList

RunRunTestLinkedListTestLinkedList

Page 44: Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Generic Linked List

GenericLinkedList list = new GenericLinkedList();

list.addLast(new String(“Welcome to Java”));list.addLast(new JButton(“OK”));list.addLast(new Rational(1, 2));