UML

advertisement
Programming With Java
ICS201
Chapter 12
UML and Patterns
University Of Hail
1
Programming With Java
ICS201
Introduction to UML
o UML
(Unified
Modeling
Language)
is
a
graphical
language used for designing and documenting OOP
software.
o UML is a software design tools that can be used within
the context of any OOP (Object-Oriented programming)
language.
University Of Hail
2
Programming With Java
ICS201
UML
o Pseudocode is a way of representing a program in a
linear and algebraic manner.
 It simplifies design by eliminating the details of
programming language syntax.
o Graphical representation systems for program design
have also been used:
 Flowcharts and structure diagrams for example.
o Unified
Modeling
Language
(UML)
is
yet
another
graphical representation formalism.
 UML is designed to reflect and be used with the OOP
philosophy.
University Of Hail
3
Programming With Java
ICS201
Types of UML Diagrams
• Use Case Diagram
• Class Diagram
• Sequence Diagram
• Collaboration Diagram
• State Diagram
• ….
Programming With Java
ICS201
Classes
•A class is simply represented as a box with the name of the class inside
– The diagram may also show the attributes and operations
– The complete signature of an operation is:
operationName(parameterName: parameterType …): returnType
Rectangle
Rectangle
getArea
resize
Rectangle
height
width
Rectangle
Rectangle
height
width
height: int
width: int
getArea
resize
getArea(): int
resize(int,int)
5
Programming With Java
ICS201
UML Class Diagram
o A class diagram is divided up into three sections:
1.The top section contains the class name.
2.The middle section contains the data specification
for the class (attributes).
3.The bottom section contains the actions or methods
of the class (operations).
ClassName
attributes
operations
University Of Hail
6
Programming With Java
ICS201
Class Names
ClassName
attributes
operations
The name of the class is the only
required
tag
in
the
graphical
representation of a class.
It always
appears in the top-most compartment.
Programming With Java
ICS201
Class Attributes (Cont’d)
Attributes are usually listed in the form:
attributeName : Type
Person
+ name
# address
# birthdate
/ age
- ssn
: String
: Address
: Date
: Date
: Id
Attributes can be:
+ public
# protected
- private
/ derived
A derived attribute is one that can be
computed from other attributes. For
example, a Person’s age can be
computed from his birth date.
/ age : Date
Software Design (UML)
Programming With Java
ICS201
Class Operations
Operations describe the class behavior
Person
name
address
birthdate
ssn
: String
: Address
: Date
: Id
eat
sleep
work
play
and appear in the third compartment.
o Each method in a UML diagram is
indicated
by
the
name
of
the
method, followed by its parameter
list, a colon (:), and its returned
type.
Software Design (UML)
Programming With Java
ICS201
UML Class Diagram
o A class diagram need not give a complete description of
the class.
o If a given analysis does not require that all the class
members be represented, then those members are
not listed in the class diagram.
o Missing members are indicated with an ellipsis
(three dots).
University Of Hail
10
Programming With Java
ICS201
Example (UML Class Diagram)
Square
attributes
- Side : double
- xCoordinate : double
- yCoordinate : double
Class name
+ resize (double newSide) : void
+ move (double newx , double newy ) : void
# erase ( ) : void
…
University Of Hail
Class methods
11
Programming With Java
ICS201
Inheritance Diagrams (Generalization)
o An inheritance diagram shows the relationship between
a base class and its derived class(es).
 Normally, only as much of the class diagram is
shown as is needed.
 Note that each derived class may serve as the base
class of its derived class(es).
o Each base class is drawn above its derived class(es)
 An upward pointing arrow is drawn between them to
indicate the inheritance relationship.
University Of Hail
12
Programming With Java
ICS201
Inheritance Diagrams
o The arrows also help in locating method definitions.
o To look for a method definition for a class:
 Examine the class definition first.
 If the method is not found, the path of connecting
arrows will show the order and direction in which to
search.
 Examine the parent
connecting arrow.
class
indicated
by
the
 If the method is still not found, then examine this
parent's parent class indicated by the connecting
arrow.
 Continue until the method is found, or until the top
base class is reached.
University Of Hail
13
Programming With Java
ICS201
A Class Hierarchy in UML Notation
Person
Arrows go from a derived
class to its base class.
Student
Undergraduate
Employee
Graduate
Faculty
University Of Hail
Staff
14
Programming With Java
ICS201
Some Details of a Class Hierarchy
Person
- name: String
+ setName(String newName): void
+ getName( ): String
+ toString( ): String
+ sameName(Person otherPerson): boolean
Student
- studentNumber: int
+ set(String newName , int newStudentNumber): void
+ getStudentNumber( ): int
+ setStudentNumber(int newStudentNumber ): void
+ toString( ): String
+ equals(Object otherObject): boolean
University Of Hail
15
Programming With Java
ICS201
Example
• Consider the class named Book that contains:
–
–
–
–
–
A private String variable called title;
A private integer variable called nbpages;
A public double variable called price ;
Two public methods SetTitle() and SetNbpages();
Two public methods getTitle() and getNbpages();
• Class TextBook is a derived class from the class Book. It
contains:
– A protected integer variable called gradeLevel;
– A public method toString() that returns a description of a
textBook;
– A public method PriceAfterDiscount() that returns the price of a
book after applying a discount of 10%.
• Question: Establish the UML diagram for this problem.
Programming With Java
ICS201
+
+
+
+
+
Book
title : String
nbpages : int
price : double
SetTitle (String t) : void
SetNbpages (int nb) : void
getTitle ( ) : String
getNbpages ( ) : int
TextBook
# gradeLevel : int
+ toString ( ) : String
+ PriceAfterDiscount() : double
Download