chapter 11 object-oriented design

66
1 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pears on Education, Inc. All rights reserved. 0-13-222158-6 Chapter 11 Object-Oriented Design Chapter7 O bjectsand Classes Chapter8 Stringsand TextI/O Chapter9 Inheritance and Polym orphism Chapter6 A rrays §10.2, “A bstractClasses” §10.4, “Interfaces” Chapter13 G raphics Chapter14 Event-D riven Program ming Chapter17 Exceptionsand A ssertions Chapter18 Binary I/O Exception and binary I/O can be covered afterChapter9 G U Ican be covered after§10.2, “A bstractClasses” Chapter12 G U IBasics Chapter11 Object-O riented D esign

Upload: varick

Post on 23-Mar-2016

52 views

Category:

Documents


0 download

DESCRIPTION

Chapter 11 Object-Oriented Design. Objectives from book. To become familiar with the process of program development (§11.2). To the relationship types: association, aggregation, composition, strong inheritance, and weak inheritance (§11.3). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 11 Object-Oriented Design

1Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Chapter 11 Object-Oriented Design

Chapter 7 Objects and Classes

Chapter 8 Strings and Text I/O

Chapter 9 Inheritance and Polymorphism

Chapter 6 Arrays

§10.2, “Abstract Classes”

§10.4, “Interfaces”

Chapter 13 Graphics

Chapter 14 Event-Driven Programming

Chapter 17 Exceptions and Assertions

Chapter 18 Binary I/O

Exception and binary I/O can be covered after Chapter 9

GUI can be covered after §10.2, “Abstract Classes” Chapter 12 GUI Basics

Chapter 11 Object-Oriented Design

Page 2: Chapter 11 Object-Oriented Design

2Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Objectives from book To become familiar with the process of program development

(§11.2). To the relationship types: association, aggregation, composition,

strong inheritance, and weak inheritance (§11.3). To declare classes to represent the relationships among the classes

(§11.3). To design systems by identifying the classes and discovering the

relationships among these classes (§11.4). To implement the Rational class and process rational numbers

using this class (§11.5). To design classes that follow the class-design guidelines (§11.6). To model dynamic behavior using sequence diagrams and

statechart diagrams (§11.7 Optional) To know the concept of framework-based programming using Java

API (§11.8).

Page 3: Chapter 11 Object-Oriented Design

3Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

My Objectives

Object Oriented Modeling Discuss real world experience

– Network management System– PDA Products

Unit Testing

Page 4: Chapter 11 Object-Oriented Design

4Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Software Development Process Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

Page 5: Chapter 11 Object-Oriented Design

5Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Requirement Specification Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

A formal process that seeks to understand the problem and document in detail what the software system needs to do. This phase involves close interaction between users and designers.

Most of the examples in this book are simple, and their requirements are clearly stated. In the real world, however, problems are not well defined. You need to study a problem carefully to identify its requirements.

Page 6: Chapter 11 Object-Oriented Design

6Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

System AnalysisRequirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

Seeks to analyze the business process in terms of data flow, and to identify the system’s input and output.

Part of the analysis entails modeling the system’s behavior. The model is intended to capture the essential elements of the system and to define services to the system.

Page 7: Chapter 11 Object-Oriented Design

7Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

System Design Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

The process of designing the system’s components.

This phase involves the use of many levels of abstraction to decompose the problem into manageable components, identify classes and interfaces, and establish relationships among the classes and interfaces.

Page 8: Chapter 11 Object-Oriented Design

8Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Implementation Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

The process of translating the system design into programs. Separate programs are written for each component and put to work together.

This phase requires the use of a programming language like Java. The implementation involves coding, testing, and debugging.

Page 9: Chapter 11 Object-Oriented Design

9Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Testing Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

Ensures that the code meets the requirements specification and weeds out bugs.

An independent team of software engineers not involved in the design and implementation of the project usually conducts such testing.

Page 10: Chapter 11 Object-Oriented Design

10Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Deployment Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

Deployment makes the project available for use.

For a Java applet, this means installing it on a Web server; for a Java application, installing it on the client's computer.

Page 11: Chapter 11 Object-Oriented Design

11Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Maintenance Requirement Specification

System Analysis

System Design

Testing

Implementation

Maintenance

Deployment

Maintenance is concerned with changing and improving the product.

A software product must continue to perform and improve in a changing environment. This requires periodic upgrades of the product to fix newly discovered bugs and incorporate changes.

Page 12: Chapter 11 Object-Oriented Design

12Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Software Methodologies

Waterfall Model Evolutionary Model Incremental Model Agile Methodology

Page 13: Chapter 11 Object-Oriented Design

13Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Waterfall Model Top-down Development steps are executed sequentially Gate approvals between consecutive steps Documentation oriented Advantages:

– simple for management Disadvantage:

– Documents are costly to produce and approve. – Some steps would get frozen prematurely

Not developing what the customer wants Being inflexible to change.

Page 14: Chapter 11 Object-Oriented Design

14Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Evolutionary Model Performs all processes in at least 2 passes First pass implements only those requirements that

are firm or known. Negotiates with the customer after the completion

of a pass before the next pass begins. Advantage:

– Developing requirements incrementally – meeting the customer’s immediate needs.

Disadvantage– resulting system architecture is often corrupted.

Page 15: Chapter 11 Object-Oriented Design

15Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Incremental Model customers identify the system features and prioritize them. Development prioritizes delivery increments. Each successive delivery includes an enlarging subset of the feature

list. Requirements of the first increment are frozen, development proceeds

in detail on the increment. Concurrently consider requirement changes for the next increment Advantage:

– Enables faster delivery to the customer who in turn gains experience on the system and provide feedback for later increments.

– Lower risk of overall project failure . top priority features are delivered first - tested more

thoroughly by the development organization as well as in operation.

Page 16: Chapter 11 Object-Oriented Design

16Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Agile Principles:

– Individuals and interactions over processes and tools – Working software over comprehensive documentation – Customer collaboration over contract negotiation– Responding to change over following a plan

Goals: – Accept customer changes any time; – have short daily communications between customers and

developers, – develop and deliver small increments in lieu of one release as at

the end of the waterfall model. Advocates putting top developers in a bullpen and let the team self

organize. Problem: Documentation is definitely downplayed.

Small Projects (< 10 )

Page 17: Chapter 11 Object-Oriented Design

17Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Observations Waterfall model is a conceptual model Personally have worked on combination of

waterfall, incremental, and evolutionary Maintenance is 80% of the life cycle cost

– Problems? Documentation Institutional knowledge

Software industry often reinvents the wheel

Page 18: Chapter 11 Object-Oriented Design

18Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Some challenges on Software Architecture

Network Management System PDA

Page 19: Chapter 11 Object-Oriented Design

19Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Optical network and Network Management

WS WS

Wide Area Network

EMS NMS EMS

XC XC

NE

NE

NE

NE

NE

NE

NE

Page 20: Chapter 11 Object-Oriented Design

20Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Network Management System Example

An optical communication network using SDH Network management system another network using

TMN Diverse nodes with common functions and differing in

hardware, functionalities, and capacities Developed by globally distributed teams Development of releases transitioned successfully from

one team to another Two key factors are product line architecture;

incremental and iterative development

Page 21: Chapter 11 Object-Oriented Design

21Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Product Architecture to Product Line Architecture

Each delivery is custom-tailored Paradigm shift: Product Development

House to Product Line Platform to generate similar products with custom options.

Benefits – reuse

Page 22: Chapter 11 Object-Oriented Design

22Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Software Architecture – Logical View

Database for– Configuration Management – on individual network

configuration– Account management – for end customers

Superclass and subclasses– NE common properties – NE specific properties– New NE’s just inherit the common properties and

override those that are specific

Page 23: Chapter 11 Object-Oriented Design

23Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Applications PDA: From calendar, address book, calculator,

notes, spreadsheet To

– exchange business cards– internet– email– instant messaging – on-line search– VoIP– GPS– camera etc.

Page 24: Chapter 11 Object-Oriented Design

24Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Relationships among Classes

Association Aggregation Composition Inheritance

Page 25: Chapter 11 Object-Oriented Design

25Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Other Applications

n-tier architectures: GUI, server, and DB

Page 26: Chapter 11 Object-Oriented Design

26Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Back to OOM

OOM concepts

Page 27: Chapter 11 Object-Oriented Design

27Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Association Association represents a general binary relationship that describes an activity between two classes.

Student * 5..60

Take Teach 0..3 1

Teacher Faculty Course

public class Student { /** Data fields */ private Course[] courseList; /** Constructors */ /** Methods */ }

public class Course { /** Data fields */ private Student[]

classList; private Faculty faculty; /** Constructors */ /** Methods */ }

public class Faculty { /** Data fields */ private Course[] courseList; /** Constructors */ /** Methods */ }

An association is usually represented as a data field in the class.

Page 28: Chapter 11 Object-Oriented Design

28Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Translation is not Unique

NOTE: If you don’t need to know the courses a student takes or a faculty teaches, the data field courseList in Student or Faculty can be omitted.

Page 29: Chapter 11 Object-Oriented Design

29Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Association Between Same Class

Association may exist between objects of the same class. For example, a person may have a supervisor.

Person Supervisor

1

1

Page 30: Chapter 11 Object-Oriented Design

30Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Aggregation and CompositionAggregation is a special form of association, which represents an ownership relationship between two classes. Aggregation models the has-a relationship. If an object is exclusively owned by an aggregated object, the relationship between the object and its aggregated object is referred to as composition.

Name Address Person

Composition Aggregation

Page 31: Chapter 11 Object-Oriented Design

31Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Representing Aggregation in ClassesAn aggregation relationship is usually represented as a data field in the aggregated class.

public class Name { /** Data fields */ /** Constructors */ /** Methods */ }

public class Person { /** Data fields */ private Name name; private Address address; /** Constructors */ /** Methods */ }

public class Address { /** Data fields */ /** Constructors */ /** Methods */ }

Page 32: Chapter 11 Object-Oriented Design

32Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Inner Classes TranslationIf Name or Address is used in the Person class only, they can be declared as an inner class in Person. For example,

public class Person { private Name name; private Address address; ... class Name { ... } class Address { ... }}

Page 33: Chapter 11 Object-Oriented Design

33Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

InheritanceInheritance models the is-an-extension-of relationship between two classes.

Person Faculty

public class Faculty extends Person { /** Data fields */ /** Constructors */ /** Methods */ }

(A) (B)

Page 34: Chapter 11 Object-Oriented Design

34Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Weak Inheritance RelationshipA weak is-an-extension-of relationship can be represented using interfaces. For example, the weak is-an-extension-of relationship “students are comparable based on their grades” can be represented by implementing the Comparable interface, as follows:

Person Student

Comparable

public class Student extends Person implements Comparable { /** Data fields, Constructors, and */ /** Methods */ /** Implement the compareTo method */ public int compareTo(Object object) { // ... } }

(A) (B)

Page 35: Chapter 11 Object-Oriented Design

35Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Class Design

1. Identify classes for the system.

2. Describe attributes and methods in each

class.

3. Establish relationships among classes.

4. Create classes.

Page 36: Chapter 11 Object-Oriented Design

36Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Example 11.1 Borrowing Loans

Name BorrowerPersonLoan Address

Loan

Borrower -loan: Loan +Borrower() +Borrower(name: Name, address: Address) +getLoan(): Loan +setLoan(loan: Loan): 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

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

Name -firstName: String -mi: char -lastName: String +Name() +Name(firstName: String, mi: char, lastName: String) +getFirstName(): String +getMi(): char +getLastName(): String +setFirstName(firstName: String): void +setMi(mi: char): void +setLastName(lastName: String): void +getFullName(): String

Page 37: Chapter 11 Object-Oriented Design

37Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Example 11.1 Borrowing Loans, cont.

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

BorrowLoan Run

Page 38: Chapter 11 Object-Oriented Design

38Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Example 11.2 The Rational Class

Rational RunTestRationalClass

1

1 Add, Subtract, Multiply, Divide

Rational -numerator: long -denominator: long +Rational() +Rational(numerator: long, denominator: long) +getNumerator(): long +getDenominator(): long +add(secondRational: Rational): Rational +multiply(secondRational: Rational): Rational +subtract(secondRational: Rational): Rational +divide(secondRational: Rational): 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 compareTo(Object): int

Page 39: Chapter 11 Object-Oriented Design

39Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Class Design Guidelines Designing a Single Class.

Using Modifiers public, protected, private and static

Using Inheritance or Aggregation Using Interfaces or Abstract Classes

Page 40: Chapter 11 Object-Oriented Design

40Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Designing a Class A class should describe a single entity or a set of

similar operations. Break a single entity with too many

responsibilities into several classes to separate responsibilities.

Example: The String class, StringBuffer class, and StringTokenizer class all deal with strings, but have different responsibilities.

Page 41: Chapter 11 Object-Oriented Design

41Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Designing a Class, cont. Classes are usually designed for use by a wide

range of applications– Provide a variety of ways for customization through

properties and methods.

Page 42: Chapter 11 Object-Oriented Design

42Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Designing a Class, cont. Classes are designed for reuse. Users can incorporate classes in many different

combinations, orders, and environments.

Hence design a class that – DON’T: imposes restrictions on what or when the user

can do with it,

– DO: ensure that properties can be set in any order, with any combination of values, and methods function independently of their order of occurrence.

Page 43: Chapter 11 Object-Oriented Design

43Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Designing a Class, cont. Provide a public no-arg constructor Override the equals method and the toString method

defined in the Object class whenever possible.

Page 44: Chapter 11 Object-Oriented Design

44Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Designing a 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 45: Chapter 11 Object-Oriented Design

45Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Using Visibility Modifiers Each class can present two contracts:

– one for the users of the class – one for the extenders of the class.

Make 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 46: Chapter 11 Object-Oriented Design

46Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Using Visibility Modifiers, cont. Use the private modifier to hide its data from direct

access by clients. 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.

Hide methods not intended for client use. – Example 11.2: gcd method in the Rational class is private

because it is only for internal use within the class.

Page 47: Chapter 11 Object-Oriented Design

47Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Using the static Modifier

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

Page 48: Chapter 11 Object-Oriented Design

48Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Using Inheritance or Aggregation?

inheritance versus aggregation =is-an-extension-of relationship versus has-a relationship. Example: an apple is fruit; so use inheritance to model the relationship between the classes Apple and Fruit. Example: A person has a name; so use aggregation to model the relationship between the classes Person and Name.

Page 49: Chapter 11 Object-Oriented Design

49Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Using Inheritance or Aggregation? cont.

The choice between inheritance and aggregation is not always obvious. Example: we used inheritance to model the relationship between the classes Circle and Cylinder. But … if you argue that a cylinder consists of circles; thus, you might use aggregation to define the Cylinder class as follows:

Page 50: Chapter 11 Object-Oriented Design

50Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Using Inheritance or Composition, cont.

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

Page 51: Chapter 11 Object-Oriented Design

51Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Using Inheritance or Aggregation? 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 aggregation design gives more flexibility because the classes are less dependent using aggregation than using inheritance.

Page 52: Chapter 11 Object-Oriented Design

52Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

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-an-extension-of relationship that clearly describes a parent-child relationship should be modeled using classes.

Page 53: Chapter 11 Object-Oriented Design

53Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Using Interfaces or Abstract Classes, cont.

Example: An orange is a fruit:

– Model their relationship using class inheritance. A weak is-an-extension-of relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property.

– Model a weak is-an-extension-of relationship with interfaces.

– All strings are comparable, so the String class implements the Comparable interface.

– A circle or a rectangle is a geometric object, so Circle can be designed as a subclass of GeometricObject.

– Circles are different and comparable based on their radius, so Circle can implement the Comparable interface.

Page 54: Chapter 11 Object-Oriented Design

54Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Using Interfaces or Abstract Classes, cont.

Interfaces are more flexible than abstract classes– a subclass can extend only one superclass, but

implement any number of interfaces. Interfaces cannot contain concrete methods.

– 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 55: Chapter 11 Object-Oriented Design

55Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Page 56: Chapter 11 Object-Oriented Design

56Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Sequence diagramsSequence diagrams describe interactions among objects by depicting the time ordering of method invocations.

anObject: TheClass Class role

Method Invocation

Activation

anotherObject: TheOtherClass

Method InvocationanotherMethod()

aMethod()

Page 57: Chapter 11 Object-Oriented Design

57Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Sequence diagrams, cont.

name: Name : BorrowLoan address: Address loan: Loan borrower: Borrower

setFirstName

setMi

setLastName

setStreet

setCity

setState

setZip

setAnnualInterestRate

setNumOfYears

setLoanAmount

setName

setAddress

setLoan

Page 58: Chapter 11 Object-Oriented Design

58Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Statechart diagramsStatechart diagrams describe flow of control of the object.

IndicateInitial State

Transition

State1

State2

Page 59: Chapter 11 Object-Oriented Design

59Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Statechart diagrams, cont.

Class Loaded

JVM loads the class for the object

Use the new operator to create the object

Object Created

Invoke the finalize method on the object

Object Destroyed

Page 60: Chapter 11 Object-Oriented Design

60Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Supplement P: Designing Generic Matrix Classes

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

GenericMatrix

Page 61: Chapter 11 Object-Oriented Design

61Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Example 11.3, 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 62: Chapter 11 Object-Oriented Design

62Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Example 11.3, cont.

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

TestIntegerMatrix Run

TestRationalMatrix RunRationalMatrix

IntegerMatrix

Page 63: Chapter 11 Object-Oriented Design

63Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

The Java API

The Java API (Application Program Interface, Application Programming Interface, or Application Programmer interface) consists of numerous classes and interfaces grouped into more than a dozen of packages. You have used classes and interfaces in the java.lang, javax.swing, and java.util packages.

Page 64: Chapter 11 Object-Oriented Design

64Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Framework-Based ProgrammingTo create comprehensive projects, you have to use more classes and interfaces in the Java API. The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java. For example, the classes and interfaces in the Java GUI API establish a framework for developing GUI programs. You have to use these classes and interfaces and follow their conventions and rules to create applications. This is referred to as framework-based programming.

Page 65: Chapter 11 Object-Oriented Design

65Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Framework-Based Programming, cont.

Once you understand the concept of Java and object-orient programming, the most important lesson from now on is learning how to use the API to develop useful programs. The most effective way to achieve it is to imitate good examples. The book provides many carefully designed examples to demonstrate the concept of the framework-based programming using the Java API.

Page 66: Chapter 11 Object-Oriented Design

66Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6

Framework-Based Programming, cont.

You will learn the Java GUI programming framework in Part III, the Java exception handling framework in Chapter 17, the Java IO framework in Chapter 18, the Java collections framework in Chapter 22, the Java multithreading framework in Chapter 24, Networking in Chapter 25, Internationalization in Chapter 26, and JDBC in Chapters 32-33, Servlets in Chapter 34and RMI in Chapter 36.