0% found this document useful (0 votes)
24 views60 pages

Week 2

Uploaded by

devesh verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views60 pages

Week 2

Uploaded by

devesh verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Use Case Modelling

• So far, we have looked into the


basic syntax...
• Factoring use cases ...
• Text description ...
• Design of Use Case model from a
given text description ...

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.

• Use cases should start off simple and at as


much higher view as possible.
– Can be refined and detailed further.

• Use case diagrams represent functionality:


– Should focus on the "what" and not the "how".

3
Is it OK?

Too many use cases


at any level should
be avoided!

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

with compartments. display()


hide()
• Compartments for name, attributes, Methods of the class

and operations. Window


• Attribute and operation compartments
are optional ... used depending on the
purpose of a diagram.
12
UML Class Representation
• A class represents a set of objects having similar attributes,
operations, relationships and behavior.
Class Name

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

Member Name issueBook( );


Membership Number
Address
findPendingBooks( ); Example UML
findOverdueBooks( );
Phone Number
E-Mail Address
returnBook( ); Classes
findMembershipDetails( );
Membership Admission Date
the design process is the oneusing which we will have a step by step method of
Membership Expiry Date
given a problem description, we will comeup with the solution, object-oriented
Books Issued solution, and there in almost every design process,we will see that at the start of
the process, we identify only the classes and we representthem in this, at the initial
part, the analysis part.We identify only the names of the classes and as we proceed
issueBook( ); in our design process, weidentify the methods, we populate the classes with the
findPendingBooks( ); methods and therefore towards themiddle of the design process, we get this kind
findOverdueBooks( ); of representation.And subsequently in the design process later, we get this kind of
returnBook( ); representation where wehave name of the class, the attributes and the operation
findMembershipDetails( ); and it becomes feasible to implementthis kind of representation, this complete
class representation and in fact many casetools that can generate code directly
based on this diagram, for example here they willcreate classes out of this diagram
just by a click of a key, they will have the classdefinition, the attributes defined
and the method prototypes defines.
14
### Lecture on Use Case Modeling:
### Class Diagrams:
#### Importance of Use Case Modeling:
#### Class Representation:
- Use case modeling is fundamental in the object-oriented design process.
- A class is a template for object creation.
- Use case diagrams are pivotal as other diagrams are derived from them.
- Represented as a solid rectangle with three compartments:
- Core concepts include syntax, actors, and communication relationships.
name, attributes, and methods.
- Attributes and methods are optional depending on the
#### Factoring Use Cases:
purpose of the diagram.
- Necessary for complex and large use cases and for reuse across use cases.
- Implicit attributes may exist due to associations with other
- Mechanisms: inheritance, include, and extend relations.
classes.
- Text description should accompany diagrams for clarity.
#### Evolution of Representation:
#### Style Notes (Ambler, 2005):
- Initial stage: Only class names.
- Use verb form for naming use cases (e.g., "register student").
- Middle stage: Class names and methods.
- Document use cases as they are typically invoked.
- Final stage: Class names, attributes, and methods.
- Primary actor on the left, collaborating actors on the right.
- Actors associated with multiple use cases need not be redrawn.
### Conclusion:
- Use lines, not arrows, for associations.
- Use case modeling is crucial for understanding system
- Consider adding an actor like "calendar" for time-related events.
functionality.
- Class diagrams evolve from basic to detailed representations.
#### Design Guidelines:
- Follow design processes for systematic development.
- Only model computer interactions, not actor-to-actor interactions.
- Use factoring judiciously, preferably single level.
#### Thank You:
- Use user-given names for use cases, avoid technical terms.
- The lecture ends, and further topics will be covered in the next
- Keep top-level diagrams simple; split into detail diagrams as needed.
class.
- Focus on functionality, not implementation details.
Class Attribute Examples
Java Syntax UML Syntax

Date birthday Birthday:Date

Public int duration = 100 +duration:int = 100

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

Java Syntax UML Syntax


void move(int dx, int dy) ~move(int dx,int dy)

public int getSize() +int getSize()

18
Are Methods and Messages Synonyms?
• No

• Message was the original concept in object-


orientation…
• Methods are the later simplifications…

• Sometimes used as synonyms

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

• An operation can be implemented by


method overloading and we use
the methods and
operationsdifferently in this

multiple methods. situation. But, if an operation is


implemented by a single
method,then we can say that the
method and the operation are the
– Known as polymorphism same in this situation

– In the absence of polymorphism-–the


two terms are used as synonyms.

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

–Allows redefinition of the existing


methods (method overriding). UnderGrad PostGrad Research

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

UnderGrad PostGrad Research UnderGrad PostGrad Research

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.

class Person{ Employee anEmployee = new Employee();


private String name; Person
private Date dob; - name: String
- dob: Date
...
}
class Employee extends Person{
private int employeeID;
private int salary;
Employee
private Date startDate; - employeeID: int
... - salary: int
} - startDate: Date

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

Object height 4.0


Reference

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

Tiger Lion Puma

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

Discriminator: allows one to group subclasses into


clusters that correspond to a semantic category.

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

Person employee employer Company


works for

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

A keyopens up to 5 doors. A single key opens up to 5 doors and A


dooris opened by many keys.

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.

Student credits hasEnrolmentOf Course


10..300 Enrols in 1..5

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

Node6 Node7 Node8

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

Please try writing the Java code for


to implement this association
between A person class and a
company class.
An advertiser has one account and n account is
associated with a single advertiser.

You might also like