You are on page 1of 54

Lecture 6

Modeling – Structural Models


Introduction
• Functional models represent system behavior

• Structural models represent system objects and their relationships:


• People
• Places
• Things

• Software Engineers create a conceptual model that shows the


logical organization of objects without indicating how the objects are
stored, created, or manipulated.
• We then evolve it into a model using
• Class diagrams
• Object diagrams
Structural Models
• Drawn using an iterative process
• First drawn in a conceptual, business-centric way
• Then refined in a technology-centric way describing the actual
databases and files
• More and more detail is added in each iteration

• One of the primary purposes of structural models is to


create a vocabulary for engineers & users
• Allows effective communication between analysts & users
4

Structural Models
• Structural models depict the static view of a system
• Structural models represent system objects and their relationships:
• People, things, places, etc…
• Main goal: to discover the key data contained in the problem domain
and to build a structural model of the objects

Structural
Modeling

Solution Domain
Problem Domain
5
6
7
8
9
10
11
Class/Object Identification
Different approaches can be used to identify objects:

• Textual analysis of use-case information


• Nouns suggest classes (e.g. patient, appointment, etc..)
• Verbs suggest operations (methods) (e.g. make new appointment,
cancel appointment)
• Creates a rough first cut to provide an object list

• Brainstorming—people offering ideas


• Initial list of classes (objects) is developed
• Attributes, operations and relationships to other classes can be
assigned in a second round
Object Identification (cont.)
• Common Object Lists
• Is a list of objects common to the business domain of the system.
• Physical things can be used as classes.
• Incidents can be used as methods.
• Roles can be used as classes.
• Interactions can be used as methods.

• Patterns
• Is a list of objects common to the business domain of the system.
• Useful groupings of collaborating classes that provide solutions to
common problems (are reusable)
• Developed patterns provide a starting point for work in similar
domains
Class Diagrams
• A static model that shows classes and their relationships to one
another. (classes remain constant over time)
• Elements of a class diagram:

• Classes
• Objects within the system (a person, place or thing)
• Stores and manages information in the system and contains:
• Attributes—characteristics of the class
• Operations (methods)—activities the class can perform

• Relationships—the associations between classes


• Depicted as lines between classes
• Multiplicity indicates how many of one object is/are associated
with other objects
15

Class
• Classes
•Are general templates used to create specific instances
(objects) of people, places, or things.
•Concrete classes: application domain classes. E.g. employee
class.
•Abstract classes: do not actually exist in the real world. E.g.
person class.
• Attributes
•Properties that describe the state of an instance of a class
(an object)
•Only important and primitive types (e.g. int, string, double,
date, bool) should be added.
• Operations (methods)
•Actions or functions that a class can perform.
•Only problem-domain specific methods should be
considered.
Relationships
• Denotes associations between classes
• Depicted with a line labeled with the name of the relationship
• May be directional (depicted with a triangle; e.g., a patient schedules an
appointment)
Relationships
• Classes may be related to themselves (e.g., employees and
managers who may be members of the same class)
• A (+) sign is placed before the label to indicate its role.
• E.g. a patient can be the primary insurance carrier for other patients.

• Multiplicity indicates how many of one class are related to another


class
18

Relationships
• Describes how classes relate to one another
• Three basic types in UML
 Generalization
• Enables inheritance of attributes and operations of other classes.
• Represents relationships that are “a-kind-of”, e.g. an employee is a kind of
person.
• Superclass/Subclass: subclasses can also contain attributes and operations
that are unique just to them.
 Aggregation
• Relates parts to wholes
• Represents relationships that are “a-part-of”, e.g. an engine is part of a car.
• Decomposition: the opposite of aggregation. E.g. decompose a class into
subclasses.
 Association
• Miscellaneous relationships between classes, usually a weaker form of
aggregation.
• E.g. a patient schedules an appointment.
19

Generalization & Aggregation associations


• Generalization
20

Generalization & Aggregation associations


• Aggregation
21

Generalization & Aggregation associations


• Composition
Multiplicities
Department Boss Exactly one:
A department has one
1 1 and only one boss

Employee Child Zero or more:


An employee has zero
1 0..* to many children

Boss Employee One or more:


A boss is responsible for
1 1..* one or more employees
Multiplicities
24

Attributes
• Properties of a class
• E.g. Person: last name, first name, address, etc.
• We can use derived attributes (\), i.e. attributes that have been
calculated from other attributes.

• Visibility of an attribute:
• Restricts access to attributes to ensure consistency
• Public attributes (+): visible to all classes, not hidden from other
objects.
• Private attributes (-) {default}: hidden from all other classes,
visible only to an instance of the class in which they are defined.
• Protected attributes (#): attribute hidden from all other classes
except its immediate subclasses
25

Methods
• Common methods are not shown. E.g. Create() or delete() an
instance, Return() or set() a value.
• Only operations unique to the class are included. E.g.
cancelAppointment(), calculateLastVisit().
• Can also be public protected, or private {default}.

• Types of operations (methods):


• Constructor—creates an object (instance of a class), e.g. insert() method
in patient class would create a new patient.
• Query—makes information about the state of an object available to other
objects. E.g. calculateLastVisit() determines when a patient last visited the
doctor's office.
• Update—changes values of some or all of an object’s attributes, e.g.
changeStatus() changes the status of a patient from new to current.
• Destructor—deletes or removes an object, eg. deletePatient()
26

Association Classes
• A relationship itself has associated properties.
• Common in many-to-many relationships
• Used when attributes about the relationship between two classes
needs to be recorded
• Students are related to courses; a Grade class provides an attribute to
describe this relationship
• Illnesses are related to symptoms; a Treatment class provides an attribute
to describe this relationship
27

Class Diagram
28

Object Diagram
• A good way of
simplifying a
class diagram is
through an object
diagram.
• Also good when
used to provide a
real use-case
scenario.
Library Book Collection System
Library Book Collection System
Library Book Collection System
Simplifying Class Diagrams
• Fully populated class diagrams of real-world system can
be difficult to understand

• Common ways of simplifying class diagrams:


• Show only concrete classes

• Use a view mechanism.


• The view mechanism shows a subset of classes. E.g. use-case view
that shows only the classes and relationships relevant to a particular
use case.
• Packages show aggregations of classes (or any elements in UML)
33

Class Diagram Exercise


• Consider the world of companies: Companies employ
employees (who can work for one company), and consist
of one or more departments. Each company has a single
president, who is an employee. Departments have
employees as members and run projects (one or more).
Employees can work in 1 to 3 projects, while a project can
have 2 to 50 assigned employees. You may assume that
companies have a name and address, while employees
have an emp# and a salary.
34

• Draw a class diagram:


• To buy prescription glasses, the patient needs to see an
eye doctor who will give him/her a prescription. Once
he/she has a prescription, they go to the glasses store,
where they select their frames and lenses, and then
place the order for their glasses. Once the glasses have
been made, they return to the store for a fitting and pay
for the glasses.
• Use abstract classes where ever needed.
• Include some attributes and methods that you can
imagine of.
35

Structure of a Class Definition


class name {
attributes and
declarations symbolic constants

how to create and


constructor definition(s) initialize objects

method definitions how to manipulate


the state of objects
} These parts of a class can
actually be in any order
36

Objects class
Time
inTi
hour minute Att
30
Me
void addMinutes( int m ) vo

outTime
Attributes: hour = 17 minute = 35
Methods:
void addMinutes(int m)
37

Classes and Objects


• A class is a prototype for creating
objects
• When we write a program in an
object-oriented language like Java,
we define classes, which in turn are
used to create objects
• A class has a constructor for creating
objects
38

A Simple Class, called “Time” (partial)


class Time {

private int hour, minute; constructor for Time

public void addMinutes (int m) {


int totalMinutes =
((60*hour) + minute + m) % (24*60);
if (totalMinutes<0)
totalMinutes = totalMinutes + (24*60);
hour = totalMinutes / 60;
minute = totalMinutes % 60;
}
}
39

Java Example: Represent a Time


class Time { attributes of Time


} constructor for Time

Time inToWork = new Time(8, 30);


Time outFromWork = new Time(17, 35);
2) Is able to perform actions, or methods, on
this state
• An object can also include a group of procedures/functions that
carry out actions
class Time {

private int hour, minute;

public Time (int h, int m) {


hour = h;
minute = m;
a method of Time
}
}
41

3) Communicates with other objects via


message passing
• Sends messages to objects, triggering methods in those
objects

void
42

Example of Object Creation


and Message Passing

bill
Attributes: …
Methods:

43

Example of Object Creation


and Message Passing
In one of bill’s methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);

bill
Attributes: …
Methods:

44

Example of Object Creation


and Message Passing
In one of bill’s methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);

bill
Attributes: …
Methods:

inToWork
Attributes: hour = 8 minute = 30
Methods:
void addMinutes(int m)
45

Example of Object Creation


and Message Passing
In one of bill’s methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);

inToWork.addMinutes(15)

bill
Attributes: …
Methods:

inToWork
Attributes: hour = 8 minute = 30
Methods:
void addMinutes(int m)
46

Example of Object Creation


and Message Passing
In one of bill’s methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);

bill
Attributes: …
Methods:

inToWork
Attributes: hour = 8 minute = 45
Methods:
void addMinutes(int m)

You might also like