Object-Oriented Design
1
Objectives
To become familiar with the process of program development.
To the relationship types: association, aggregation, composition,
strong inheritance, and weak inheritance.
To declare classes to represent the relationships among the classes.
To design systems by identifying the classes and discovering the
relationships among these classes.
To implement the Rational class and process rational numbers
using this class.
To design classes that follow the class-design guidelines.
To model dynamic behavior using sequence diagrams and
statechart diagrams.
To know the concept of framework-based programming using Java
API.
2
Software Development Process
Requirement
Specification
System
Analysis
System
Design
Implementation
Testing
Deployment
Maintenance
3
Requirement Specification
A formal process that seeks to understand
Requirement
Specification
the problem and document in detail what
the software system needs to do. This
System phase involves close interaction between
Analysis
users and designers.
System
Design
Implementation
Testing
Most of the examples in this book are simple,
and their requirements are clearly stated. In Deployment
the real world, however, problems are not
well defined. You need to study a problem Maintenance
carefully to identify its requirements.
4
System Analysis
Requirement
Specification Seeks to analyze the business
process in terms of data flow, and
System
Analysis to identify the system’s input and
output.
System
Design
Implementation
Testing
Part of the analysis entails modeling
the system’s behavior. The model is
Deployment
intended to capture the essential
elements of the system and to define
Maintenance
services to the system.
5
System Design
Requirement
Specification
The process of designing the
system’s components.
System
Analysis
System
Design
Implementation
Testing
This phase involves the use of many levels
Deployment
of abstraction to decompose the problem into
manageable components, identify classes
and interfaces, and establish relationships Maintenance
among the classes and interfaces.
6
Implementation
Requirement The process of translating the
Specification
system design into programs.
System Separate programs are written for
Analysis
each component and put to work
System together.
Design
Implementation
Testing
This phase requires the use of a
programming language like Java. Deployment
The implementation involves
coding, testing, and debugging. Maintenance
7
Testing
Requirement
Specification Ensures that the code meets the
requirements specification and
System
Analysis weeds out bugs.
System
Design
Implementation
Testing
An independent team of software
engineers not involved in the design Deployment
and implementation of the project
usually conducts such testing. Maintenance
8
Deployment
Requirement
Specification Deployment makes the project
available for use.
System
Analysis
System
Design
Implementation
Testing
For a Java applet, this means
installing it on a Web server; for a Deployment
Java application, installing it on the
client's computer. Maintenance
9
Maintenance
Requirement
Specification Maintenance is concerned with
changing and improving the
System
Analysis product.
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 Maintenance
discovered bugs and incorporate changes.
10
Relationships among Classes
Association
Aggregation
Composition
Inheritance
11
Association
Association represents a general binary relationship that describes an activity
between two classes.
Take Teach
5..60 0..3 1
Student * Course Teacher
Faculty
public class Student { public class Course { public class Faculty {
/** Data fields */ /** Data fields */ /** Data fields */
private Course[] private Student[] private Course[]
courseList; classList; courseList;
private Faculty faculty;
/** Constructors */ /** Constructors */
/** Methods */ /** Constructors */ /** Methods */
} /** Methods */ }
}
An association is usually represented as a data field in the class.
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.
13
Association Between Same Class
Association may exist between objects of the same class.
For example, a person may have a supervisor.
1
Person
Supervisor
1
14
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 Aggregation
Name Person Address
15
Representing Aggregation in Classes
An aggregation relationship is usually represented as a data field in
the aggregated class.
public class Name { public class Person { public class Address {
/** Data fields */ /** Data fields */ /** Data fields */
/** Constructors */ private Name name; /** Constructors */
/** Methods */ private Address address; /** Methods */
} }
/** Constructors */
/** Methods */
}
16
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 {
...
}
}
17
Inheritance
Inheritance models the is-an-extension-of
relationship between two classes.
public class Faculty extends Person {
/** Data fields */
Person Faculty
/** Constructors */
/** Methods */
}
(A) (B)
18
Weak Inheritance Relationship
A 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:
public class Student extends Person
implements Comparable {
Person /** Data fields, Constructors, and */
Student /** Methods */
Comparable
/** Implement the compareTo method */
public int compareTo(Object object) {
// ...
}
}
(A) (B)
19
Class Design
1. Identify classes for the system.
2. Describe attributes and methods in each
class.
3. Establish relationships among classes.
4. Create classes.
20
Example Borrowing Loans
Name Person Address
-firstName: String -name: Name -street: String
-mi: char -address: Address -city: String
-lastName: String -state: String
+Person() -zip: String
+Name() +Person(name: Name, address: Address)
+Name(firstName: String, +getName(): Name +Address()
mi: char, lastName: String) +seName(name: Name): void +Address(street: String, city: String,
+getFirstName(): String +getAddress(): Address state: String, zip: String)
+getMi(): char +setAddress(address: Address): void +getStreet(): String
+getLastName(): String +toString(): String +getCity(): String
+setFirstName(firstName: +getState(): String
String): void +getZip(): String
+setMi(mi: char): void +setStreet(street: String): void
+setLastName(lastName: +setCity(city: String): void
String): void Borrower +setState(state: String): void
+getFullName(): String +setZip(zip: String): void
-loan: Loan +getFullAddress(): String
+Borrower()
Loan +Borrower(name: Name, address: Address)
+getLoan(): Loan
+setLoan(loan: Loan): void
+toString(): String
Name Loan Person Borrower Address
21
Example Borrowing Loans, cont.
The following is a test program that uses the
classes Name, Person, Address,
Borrower, and Loan.
BorrowLoan Run
22
Example 11.2 The Rational Class
1
java.lang.Number Rational
+byteValue(): byte -numerator: long
+shortValue(): short -denominator: long
+intValue(): int
+longVlaue(): long +Rational()
+floatValue(): float +Rational(numerator: long, denominator: long)
+doubleValue():double +getNumerator(): long
+getDenominator(): long
+add(secondRational: Rational): Rational
+multiply(secondRational: Rational): Rational
+subtract(secondRational: Rational): Rational
java.lang.Comparable +divide(secondRational: Rational): Rational
+toString(): String
compareTo(Object): int -gcd(n: long, d: long): long
1 Add, Subtract, Multiply, Divide
Rational TestRationalClass Run
23
Class Design Guidelines
Designing a Single Class.
Using Modifiers public, protected, private
and static
Using Inheritance or Aggregation
Using Interfaces or Abstract Classes
24
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.
25
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.
26
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.
27
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.
28
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.
29
Using Visibility Modifiers
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.
30
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
“The Rational Class” is private, for example,
because it is only for internal use within the class.
31
Using the static Modifier
A property that is shared by all the
instances of the class should be declared
as a static property.
32
Using Inheritance or Aggregation
In general, the difference between inheritance
and aggregation is the difference between the
is-an-extension-of 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 aggregation to model the relationship
between the classes Person and Name.
33
Using Inheritance or
Aggregation, cont.
Sometimes, the choice between inheritance
and aggregation 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
aggregation to define the Cylinder class as
follows:
34
Using Inheritance or
Composition, cont.
public class Cylinder {
private Circle circle;
/** Constructors */
/** Methods */
}
35
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.
36
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.
37
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-an-
extension-of relationship, also known as an is-kind-of
relationship, indicates that an object possesses a certain
property. A weak is-an-extension-of 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.
38
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.
39
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
40
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
41
Statechart diagrams
Statechart diagrams describe flow of control of
the object.
Indicate
Initial State
State1
Transition
State2
42
Statechart diagrams, cont.
JVM loads the Use the new operator Invoke the finalize
class for the object to create the object method on the object
Class Loaded Object Created Object Destroyed
43
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.
44
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.
45
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.
46