Week 2
Week 2
1
• Use case name should begin with a verb. Style Notes
• While use cases do not explicitly imply timing: (Ambler, 2005)
– Order use cases from top to bottom to imply timing -- it improves
readability.
• The primary actors should appear in the left.
• Actors are associated with one or more use cases.
• Do not use arrows on the actor-use case relationship.
• To initiate scheduled events include an actor called
“calendar”
• Do not show actors interacting with each other.
• <<include>> and <<extend>> should rarely nest more
than 2 levels deep.
2
Effective Use Case
• Use cases should be named and organized
Modelling
from the perspective of the users.
3
Is it OK?
4
Use Case Packaging
Accounts
Print
Query balance
Balance sheet
Receive Make
grant payments
5
Which is more acceptable?
6
• HAS will be used by an instructor to: Quiz: Home Assignment
– Distribute homework assignments, System - Use Case Model
– Review students’ solutions,
– Distribute suggested solution,
– Assign a grade to each assignment.
• Students can:
– Download assignments
– Submit assignment solutions
• System:
– Automatically reminds the students a day before
an assignment is due.
7
Remind Quiz: Solution 1
Student
(Inferior)
Calendar Distribute Get
Assignments Assignment
Submit
<<Extends>>
Post Solutions Assignment
Student
Distribute Get
Grade Solution
Instructor
Get Grade
8
Quiz: Alternate
Distribute
Assignments (Better) Solution
Post Solutions
Student
Distribute
Grade
Instructor
Submit
Assignment
Remind
Student
Calendar
9
Class Diagram
10
Template for object creation: Class: A
Fundamental
− Instantiated into objects Object-Oriented
Concept
Examples: Employees, Books, etc.
Sometimes not intended to produce instances:
− Abstract classes
see if you can answer this question that if we definea abstract class, what is the use of it because we do not
create an abstract class, we cannotcreate any objects from an abstract class, the answer to this question is
that yes, wecannot instantiate an abstract class but then, it helps us to reuse the definitions providedin abstract
class because we can define concrete classes based on the abstract class whichwill reuse the functionality
defined once in the abstract class and we can derive severalconcrete classes from abstract class.We define
the functionality only once in the abstract class and reuse this in the concreteclasses.
11
• Entities with common features Class Window
are made into a class. Diagram Name of the class
size: Size
• Represented as solid outline rectangle visibility: boolean
Attribute of the class
EL
Window
A class can
PT
size: Size implicitly
Attributes have a few
visibility: boolean association
attributes
Operations
N
display()
hide()
13
Different representations of the LibraryMember class
LibraryMember LibraryMember LibraryMember
Private Student
-Students[0..MAX_Size]:Student
students[0..MAX_Size]
15
Visibilty Java Syntax UML Syntax Visibility
Syntax in
public public + UML
protected protected #
package ~
private private -
16
Methods are the operations supported by Methods
an object: vs.
Messages
− Means for manipulating the data of an object.
EL
− Invoked by sending a message (method call).
PT
− Examples:calculate_salary(), issue-book(),
N
getMemberDetails(), etc.
17
Method Examples
18
Are Methods and Messages Synonyms?
• No
19
Are Methods and Operations Synonyms?
• No . If an operation of a class is
implemented by multiple methods
we say that,this is a case for
20
What are the Different
• Four types: Types of Relationships
Among Classes?
– Inheritance
– Association
– Aggregation/Composition
– Dependency
21
• Allows to define a new class (derived Inheritance
class) by extending an existing class
(base class).
LibraryMember Base Class
–Represents generalization-
specialization relation. Faculty Students Staff
Derived
Classes
22
Inheritance One More Example
Library “A Student ISA Library Member”
Member
“A Faculty ISA Library Member”
Stdent Faculty
23
Inheritance: Semantics
• Lets a subclass LibraryMember Base Class
inherit
attributes and
Derived
methods from Faculty Students Staff
Classes
a base class.
UnderGrad PostGrad Research
24
LibraryMember Base Class LibraryMember Base Class
Multiple
Inheritance
Derived
Faculty Students Staff Faculty Students Staff
Classes
Multiple
Inheritance
25
Inheritance Implementation in Java
Inheritance is declared using the "extends" keyword
Even when no inheritance defined, the class implicitly extends a class called Object.
26
Rectangle myRectangle = new Rectangle(5, 3); Objects
Box myBox = new Box(6, 5, 4); myRectangle
and myBox
length 6.0 Rectangle
length 5.0
myRectangle myBox width 5.0 Box
width 3.0
27
Lecture on Class Diagrams and Class Relations:
Class Representation and Visibility:
Classes can be represented in three notations: name only, name with operations, and detailed representation with attributes and operations.
Visibility of attributes and operations is important and represented differently in UML.
Public: +, Private: -, Protected: #, Package wide: ~.
Methods and Messages:
Methods manipulate object data and are invoked by sending messages.
Methods and messages are often used interchangeably but originally had distinct purposes.
Messages were for loose coupling, methods for synchronous invocation.
Methods vs. Operations:
Methods are implementations of operations.
In method overloading, multiple methods implement the same operation.
Methods and operations are synonyms if a single method implements a single operation.
Class Relations:
Four types: inheritance, association, aggregation/composition, dependency.
Inheritance: Base class deriving new classes, representing IS A relationship.
Semantics: Derived class inherits attributes and methods of base class.
Multiple inheritance can lead to programming complications; handled differently in C++ and Java.
Implementation in Java:
In Java, inheritance is implemented using the extends keyword.
Instantiation and object references are crucial; objects are accessed through references.
Conclusion:
Understanding class relations is essential for application development.
Inheritance is a powerful mechanism for code reuse and hierarchy creation.
Java provides straightforward implementation of inheritance with the extends keyword.
More
Mammal Quadruped Generalization
Examples…
Feline Lizard
28
Any problems?
Does it appear correct that we have car as
the base class and chassis, engineand door
are the derived classes. Do you find it
Wrong Generalization Car acceptable relation or is there any problem?
--- attributes
Okay, it is not really correct because we had
said that whenever there is a
violates “is a” or “is a generalization,we should be able to express
operations
kind of” heuristic this in the form of a IS A relation. We should
be able to saychassis is a car, engine is a car
and door is a car which is looks odd, it’s not
correct. So,it’s not correct, it’s a wrong
generalization. It violates the IS A or IS A kind
Chassis Engine Door of heuristicthat we had mentioned earlier.
Whenever we want to test inheritance
attributes attributes attributes hierarchy that,whether it is okay we can
operations operations operations express that using the IS A heuristic to find
that whether it’sa correct generalization. This
is not correct, the classes here havedifferent
relation. The car is an aggregate of chassis,
engine and door car consists ofactually. A car
consists of chassis, engine and door, it is not
inheritance relation.
29
Library Inheritance Example
Book
issuable reference
Issuable Reference
Issuable Reference
Single Volume Single Volume
BookSet BookSet
Book Book
30
Inheritance Pitfalls
• Inheritance certainly promotes reuse.
• Indiscriminate use can result in poor
quality programs.
• Base class attributes and methods
visible in derived class…
– Leads to tight coupling
31
• How implemented in program? Association
Relationship
• Enables objects to communicate with each other:
–One object must “know” the ID of the
corresponding object in the association.
• Usually binary:
–But in general can be n-ary.
32
Association – An Example
• In a home theatre system,
– A TV object is associated with a VCR object
• It may receive a signal from the VCR
– VCR may be associated with remote
• It may receive a command to record
1 1 1 1
Remote commands VCR Connected to TV
33
tax_file 1-1 Association
Rakesh Shukla 760901-1234
– example
V. Ramesh
691205-5678
Keshab Parhi
People Tax_files
1 1
People Associated Tax_files
with
34
motherOf
Multiple
motherOf
Bhim Association –
Kunti Yudhistir
example
motherOf
Arjun
Woman Person
1 *
Woman Mother of Person
35
role B Association
Class A role A Class B
UML Syntax
• A Person works for a Company.
Role
Association Name
36
Multiplicity: The number of objects Association -
from one class that relate with a single More Examples
object in an associated class.
Library Member
1 borrowed by 0..5 Book
eats
* * Human
Lion
So, we read a lion eats many humans and on theother direction, we read a human is eaten by many lions. Let me
read that again. A lioneats and this the multiplicity. A Lion eats many humans and here on this side we read a
human iseaten by many lions.
37
Navigability
opens 0..5
Key * Door
38
Question
• A teacher teaches 1 to 3 courses (subjects) Association
•
teacher is associated with courses
Each course is taught by only one teacher. – Quiz 1
• A student can take between 1 to 5 courses.
• A course can have 10 to 300 students. Draw the class diagram.
1 teaches 1..3
Teacher Course
1..5
Students
10..300
39
• A Student can take up to five Courses. Quiz 2: Draw
• A student needs to enroll in at least one Class Diagram
course.
• Up to 300 students can enroll in a course.
• An offered subject in a semester should have
at least 10 registered students.
40
Student credits
10..300
hasEnrolmentOf
Enrols in 1..5
Course
A Identify as
Correct or
Wrong
Student credits
10..300
hasEnrolmentOf
Enrols in 1..5
Course B
Student credits
10..300
hasEnrolmentOf
Enrols in 1..5
Course C
Student credits
10..300
hasEnrolmentOf
Enrols in 1..5
Course D
41
Quiz: Read the Diagram
1..2 teaches 0..3
Teacher Course
opens 0..5
* Door
Key
42
• A link: Association
one object of a class is associated with how many elements of the other class that we call as link.
– An instance of an association and Link
– Exists between two or more objects
– Dynamically created and destroyed as
the run of a system proceeds
• For example:
– An employee joins an organization.
– Leaves that organization and joins a new
organization.
43
• A class can be associated with itself Unary
a single class is associated with itself and this we call as a unary association.
(unary association). Association
–Give an example?
• An arrowhead used along with name:
–Indicates direction of association.
• Multiplicity (association cardinality)
indicates # of instances taking part in the
association.
44
Uniary Association: Example 1
A person is a friend of another person, A person is
friend of many persons this isthe star is many, A single
person is a friend of many persons. We can read in the
other direction,a person has many persons as his friend
Supervises
Boss 1
Person *Employee the friend has other objects in this the link
gets formed with the other objectsof the
person class. So a person A might have friends
B, C, D3 friends person A has friends have
friends B, C, D and so on.
45
Self Association:
A computer connects to many other computersor A computer is
connected to many other computers. So that is indicated by the
star here which is many Example 2
Computer Network
Connects to
Computer *
46
Computer
Node1 Node2 Node3 Network:
Object
Node4
Diagram
Node5
47
Self Association:
Connected to Example 3
A node in a link list is connected to the
next node. We can read it in the other
directionA node in the link list is
connected to the next nodeor we can
next read it as saying A node, A link list node
is connected to the previous node
LinkedListNode
previous
Here the association is defined on a single classthat is only the objects of this class participate in the
association relationthe links exists between different nodes of the same class, different objects of the
same class.
48
Reflexive
Course Association:
* Example 4
*
has pre-requisite of
49
we need to store the bookreference on the member side and also
the member reference on the book side.
this is the code for the member we have the bookas attribute and
whenever a book is issued the reference of the book is applied to
thisissue book with book reference and the book reference is
stored here by calling the setbook. In the set book we set it to
abook and also during the issue we write abook dot setlender this
and this forms the link on the other side
Book class we have member as attribute and when the set lender iscalled then the
lender reference is stored in the book class as an attribute. So thatforms the
bidirectional association link gets formed between member and book and the
bookand member.
For example, on the person class we will have employer as the nameof the attribute and we will set the
specific company on the employer attribute and on thecompany side we will have employee as the
implicit attribute and for the specific person we willset the employee here. But we had mentioned that the
roles here are optional sometimes we may nothave the roles specified in that case we use the name of the
class as the name of the attribute.
Here, observe the navigation is bidirectional that same as just a straight line we has drawn arrow heads on both sides that is equivalent to just
straight line. Now on the student side,student object can enroll in up to 5 courses, 1 to 5 courses. So we write here an array of 5. On the
course side a course can have enrollment of 1 to 300 students. So we write a course has 300 students up to 300 students crediting the course.
A company has one person working for it and A
person works for one company