Download Chapter 10 slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Functional programming wikipedia , lookup

Java syntax wikipedia , lookup

Reserved word wikipedia , lookup

Scala (programming language) wikipedia , lookup

Go (programming language) wikipedia , lookup

Reactive programming wikipedia , lookup

Structured programming wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Name mangling wikipedia , lookup

Design Patterns wikipedia , lookup

Java (programming language) wikipedia , lookup

C++ wikipedia , lookup

Class (computer programming) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Java performance wikipedia , lookup

Object-oriented programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Chapter 10 Object-Oriented Modeling
Prerequisites for Part II
Chapter 5 Arrays
Chapter 6 Objects and Classes
Chapter 7 Strings
You can cover GUI after Chapter 8
Chapter 8 Inheritance and Polymorphism
Chapter 11 Getting Started with GUI Programming
Chapter 9 Abstract Classes and Interfaces
Chapter 12 Event-Driven Programming
Chapter 10 Object-Oriented Modeling
Chapter 15 Exceptions and Assertions
You can cover Exceptions and I/O after Chapter 8
Chapter 16 Simple Input and Output
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
1
Objectives








To become familiar with the process of program development
(§10.2).
To the relationship types: association, aggregation, strong
inheritance, and weak inheritance (§10.3).
To declare classes to represent the relationships among the classes
(§10.3).
To design systems by identifying the classes and discovering the
relationships among these classes (§10.4).
To implement the Rational class and process rational numbers using
this class (§10.5).
To design classes that follow the class-design guidelines (§10.6).
To model dynamic behavior using sequence diagrams and statechart
diagrams (§10.7 Optional)
To know the concept of framework-based programming using Java
API (§10.8).
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
2
Software Development Process
Requirement
Specification
System
Analysis
System
Design
Implementation
Testing
Deployment
Maintenance
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
3
Requirement Specification
Requirement
Specification
System
Analysis
System
Design
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.
Implementation
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.
Testing
Deployment
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Maintenance
4
System Analysis
Requirement
Specification
System
Analysis
System
Design
Seeks to analyze the business
process in terms of data flow, and
to identify the system’s input and
output.
Implementation
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.
Testing
Deployment
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Maintenance
5
System Design
Requirement
Specification
The process of designing the
system’s components.
System
Analysis
System
Design
Implementation
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.
Testing
Deployment
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Maintenance
6
Implementation
The process of translating the
system design into programs.
Separate programs are written for
each component and put to work
together.
Requirement
Specification
System
Analysis
System
Design
Implementation
This phase requires the use of a
programming language like Java.
The implementation involves
coding, testing, and debugging.
Testing
Deployment
Maintenance
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
7
Testing
Requirement
Specification
Ensures that the code meets the
requirements specification and
weeds out bugs.
System
Analysis
System
Design
Implementation
An independent team of software
engineers not involved in the design
and implementation of the project
usually conducts such testing.
Testing
Deployment
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Maintenance
8
Deployment
Requirement
Specification
Deployment makes the project
available for use.
System
Analysis
System
Design
Implementation
For a Java applet, this means
installing it on a Web server; for a
Java application, installing it on the
client's computer.
Testing
Deployment
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Maintenance
9
Maintenance
Requirement
Specification
Maintenance is concerned with
changing and improving the
product.
System
Analysis
System
Design
Implementation
Testing
A software product must continue to
perform and improve in a changing
Deployment
environment. This requires periodic
upgrades of the product to fix newly
discovered bugs and incorporate
changes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Maintenance
10
Relationships among Classes
 Association
 Aggregation
 Composition
 Inheritance
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
11
Association
Association represents a general binary relationship that describes
an activity between two classes.
Student
Take
5..60
* Course
public class Student {
/** Data fields */
private Course[]
courseList;
0..3
public class Course {
/** Data fields */
private Student[]
classList;
private Faculty faculty;
/** Constructors */
/** Methods */
/** Constructors */
/** Methods */
}
Teach
1
Teacher
Faculty
public class Faculty {
/** Data fields */
private Course[]
courseList;
/** Constructors */
/** Methods */
}
}
An association is usually represented as
a data field in the class.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
12
Translation is not Unique
NOTE: If you don’t need to know the courses a student
takes or a faculty teaches, the data field coureList in Student
or Faculty can be omitted.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
13
Aggregation and Composition
Aggregation 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.
Composition
Name
Aggregation
Person
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Address
14
Representing Aggregation in Classes
An 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;
public class Address {
/** Data fields */
/** Constructors */
/** Methods */
}
/** Constructors */
/** Methods */
}
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
15
Inner Classes Translation
If 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 {
...
}
}
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
16
Inheritance
Inheritance models the is-a relationship between
two classes.
public class Student extends Person {
Person
/** Data fields */
/** Constructors */
/** Methods */
Faculty
}
(A)
(B)
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
17
Weak Inheritance Relationship
A 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:
public class Student extends Person
implements Comparable {
Person
/** Data fields, Constructors, and */
/** Methods */
Student
Comparable
/** Implement the compareTo method */
public int compareTo(Object object) {
// ...
}
}
(A)
(B)
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
18
Class Design
1. Identify classes for the system.
2. Describe attributes and methods in each
class.
3. Establish relationships among classes.
4. Create classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
19
Example 10.1 Borrowing Loans
Name
Person
-firstName: String
-mi: char
-lastName: String
-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()
+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
Borrower
-loan: Loan
Loan
Defined in
Example 6.7
Name
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
+Borrower()
+Borrower(name: Name, address: Address)
+getLoan(): Loan
+setLoan(loan: Loan): void
+toString(): String
Loan
Person
Borrower Address
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
20
Example 10.1 Borrowing Loans,
cont.
The following is a test program that uses the
classes Name, Person, Address,
Borrower, and Loan.
BorrowLoan
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Run
21
Example 10.2 The Rational Class
1
java.lang.Number
+byteValue(): byte
+shortValue(): short
+intValue(): int
+longVlaue(): long
+floatValue(): float
+doubleValue():double
java.lang.Comparable
compareTo(Object): int
Rational
-numerator: long
-denominator: long
+Rational()
+Rational(numerator: long, denomination: 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
1
Rational
TestRationalClass
Add, Subtract, Multiply, Divide
Run
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
22
Class Design Guidelines
 Designing
a Single Class.
 Using
Modifiers public, protected, private
and static
 Using
Inheritance or Composition
 Using
Interfaces or Abstract Classes
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
23
Designing a 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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
24
Designing a 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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
25
Designing a 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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
26
Designing a Class, cont.
 Provide
a public no-arg constructor and override the
equals method and the toString method defined in
the Object class whenever possible.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
27
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
28
Using Visibility Modifiers
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.
29
 Each
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Using Visibility Modifiers, 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 10.2,
“The Rational Class,” is private, for example,
because it is only for internal use within the class.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
30
Using the static Modifier
 A property
that is shared by all the
instances of the class should be declared
as a static property.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
31
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
32
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:
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
33
Using Inheritance or
Composition, cont.
public class Cylinder {
private Circle circle;
/** Constructors */
/** Methods */
}
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
34
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
35
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
36
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
37
interface.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
38
Sequence diagrams
Sequence diagrams describe interactions among
objects by depicting the time ordering of method
invocations.
Class role
anObject:
TheClass
anotherObject:
TheOtherClass
Method Invocation
aMethod()
Activation
anotherMethod()
Method Invocation
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
39
Sequence diagrams, cont.
: BorrowLoan
name: Name
address: Address
loan: Loan
borrower: Borrower
setFirstName
setMi
setLastName
setStreet
setCity
setState
setZip
setAnnualInterestRate
setNumOfYears
setLoanAmount
setName
setAddress
setLoan
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
40
Statechart diagrams
Statechart diagrams describe flow of control of the
object.
Indicate
Initial State
State1
Transition
State2
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
41
Statechart diagrams, cont.
JVM loads the
class for the object
Use the new operator
to create the object
Class Loaded
Invoke the finalize
method on the object
Object Created
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
Object Destroyed
42
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
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
43
Example 10.3, cont.
GenericMatrix
IntegerMatrix
-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
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
RationalMatrix
44
Example 10.3, cont.

Objective: This example gives two programs that
utilize the GenericMatrix class for integer matrix
arithmetic and rational matrix arithmetic.
IntegerMatrix
TestIntegerMatrix
Run
RationalMatrix
TestRationalMatrix
Run
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
45
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
46
Framework-Based Programming
To 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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
47
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
48
Framework-Based Programming, cont.
You will learn the Java GUI programming
framework in Chapters 11, 12, 13, and 14, the
Java exception handling framework in Chapter
15, the Java IO framework in Chapter 16, the
Java collections framework in Chapter 18, the
Java multithreading framework in Chapter 19,
and JDBC in Chapter 25, Servlets in Chapter 26,
Networking in Chapter 28, and RMI in Chapter
29.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All
rights reserved. 0-13-148952-6
49