Object-Oriented Design
1
Object-Oriented Design
2
Object-Oriented Design
Chapter Goals
✓To learn about the software life cycle
✓To learn how to discover new classes and methods
✓To be able to identify association, aggregation, composition
and dependency relationships between classes
✓To master the use of UML class diagrams to describe class
relationships
✓To learn how to use object-oriented design to build complex
programs
3
The Software Life Cycle
✓Encompasses all activities from initial analysis until
obsolescence
✓Formal process for software development
▪ Describes phases of the development process
▪ Gives guidelines for how to carry out the phases
✓Development process
▪ Analysis
▪ Design
▪ Implementation
▪ Testing
▪ Deployment
4
Analysis
✓Decide what the project is supposed to do
✓Do not think about how the program will accomplish tasks
✓Output: Requirements document
▪ Describes what program will do once completed
▪ User manual: Tells how user will operate program
▪ Performance criteria
5
Design
✓Plan how to implement the system
✓Discover structures that underlie problem to be solved
✓Decide what classes and methods you need
✓Output:
▪ Description of classes and methods
▪ Diagrams showing the relationships among the classes
6
Implementation
✓Write and compile the code
✓Code implements classes and methods discovered in the design
phase
✓Program Run: Completed program
7
Testing
✓Run tests to verify the program works correctly
✓Program Run: A report of the tests and their results
8
Deployment
✓Users install program
✓Users use program for its intended purpose
9
The Waterfall Model
✓Sequential process of analysis, design, implementation, testing,
and deployment
✓When rigidly applied,
waterfall model did not
work
10
Activity Levels in the Rational Unified Process
✓Development process methodology by the inventors of UML
11
Principles
✓Industry standard process model, initiated by Ericsson, then
Objectory and Rational companies
✓Development of an OO system
✓Uses the UML notation throughout the process:
▪ different views that are informal and not necessarily consistent.
✓Supports an iterative and incremental process
✓Decomposes a large process into controlled iterations (mini
projects)
12
Object-Oriented Design
✓Discover classes
✓Determine responsibilities of each class
✓Describe relationships between the classes
13
Discovering Classes
✓A class represents some useful concept
✓Concrete entities: Bank accounts, ellipses, and products
✓Abstract concepts: Streams and windows
✓Find classes by looking for nouns in the task description
✓Define the behavior for each class
✓Find methods by looking for verbs in the task description
14
Example: Invoice
15
Example: Invoice
✓Classes that come to mind: Invoice, LineItem, and Customer
✓Good idea to keep a list of candidate classes
✓Brainstorm, simply put all ideas for classes onto the list
✓You can cross not useful ones later
16
Finding Classes
✓Keep the following points in mind:
▪ Class represents set of objects with the same behavior
–Entities with multiple occurrences in problem description are good
candidates for objects
–Find out what they have in common
–Design classes to capture commonalities
▪ Represent some entities as objects, others as primitive types
–Should we make a class Address or use a String?
▪ Not all classes can be discovered in analysis phase
▪ Some classes may already exist
17
UML Diagram for a class
✓Unified Modeling Language (UML) provides a set of standard
diagrams for graphically depicting object-oriented systems.
Class name goes here ClassName
Fields are listed here Fields
Methods
Methods are listed
here
18
UML Data Type and Parameter Notation
✓UML diagrams are language independent.
✓UML diagrams use an independent notation to show return
types, access modifiers, etc.
Access modifiers Rectangle
are denoted as:
+ public
- private - width : double
+ setWidth(w : double) : void
19
Converting the UML Diagram to Code
✓Putting all of this information together, a Java class file can be
built easily using the UML diagram.
✓The UML diagram parts match the Java class file structure.
class ClassName
{ ClassName
Fields Fields
Methods Methods
}
20
Converting the UML Diagram to Code
The structure of the class can be public class Rectangle {
compiled and tested without having private double width;
bodies for the methods. Just be sure private double length;
to put in dummy return values for
methods that have a return type public void setWidth(double w){
other than void. }
public void setLength(double len){
Rectangle }
public double getWidth(){
- width : double return 0.0;
- length : double }
public double getLength(){
+ setWidth(w : double) : void return 0.0;
+ setLength(len : double): void }
+ getWidth() : double public double getArea() {
+ getLength() : double return 0.0;
+ getArea() : double }
} 21
Constructors in UML
✓In UML, the most common way constructors are defined is:
Notice there is no
Rectangle
return type listed
- width : double for constructors.
- length : double
+Rectangle(len:double, w:double)
+ setWidth(w : double) : void
+ setLength(len : double): void
+ getWidth() : double
+ getLength() : double
+ getArea() : double
22
Relationships Between Classes
✓Association
✓Aggregation
✓Composition
✓Dependency
23
Relationships Between Classes
✓Association: More general relationship between classes
✓Use early in the design phase
✓A class is associated with another if you can navigate from
objects of one class to objects of the other
✓Given a Bank object, you can navigate to Customer objects
24
Relationships Between Classes
Aggregation
✓Has-a relationship
✓Objects of one class contain references to objects of another class
✓Use an instance variable
▪ A tire has a circle as its boundary:
class Tire
{
...
private String rating;
private Circle boundary;
}
▪ Every car has a tire (in fact, it has four)
25
Relationships Between Classes
Example
class Car
{
...
private Tire[] tires;
}
26
Relationships Between Classes
Composition
✓An aggregation relationship where the aggregated objects do
not have an existence independent of the containing object.
✓For example, composition models the relationship between a
bank and its accounts. If a bank closes, the account objects
cease to exist as well.
27
Relationships Between Classes
Dependency
✓Uses relationship
▪ Example: Many of our applications depend on the Scanner class to
read input
✓Aggregation is a stronger form of dependency
✓Use aggregation to remember another object between method
calls
28
UML Relationship Symbols
Relationship Symbol Line Style Arrow Tip
Composition Filled-in Diamond
Interface Implementation Dotted Triangle
Aggregation Solid Diamond
Dependency Dotted Open
Association Solid Line
29
Multiplicities
✓any number (zero or more): *
✓one or more: 1..*
✓zero or one: 0..1
✓exactly one: 1
30
Five-Part Development Process
✓Gather requirements
✓Find classes, responsibilities, and collaborators
✓Use UML diagrams to record class relationships
✓Use javadoc to document method behavior
✓Implement your program
31