You are on page 1of 71

Software Design

System Modeling

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Software Design 1/7


System Modeling

System modeling is the process of developing abstract


models of a system

Each model presents a different view or perspective of that


system

Uses some kind of graphical notation

Primarily based on notations in the Unified Modeling


Language (UML)

CS253: Software Development and Operations Software Design 2/7


Use of Models

Models help explain the proposed requirements to other


system stakeholders

Engineers use these models to discuss design proposals


and to document the system for implementation

In a model-driven engineering process, it is possible to


generate a complete or partial system implementation from
the system model

CS253: Software Development and Operations Software Design 3/7


Model vs. Representation

A model is an abstraction of the system being studied


rather than an alternative representation of that system

A representation of a system should maintain all the


information about the entity being represented

An abstraction deliberately simplifies and picks out the


most salient characteristics

Example: Book Review vs translation

CS253: Software Development and Operations Software Design 4/7


Static Vs. Dynamic Models

Static models: Show the structure of the system design

Dynamic models: Show the organization of the system


when it is executing

The dynamic organization of a system as a set of


interacting threads may be very different from a static
model of the system components

CS253: Software Development and Operations Software Design 5/7


Popular UML Diagrams

Use case diagrams: Shows the interactions between a


system and its environment
- Used as part of requirement engineering

Class diagrams: Shows the object classes in the system


and the associations between these classes
- used as static model

Sequence diagrams: Shows interactions between actors


and the system and between system components
- used as dynamic model

State diagrams: Shows how the system reacts to internal


and external events
- used as dynamic model

CS253: Software Development and Operations Software Design 6/7


Software Design
System Modeling

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Software Design 7/7


Software Design
Class Diagram

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Software Design 1/9


Objects

Objects represent something in the real world, such as a


patient, a prescription, a doctor, etc.

May need to define additional implementation objects that


are used to provide the required system functionality

CS253: Software Development and Operations Software Design 2/9


Class Diagrams

Class diagrams are used when developing an


object-oriented system model

Shows the classes in a system and the associations


between these classes

An object class can be thought of as a general definition of


one kind of system object

An association is a link between classes that indicates that


there is a relationship between these classes

Each class may have to have some knowledge of its


associated class

CS253: Software Development and Operations Software Design 3/9


Classes and Association

Look at the world, identify the essential objects, and


represent these as classes

Note the existence of an association

Name associations to give the reader an indication of the


type of relationship that exists

Show how many objects are involved in the association

CS253: Software Development and Operations Software Design 4/9


Example: Patient Class Diagram for MHC-PMS

CS253: Software Development and Operations Software Design 5/9


Example: Classes and Association in MHC-PMS

CS253: Software Development and Operations Software Design 6/9


Class Details

Attributes: The characteristics of an object

Operations: The things that you can request from an


object

CS253: Software Development and Operations Software Design 7/9


Example: The Consultation Class in MHC-PMS

CS253: Software Development and Operations Software Design 8/9


Software Design
Class Diagram

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Software Design 9/9


Software Design
Generalization and Aggregation

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Software Design 1/7


Generalization

A specific type of association between a base (parent)


class and subclass (child)

Shown as an arrowhead pointing up to the more general


class

Common information maintained in one place only

Good design practice – if changes are proposed, then you


do not have to look at all classes in the system to see if
they are affected by the change

CS253: Software Development and Operations Software Design 2/7


Example: The Generalization Hierarchy in MHC-PMS

CS253: Software Development and Operations Software Design 3/7


Generalization Details

The attributes and operations associated with higher-level


classes are also associated with the lower-level classes

The lower-level classes are subclasses inherit the


attributes and operations from their superclasses

These lower-level classes then add more specific attributes


and operations

CS253: Software Development and Operations Software Design 4/7


Aggregation

A special type of association between classes

Means that one object (the whole) is composed of other


objects (the parts)

Uses a diamond shape next to the class that represents


the whole

CS253: Software Development and Operations Software Design 5/7


Example: Aggregation Association in MHC-PMS

CS253: Software Development and Operations Software Design 6/7


Software Design
Generalization and Aggregation

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Software Design 7/7


System Modeling
Sequence and Activity Diagram

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations System Modeling 1/8


Dynamic models

Describe the dynamic structure of the system and show


the interactions between the system objects.
Interactions that may be documented include
the sequence of service requests made by objects
(Sequence models)
the state changes that are triggered by these object
interactions (state machine models)

CS253: Software Development and Operations System Modeling 2/8


Sequence Diagram

Primarily used to model the interactions between the


actors and the objects in a system and the interactions
between the objects themselves

Shows the sequence of interactions that take place during


a particular use case or use case instance

CS253: Software Development and Operations System Modeling 3/8


Example: Patient Information Sequence Diagram for
MHC-PMS

CS253: Software Development and Operations System Modeling 4/8


Activity Diagram

A way of illustrating the processing steps in a system

Track how data associated with a particular process moves


through the system Data-flow diagrams are simple and
intuitive

CS253: Software Development and Operations System Modeling 5/8


Example: Activity model of the Insulin Pump’s
Operation

CS253: Software Development and Operations System Modeling 6/8


Example: Sequence model of an Order Processing

CS253: Software Development and Operations System Modeling 7/8


System Modeling
Sequence and Activity Diagram

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations System Modeling 8/8


System Modeling
Finite State Machine

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations System Modeling 1/11


State Machine Models

Summarize the behavior of an object or a subsystem in


response to messages and events

Shows how the object instance changes state depending


on the messages that it receives

Usually need a state diagram for the complex objects in the


system

CS253: Software Development and Operations System Modeling 2/11


Finite State Machine
State Machine: Model of a system with discrete dynamics

Finite-State Machine (FSM): A state machine where the set


States of possible states is finite

State

Initial State

Transition

Self transition

Guard and Action

CS253: Software Development and Operations System Modeling 3/11


Example: FSM for Thermostat

Chattering: the heater would turn on and off rapidly when the
temperature is close to the set-point temperature
Solution: hysteresis strategy, dwell time

Event-triggered vs. time-triggered transitions

CS253: Software Development and Operations System Modeling 4/11


Mathematical Notation of a State Machine

A finite state machine is represented as a five-tuple

(States, Inputs, Outputs, update, initialState)

where,
States is a finite set of states
Inputs is a set of input valuations
Outputs is a set of output valuations
update : States × Inputs → States × Outputs is an update
function, mapping a state and an input valuation to a next
state and an output valuation
initialState is the initial state

CS253: Software Development and Operations System Modeling 5/11


Example: Garage Counter

States = {0, 1, . . . , M}
Inputs = ({up, down} → {present, absent})
Outputs = ({count} → {0, 1, . . . , M, absent})
initialState = 0
Update function:
( (s + 1, s + 1) if s < M ∧ up ∧ ¬down
update(s, i) = (s − 1, s − 1) if s > 0 ∧ ¬up ∧ down
(s, absent) otherwise

CS253: Software Development and Operations System Modeling 6/11


Mealy Machines and Moore Machines

Mealy Machine: Produces outputs when a transition is taken

Moore Machine: Produces outputs when the machine is in a


state

CS253: Software Development and Operations System Modeling 7/11


Extended State Machine
Augments the FSM Model with variables
Variables may be read and written while taking a transition

The number of states. can be quite large, or even infinite


n discrete states, m variables each of which can have one of p
possible values
| States |= npm
CS253: Software Development and Operations System Modeling 8/11
Example: ESM for Traffic Light

CS253: Software Development and Operations System Modeling 9/11


Reachable States

All states that can be reached from the initial state on some
input sequence

May be smaller than the set of states

Traffic Light:
Total number of states = 244
Total number of reachable states = 189

CS253: Software Development and Operations System Modeling 10/11


System Modeling
Finite State Machine

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations System Modeling 11/11


System Modeling
State Diagram

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations System Modeling 1/6


Example: State Diagram of a Microwave Oven

CS253: Software Development and Operations System Modeling 2/6


Example: State Diagram of a Microwave Oven

CS253: Software Development and Operations System Modeling 3/6


Example: State Diagram of a Microwave Oven

CS253: Software Development and Operations System Modeling 4/6


Example: State Diagram of the Operation State in the
Microwave Oven

CS253: Software Development and Operations System Modeling 5/6


System Modeling
State Diagram

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations System Modeling 6/6


Object-Oriented Programming Using C++
Friend Function

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Object-Oriented Programming Using C++ 1/6
Friend Function

A friend function of a class is defined outside that class’ scope but it has
the right to access all private and protected members of the class
Even though the prototypes for friend functions appear in the class
definition, friends are not member functions

A friend can be a class, in which case the entire class and all of its
members are friends

To declare a function as a friend of a class, precede the function


prototype in the class definition with keyword friend

CS253: Software Development and Operations Object-Oriented Programming Using C++ 2/6
Friend Function

Friend frunction for the Box class


class Box {
double width;

public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};

Friend an entire class


friend class ClassTwo;

CS253: Software Development and Operations Object-Oriented Programming Using C++ 3/6
Example: Friend Function
Friend function for the Box class
class Box {
double width;

public:
friend void printWidth( Box box );
void setWidth( double wid );
};

// Member function definition


void Box::setWidth( double wid ) {
width = wid;
}

// Note: printWidth() is not a member function of any class.


void printWidth( Box box ) {
/* Because printWidth() is a friend of Box, it can
directly access any member of this class */
cout << "Width of box : " << box.width <<endl;
}

// Main function for the program


int main() {
Box box;
// set box width without member function
box.setWidth(10.0);
// Use friend function to print the width
printWidth( box );
return 0;
}

CS253: Software Development and Operations Object-Oriented Programming Using C++ 4/6
Example: Friend Class

Friend class LinkedList of the Node class


class Node {
private:
int key;
Node* next;
/* Other members of Node Class */

// Now class LinkedList can access private members of Node


friend class LinkedList;
};

Friend function of the Node class from the LinkedList class


class Node {
private:
int key;
Node* next;
/* Other members of Node Class */

// Only search() of linkedList can access internal members


friend int LinkedList::search();
};

CS253: Software Development and Operations Object-Oriented Programming Using C++ 5/6
Object-Oriented Programming Using C++
Friend Function

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Object-Oriented Programming Using C++ 6/6
Object-Oriented Programming Using C++
This Pointer and Pointer to a Class

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Object-Oriented Programming Using C++ 1/9
This Pointer

Every object in C++ has access to its own address through an


important pointer called this pointer

The this pointer is an implicit parameter to all member functions


Inside a member function, this may be used to refer to the invoking
object

Friend functions do not have a this pointer, because friends are not
members of a class

CS253: Software Development and Operations Object-Oriented Programming Using C++ 2/9
Example 1: This Pointer

This pointer for a simple class


class Test {
private:
int x;
public:
void setX (int x)
{
// The ’this’ pointer is used to retrieve the object’s x
// hidden by the local variable ’x’
this->x = x;
}
void print()
{
cout << "x = " << x << endl;
}
};

int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}

CS253: Software Development and Operations Object-Oriented Programming Using C++ 3/9
Example 2: This Pointer
This pointer for the Box class
class Box {
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box

public:
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h;
}
double Volume() {
return length * breadth * height;
}
int compare(Box box) {
return this->Volume() > box.Volume();
}
};

int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2)) {
cout << "Box2 is smaller than Box1" <<endl;
} else {
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}

CS253: Software Development and Operations Object-Oriented Programming Using C++ 4/9
Example 2: This Pointer

Output
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1

CS253: Software Development and Operations Object-Oriented Programming Using C++ 5/9
Pointer to a Class

A pointer to a C++ class is done exactly the same way as a pointer to a


structure
To access members of a pointer to a class you use the member access
operator ->, just as you do with pointers to structures

As with all pointers, you must initialize the pointer before using it

CS253: Software Development and Operations Object-Oriented Programming Using C++ 6/9
Example: Pointer to a Class
Pointer to the Box class
class Box {
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h;
}
double Volume() {
return length * breadth * height;
}
};

int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
Box *ptrBox; // Declare pointer to a class.

ptrBox = &Box1; // Save the address of first object


// Now try to access a member using member access operator
cout << "Volume of Box1: " << ptrBox->Volume() << endl;

ptrBox = &Box2; // Save the address of second object


// Now try to access a member using member access operator
cout << "Volume of Box2: " << ptrBox->Volume() << endl;

return 0;
}
CS253: Software Development and Operations Object-Oriented Programming Using C++ 7/9
Example: Pointer to a Class

Output
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 102

CS253: Software Development and Operations Object-Oriented Programming Using C++ 8/9
Object-Oriented Programming Using C++
This Pointer and Pointer to a Class

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Object-Oriented Programming Using C++ 9/9
Object-Oriented Programming Using C++
Static Class Members

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Object-Oriented Programming Using C++ 1/8
Static Member

We can define class members static using static keyword


When we declare a member of a class as static it means no matter how
many objects of the class are created, there is only one copy of the
static member

A static member is shared by all objects of the class

All static data is initialized to zero when the first object is created, if no
other initialization is present
We can’t put it in the class definition but it can be initialized outside the
class by redeclaring the static variable, using the scope resolution
operator :: to identify which class it belongs to

CS253: Software Development and Operations Object-Oriented Programming Using C++ 2/8
Example: Static Member
Destructor for the Line class
class Box {
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box

public:
static int objectCount;

// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h;
// Increase every time object is created
objectCount++;
}
double Volume() {
return length * breadth * height;
}
};

// Initialize static member of class Box


int Box::objectCount = 0;

int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects.
cout << "Total objects: " << Box::objectCount << endl;
return 0;
}
CS253: Software Development and Operations Object-Oriented Programming Using C++ 3/8
Example: Static Member

Output
Constructor called.
Constructor called.
Total objects: 2

CS253: Software Development and Operations Object-Oriented Programming Using C++ 4/8
Static Member Function

By declaring a function member as static, you make it independent of


any particular object of the class

The static functions are accessed using only the class name and the
scope resolution operator ::.

Can be called even if no objects of the class exist


Can only access static data member, other static member functions and
any other functions from outside the class

Does not have access to the this pointer of the class

You could use a static member function to determine whether some


objects of the class have been created or not

CS253: Software Development and Operations Object-Oriented Programming Using C++ 5/8
Example: Static Member Function
Static Member Function for the Box class
class Box {
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
static int objectCount;
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l; breadth = b; height = h;
objectCount++;
}
double Volume() {
return length * breadth * height;
}
static int getCount() {
return objectCount;
}
};

int Box::objectCount = 0;

int main(void) {
cout << "Inital Stage Count: " << Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects after creating object.
cout << "Final Stage Count: " << Box::getCount() << endl;
return 0;
}
CS253: Software Development and Operations Object-Oriented Programming Using C++ 6/8
Example: Static Member Function

Output
Initial Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2

CS253: Software Development and Operations Object-Oriented Programming Using C++ 7/8
Object-Oriented Programming Using C++
Static Class Members

Indranil Saha

Department of Computer Science and Engineering


Indian Institute of Technology Kanpur

CS253: Software Development and Operations Object-Oriented Programming Using C++ 8/8

You might also like