Professional Documents
Culture Documents
OBJECTIVES
In this chapter you will learn:
• Some basics of object technology.
• UML—the industry-standard object-oriented system modeling
language.
• UML: ATM Requirements Document
• UML Use Case Diagrams
• UML Class Diagrams
UML: ATM Case Study • UML Class Attributes
• UML Objects’ States and Activities
• UML Class Operations
• UML Collaboration
• Class Programming
• Inheritance and Polymorphism
06/06/52 Dr.Mud-Armeen Munlin 06/06/52 Dr.Mud-Armeen Munlin
3 4
Outline
1.19 Software Engineering Case Study: Introduction to Object
1.19 Introduction to Object Technology
Technology and the UML and the UML
3.10 Software Engineering Case Study: Examining the ATM
Requirements Document
4.12 Software Engineering Case Study: Identifying the Classes in • Humans think in terms of objects such as telephones,
the ATM Requirements Document
5.14 Software Engineering Case Study: Identifying Class houses, traffic lights, and microwave ovens.
Attributes in the ATM System
6.10 Software Engineering Case Study: Identifying Objects’ States • Objects have attributes (e.g., size, shape, color and
and Activities in the ATM System
weight), and exhibit behaviors (e.g., a ball rolls,
7.15 Software Engineering Case Study: Identifying Class
Operations in the ATM System bounces, inflates and deflates; a baby cries, sleeps,
8.14 Software Engineering Case Study: Collaboration Among crawls, walks and blinks).
Objects in the ATM System
10.22 Software Engineering Case Study: Starting to Program the
Classes of the ATM System
12.9 Software Engineering Case Study: Incorporating Inheritance
and Polymorphism into the ATM System
• Object-oriented design (OOD) models software in • OOD encapsulates attributes and operations into
terms similar to those used to describe real-world objects.
objects. • Objects have the property of information hiding.
• New classes of objects can inherit characteristics of – Objects may communicate with one another across well-defined
existing classes and add unique characteristics of their interfaces, but implementation details are hidden within the
objects themselves.
own.
– Similarly, you can drive a car without knowing the details of how
• OOD also models communication between objects, it works internally.
just as people send messages to one another.
7 8
• Languages like C# are object oriented. • Classes are to objects as blueprints are to houses—a
– Programming in such a language is called object-oriented class is a “plan” for building objects of the class.
programming (OOP).
• Just as we can build many houses from one blueprint,
– In C#, the unit of programming is the class from which objects
are eventually instantiated (an OOP term for “created”).
we can instantiate (create) many objects from one
– C# classes contain methods that implement operations, and data
class.
that implements attributes. • The data components of a class are called attributes or
fields.
• The operation components of a class are called
methods.
06/06/52 Dr.Mud-Armeen Munlin 06/06/52 Dr.Mud-Armeen Munlin
9 10
• Packaging software as classes makes it possible for • In the 1980s, many organizations began using OOP to build their
future software systems to reuse the classes. applications.
• In 1994, James Rumbaugh and Grady Booch began working
• Groups of related classes often are packaged as to unify several processes for representing OOP.
reusable components.
• The Object Management GroupTM (OMGTM ) invited submissions
Software Engineering Observation 1.1 for a common modeling language.
Reuse of existing classes when building new classes and programs • Several companies formed the UML Partners—a consortium that
saves time, money and effort. Reuse also helps programmers build developed the UML version 1.1.
more reliable and effective systems, because existing classes and • The OMG accepted the proposal and, in 1997, assumed
components often have gone through extensive testing, debugging responsibility for the continuing maintenance and revision
and performance tuning. of the UML.
06/06/52 Dr.Mud-Armeen Munlin 06/06/52 Dr.Mud-Armeen Munlin
11 12
3.10 Examining the ATM Requirements 3.10 Examining the ATM Requirements
Document (Cont.) Document (Cont.)
• The ATM should have the following functionality. • The main menu (Fig. 3.29) displays a numbered option for
– The screen prompts the user to enter an account number. each of the three types of transactions, and the option to
– The user enters a five-digit account number, using the keypad.
exit the system.
– For authentication purposes, the screen prompts the user to enter
the PIN.
– The user enters a five-digit PIN, using the keypad.
– If the user enters a valid account number and the correct PIN for
that account, the screen displays the main menu.
15 16
3.10 Examining the ATM Requirements 3.10 Examining the ATM Requirements
Document (Cont.) Document (Cont.)
• The user enters 2 to make a withdrawal (Fig. 3.30). • The user enters 3 to make a deposit:
• If the user enters 1, the screen displays the user’s account balance from
the bank’s database. 1. The screen prompts the user to enter a deposit
amount.
2. The screen displays a message telling the user to
insert a deposit envelope into the deposit slot.
3. If the deposit slot receives a deposit envelope, the
ATM credits the deposit amount to the user’s
account balance in the bank’s database.
3.10 Examining the ATM Requirements 3.10 Examining the ATM Requirements
Document (Cont.) Document (Cont.)
• Requirements gathering might include interviews • To capture what a proposed system should do,
with potential users of the system and specialists in developers often employ a technique known as use
fields related to the system. case modeling.
• The software life cycle specifies the stages through • Use cases represent different capabilities that the
which software evolves from the time it is conceived system provides to its clients.
to the time it is retired from use. • The simplified ATM system we build in this case
– Waterfall models perform each stage once in succession. study requires only the first three use cases (Fig. 3.31).
– Iterative models may repeat one or more stages several times
throughout a product’s life cycle.
19 20
3.10 Examining the ATM Requirements 3.10 Examining the ATM Requirements
Document (Cont.) Document (Cont.)
Use Case Diagrams • The stick figure represents an actor, which defines the
• A use case diagram models the interactions between a roles that an external entity—such as a person or
system’s clients and the system. another system—plays when interacting with the
• Figure 3.31 shows the use case diagram for our ATM system. system.
3.10 Examining the ATM Requirements 4.12 Software Engineering Case Study:
Document (Cont.) Identifying the Classes in the ATM
• A design specification should specify how the system should Requirements Document
be constructed to satisfy the system requirements. • We create classes only for the nouns and noun phrases in the
ATM system (Fig. 4.21).
• A system is a set of components that interact to solve a
• We do not need to model some nouns such as “bank” which
problem. are not part of the ATM operations.
• For example, to perform the ATM system’s designated tasks,
our ATM system has a user interface (Fig. 3.28), contains Nouns and noun phrases in the requirements document
software that executes financial transactions and interacts with bank money / funds account number
23 24
4.12 Identifying the Classes in the ATM 4.12 Identifying the Classes in the ATM
Requirements Document (Cont.) Requirements Document (Cont.)
• UML class diagrams model the classes in the ATM system and their • Figure 4.23 shows how our classes ATM and Withdrawal relate to
interrelationships (Fig. 4.22). one another.
– The top compartment contains the name of the class. – The line that connects the two classes represents an association.
– The middle compartment contains the class’s attributes. – Multiplicity values indicate how many objects of each class
– The bottom compartment contains the class’s operations. participate in the association.
– One ATM object participates in an association with either zero or
one Withdrawal objects.
• currentTransaction is a role name, which identifies the role
the Withdrawal object plays.
Fig. 4.22 | Representing a class in the UML using a class diagram.
4.12 Identifying the Classes in the ATM 4.12 Identifying the Classes in the ATM
Requirements Document (Cont.) Requirements Document (Cont.)
• Composition relationships have the following properties: • Figure 4.26 shows a class diagram for the ATM system.
– Only one class in the relationship can represent the whole. • The class diagram shows that class ATM has a one-to-one
– The parts in the composition relationship exist only as long as the relationship with class BankDatabase.
whole. • We also model that one object of class BankDatabase
– A part may belong to only one whole at a time. participates in a composition relationship with zero or
• If a “has-a” relationship does not satisfy one or more of more objects of class Account.
these criteria, hollow diamonds are used to indicate
aggregation.
31 32
5.14 Identifying Class Attributes in the ATM 5.14 Identifying Class Attributes in the
System (Cont.) Class Descriptive words and phrases ATM System (Cont.)
ATM user is authenticated
BalanceInquiry account number
Withdrawal account number
• The class diagram in
amount Fig. 5.19 lists some of
Deposit account number the attributes for the
amount
classes in our system.
BankDatabase [no descriptive words or phrases]
Account account number • We list each attribute’s
PIN name and type,
balance
followed in some cases
Screen [no descriptive words or phrases]
by an initial value.
Keypad [no descriptive words or phrases]
CashDispenser begins each day loaded with 500 $20 bills
DepositSlot [no descriptive words or phrases]
35 36
6.10 Software Engineering Case Study: 6.10 Software Engineering Case Study:
Identifying Objects’ States and Activities Identifying Objects’ States and Activities
in the ATM System (Cont.) in the ATM System (Cont.)
• Figure 6.26 is a simple state machine diagram that models two of • An activity diagram models an object’s workflow
the states of an object of class ATM. during application execution.
• The activity diagram in Fig. 6.27 models the actions
involved in executing a BalanceInquiry
transaction.
Fig. 6.26 | State machine diagram for some of the states of the ATM object.
06/06/52
Fig. 6.27 | Activity diagram for a BalanceInquiry
Dr.Mud-Armeen Munlin
transaction. 06/06/52 Dr.Mud-Armeen Munlin
39 40
6.10 Software
7.15 Software Engineering Case Study:
Engineering Case Identifying Class Operations in the ATM System
Study: Identifying
Objects’ States and • An operation is a service that objects of a class provide to
clients of the class.
Activities in the ATM
• The verbs and verb phrases in Fig. 7.20 help us determine the
System (Cont.) operations of our classes.
Fig. 7.20| Verbs and verb phrases for each class in the ATM system. (Part 1 of 2.)
06/06/52 Dr.Mud-Armeen Munlin 06/06/52 Dr.Mud-Armeen Munlin
Fig. 6.28 | Activity diagram for a Withdrawal transaction.
41 42
7.15 Software Engineering Case Study: 7.15 Software Engineering Case Study:
Identifying Class Operations in the ATM System Identifying Class Operations in the ATM System
(Cont.) (Cont.)
Class Verbs and verb phrases
BankDatabase authenticates a user, retrieves an account balance, credits an account, Modeling Operations
debits an account
Account retrieves an account balance, credits a deposit amount to an account,
• We place operations in the third compartment of the
debits a withdrawal amount to an account three transaction classes in the updated class diagram
Screen displays a message to the user of Fig. 7.21.
Keypad receives numeric input from the user
CashDispenser dispenses cash, indicates whether it contains enough cash to satisfy a
withdrawal request
DepositSlot receives a deposit envelope
Fig. 7.20| Verbs and verb phrases for each class in the ATM system. (Part 2 of 2.)
43 44
7.15 Software Engineering Case Study: 7.15 Software Engineering Case Study:
Identifying Class Operations in the ATM System Identifying Class Operations in the ATM System
(Cont.) (Cont.)
7.15 Software Engineering Case Study: 7.15 Software Engineering Case Study:
Identifying Class Operations in the ATM System Identifying Class Operations in the ATM System
(Cont.) (Cont.)
• Class Properties are placed in the second compartment of a class
Operations of Class BankDatabase and Class
diagram.
Account • To indicate a read-only property in the UML, we follow the property’s
• Class BankDatabase needs an operation that provides an type with “{readOnly}.”
authentication service to the ATM. • We modeled GetAvailableBalance and GetTotalBalance as
• We place the operation AuthenticateUser in the third operations in class BankDatabase so that we could specify
compartment of class BankDatabase. parameters to which the ATM can pass arguments.
• We add a ValidatePIN operation to class Account. • We assign Credit and Debit operations to classes BankDatabase
• Note that we specify a return type of bool for the and Account.
Authenticate-User and ValidatePIN operations. – Crediting an account adds an amount only to the Account’s total
balance.
• The database acts as an intermediary between the ATM and the
– Debiting an account, subtracts the amount from both the total and
account data, preventing unauthorized access.
available balances.
06/06/52 Dr.Mud-Armeen Munlin 06/06/52 Dr.Mud-Armeen Munlin
47 48
7.15 Software Engineering Case Study: 7.15 Software Engineering Case Study:
Identifying Class Operations in the ATM System Identifying Class Operations in the ATM System
(Cont.) (Cont.)
Operations of Class Screen
• All visual output occurs through the screen of the ATM.
Identifying and Modeling Operation Parameters
• We identify an operation’s parameters by examining what data the
• We simply create one operation that can display any message
operation requires to perform its assigned task.
specified by a parameter.
• The class diagram in Fig. 7.22 models class BankDatabase.
Operations of Class Keypad
• Class Keypad should perform a GetInput operation.
7.15 Software Engineering Case Study: 7.15 Software Engineering Case Study:
Identifying Class Operations in the ATM System Identifying Class Operations in the ATM System
(Cont.) (Cont.)
• The class diagram in Fig. 7.23 models the parameters of • Figure 7.24 models class Screen with a parameter for
class Account’s operations. operation Display-Message.
51 52
7.15 Software Engineering Case Study: 8.14 Software Engineering Case Study:
Collaboration Among Objects in the ATM
Identifying Class Operations in the ATM System
(Cont.) System
• The class diagram in Fig. 7.25 specifies that operation • When two objects communicate with each other to
DispenseCash of class CashDispenser takes decimal accomplish a task, they are said to collaborate.
parameter amount to indicate the amount
of cash (in dollars) to be dispensed. • A collaboration consists of an object of one class
sending a message to an object of another class.
• Messages are sent in C# via method calls.
55 56
8.14 Software Engineering Case Study: 8.14 Software Engineering Case Study:
Collaboration Among Objects in the ATM Collaboration Among Objects in the ATM
System (Cont.) System (Cont.)
An object to an object
of class... sends the message... of class... Interaction Diagrams
Withdrawal DisplayMessage Screen
• The UML provides several types of interaction
GetInput Keypad
GetAvailableBalance BankDatabase diagrams that model the behavior of a system by
IsSufficientCashAvailable CashDispenser modeling how objects interact with one another.
Debit BankDatabase
DispenseCash CashDispenser • The communication diagram emphasizes which
Deposit DisplayMessage Screen
GetInput Keypad
objects participate in collaborations.
IsDepositEnvelopeReceived DepositSlot
Credit BankDatabase • The sequence diagram emphasizes when messages
BankDatabase ValidatePIN
AvailableBalance (get)
Account
Account
are sent between objects.
TotalBalance (get) Account
Debit Account
Credit Account
06/06/52 Dr.Mud-Armeen Munlin 06/06/52 Dr.Mud-Armeen Munlin
Fig. 8.24 | Collaborations in the ATM system. (Part 2 of 2.)
57 58
8.14 Software Engineering Case Study: 8.14 Software Engineering Case Study:
Collaboration Among Objects in the ATM Collaboration Among Objects in the ATM
System (Cont.) System (Cont.)
Communication Diagrams • Communicating objects are connected with solid lines.
• Figure 8.25 shows a communication diagram that models the • Messages are passed between objects along these lines
ATM executing a BalanceInquiry. in the direction shown by arrows with filled
arrowheads.
• The filled arrow in Fig. 8.25 represents a message—or
synchronous call—in the UML and a method call in
Fig. 8.25 | Communication diagram of the ATM executing a balance inquiry. C#.
59 60
8.14 Software Engineering Case Study: 8.14 Software Engineering Case Study:
Collaboration Among Objects in the ATM Collaboration Among Objects in the ATM
System (Cont.) System (Cont.)
Sequence of Messages in a Communication Diagram
• Note that Fig. 8.26 models two additional messages passing
• Figure 8.26 shows a communication diagram that models the
from the BankDatabase to an Account (message 1.1 and
interactions among objects in the system when an object of class
BalanceInquiry executes.
message 2.1).
• The BankDatabase must send get messages to the
Account’s AvailableBalance and TotalBalance
properties.
• A message passed within the handling of another message is
called a nested message.
• The UML recommends using a decimal numbering scheme to
indicate nested messages. If the BankDatabase needed to
pass a second nested message while processing message 1, it
would be numbered 1.2.
06/06/52 Dr.Mud-Armeen Munlin 06/06/52 Dr.Mud-Armeen Munlin
Fig. 8.26 | Communication diagram for executing a BalanceInquiry.
61 62
8.14 Software Engineering Case Study: 8.14 Software Engineering Case Study:
Collaboration Among Objects in the ATM Collaboration Among Objects in the ATM
System (Cont.) System (Cont.)
Sequence Diagrams
• A sequence diagram helps model the timing of
collaborations more clearly.
• Figure 8.27 shows a sequence diagram modeling the
sequence of interactions that occur when a
Withdrawal executes.
63 64
8.14 Software Engineering Case Study: 10.22 Software Engineering Case Study:
Collaboration Among Objects in the ATM Starting to Program the Classes of the ATM
System (Cont.) System
• The dotted line extending down from an object’s rectangle Visibility
is that object’s lifeline, which represents the progression
of time. • Access modifiers determine the visibility, or
accessibility, of an object’s attributes and operations
• Actions typically occur along an object’s lifeline in
to other objects.
chronological order from top to bottom.
• An activation, shown as a thin vertical rectangle, indicates • The UML employs visibility markers for modeling
that an object is executing. the visibility of attributes and operations.
– Public visibility is indicated by a plus sign (+).
– A minus sign (–) indicates private visibility.
67 68
objects. 5
6
// parameterless constructor
public Withdrawal()
7 {
• Associations in a class diagram that have navigability 8 // constructor body code
9 } // end constructor
arrows at both ends or do not have navigability arrows 10 } // end class Withdrawal
at all indicate bidirectional navigability. Fig. 10.32 | Initial C# code for class Withdrawal based on
Figs. 10.30 and 10.31.
71 72
Outline Outline
• Use the operations located in the third compartment of
Fig. 10.30 to declare the shells of the methods. If we have not
yet specified a return type for an operation, we declare the
• Withdrawal.cs method with return type void. Refer to the class diagrams to • Withdrawal.cs
14
15 // parameterless constructor • (2 of 2 ) declare any necessary parameters. • (1 of 2 )
16 public Withdrawal()
17 {
• Adding the public operation Execute (which has an
18 // constructor body code empty parameter list) in class Withdrawal yields the code
19 } // end constructor
20 } // end class Withdrawal
in Fig. 10.35.
1 // Fig. 10.35: Withdrawal.cs
Fig. 10.34 | Incorporating private reference handles for the associations 2 // Class Withdrawal represents an ATM withdrawal transaction
of class Withdrawal based on Figs. 10.30 and 10.31. (Part 2 of 2.) 3 public class Withdrawal
4 {
5 // attributes
6 private int accountNumber; // account to withdraw funds from
7 private decimal amount; // amount to withdraw
8
9 // references to associated objects
10 private Screen screen; // ATM’s screen
11 private Keypad keypad; // ATM’s keypad
12 private CashDispenser cashDispenser; // ATM’s cash dispenser
13 private BankDatabase bankDatabase; // account-information database
75 76
12.9 Software Engineering Case Study: 12.9 Software Engineering Case Study:
Incorporating Inheritance and Polymorphism Incorporating Inheritance and Polymorphism
into the ATM System (Cont.) into the ATM System (Cont.)
• The arrows with triangular hollow arrowheads indicate • As Fig. 12.19 shows, classes BalanceInquiry,
that classes BalanceInquiry, Withdrawal and Withdrawal and Deposit share private int attribute
Deposit are derived from class Transaction by accountNumber.
inheritance. • Because the derived classes in this case do not need to modify
– Class Transaction is said to be a generalization of its derived attribute accountNumber, we have chosen to replace private
classes. attribute accountNumber in our model with the public
– The derived classes are said to be specializations of class read-only property AccountNumber.
Transaction . • Since this is a read-only property, it provides only a get
accessor to access the account number.
• We declare Execute as an abstract operation in base class
Transaction—it will become an abstract method in the
C# implementation.
06/06/52 Dr.Mud-Armeen Munlin 06/06/52 Dr.Mud-Armeen Munlin
77 78
12.9 Software Engineering Case Study: 12.9 Software Engineering Case Study:
Incorporating Inheritance and Polymorphism Incorporating Inheritance and Polymorphism
into the ATM System (Cont.) into the ATM System (Cont.)
• Figure 12.21 presents an updated class diagram of our model that incorporates
inheritance and introduces abstract base class Transaction.
79 80
12.9 Software Engineering Case Study: 12.9 Software Engineering Case Study:
Incorporating Inheritance and Polymorphism Incorporating Inheritance and Polymorphism
into the ATM System (Cont.) into the ATM System (Cont.)
• We model an association between class ATM and class • We also add an association between Transaction and
Transaction to show that the ATM, at any given moment, BankDatabase (Fig. 12.21). All Transactions require a
either is executing a transaction or is not. reference to the BankDatabase so that they can access and
• Because a Withdrawal is a type of Transaction, we no modify account information.
longer draw an association line directly between class ATM and • We include an association between class Transaction and
class Withdrawal—derived class Withdrawal inherits the Screen because all Transactions display output to the
base class Transaction’s association with class ATM. user via the Screen.
• Derived classes BalanceInquiry and Deposit also • Class Withdrawal still participates in associations with the
inherit this association, which replaces the previously omitted CashDispenser and the Keypad.
associations between classes BalanceInquiry and
Deposit, and class ATM.
12.9 Software Engineering Case Study: 12.9 Software Engineering Case Study:
Incorporating Inheritance and Polymorphism Incorporating Inheritance and Polymorphism
into the ATM System (Cont.) into the ATM System (Cont.)
83 84