0% found this document useful (0 votes)
12 views10 pages

Specifying Operations

Uploaded by

syedaqudeja
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)
12 views10 pages

Specifying Operations

Uploaded by

syedaqudeja
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/ 10

In this Lecture you will Learn:

„ About the role of operation specifications


„ What is meant by “Contracts”
„ About algorithmic and non-algorithmic techniques, and
Specifying Operations how they differ
„ How to use:
‹ Decision Tables
Chapter 10
‹ Pre- and Post-condition Pairs

‹ Structured English

‹ Activity Diagrams

‹ Object Constraint Language

M8748 © Peter Lo 2007 1 M8748 © Peter Lo 2007 2

Main Purposes of Operation


Why we Specify Operations
Specification
„ From analysis perspective: „ Confirm that the logic of user requirements has
‹ Ensure users’ needs are understood been understood correctly and documented
„ From design perspective: accurately.
‹ Guide programmer to an appropriate
„ Provide designers and programmers with a
implementation (i.e. method) detailed and unambiguous basis for the design and
implementation of system behavior.
„ From test perspective:
‹ Verify that the method does what was
originally intended

M8748 © Peter Lo 2007 3 M8748 © Peter Lo 2007 4


Types of Operation and Their Effects Services among Objects
„ Operations with side-effects may: „ When objects collaborate, one object typically
‹ Create or destroy object instances provides a service to another
‹ Set attribute values „ Examples:
‹ Form or break links with other objects ‹ A Client object might ask a Campaign object
‹ Carry out calculations for its details
‹ Send messages or events to other objects ‹ The same Client object might then ask a
‹ Any combination of these boundary object to display its related
„ Operations without side-effects are pure queries Campaign details to the user
‹ Request data but do not change anything

M8748 © Peter Lo 2007 5 M8748 © Peter Lo 2007 6

Contracts: an Approach to Defining


Class Exercise
Services
„ Why are operation specifications in an object-oriented „ A service can be defined as a contract between the
project likely to be small? participating objects
„ Contracts focus on inputs and outputs
„ Intervening process is seen as a black box
„ Irrelevant details are hidden
„ This emphasizes service delivery, and ignores
implementation

M8748 © Peter Lo 2007 7 M8748 © Peter Lo 2007 8


Contract-Style Operation
Types of Logic Specification
Specification
„ Intent or purpose of the operation „ Logic description is probably the most important
„ Operation signature including return type element
„ Description of the logic „ Two main categories:
„ Other operations called ‹ Algorithmic Specification Techniques

„ Events transmitted to other objects describe the sequence of internal logical steps
that an operation is to follow.
„ Any attributes set
‹ Non-Algorithmic Techniques describe the
„ Response to exceptions (e.g. an invalid parameter) result for a given set of inputs.
„ Non-functional requirements

M8748 © Peter Lo 2007 9 M8748 © Peter Lo 2007 10

Non-Algorithmic Techniques Decision Tree


„ Black box type „ Diagram that shows conditions and actions graphically
„ Describe the result for a given set of inputs.
„ Focus on what the operation should achieve
„ Appropriate where correct result matters more than method
to arrive at it
‹ Decision Tree: complex decisions, multiple criteria and

steps
‹ Decision Table: similar applications to decision tree

‹ Pre-condition and Post-condition Pairs: suitable

where precise logic is unimportant or uncertain

M8748 © Peter Lo 2007 11 M8748 © Peter Lo 2007 12


Decision Table Decision Table
„ Decision tables are particularly suited to representing „ Provides a notation that translates actions and conditions
decisions with complex multiple input conditions and (described in a processing narrative) into a tabular form.
complex multiple outcomes, where the precise sequence of ‹ The upper left-hand section contains a list of all
steps either is not significant or is not known. conditions.
Conditions and actions Rule 1 Rule 2 Rule 3 ‹ The lower left-hand section lists all actions that are

Conditions possible based on the conditions.


Is budget likely to be overspent? N Y Y ‹ The right-hand sections form a matrix that indicates

Is overspend likely to exceed 2%? - N Y condition combinations and the corresponding actions
Actions
that will occur for a specific combination.
No action X
Send letter X X
Set up
M8748 meeting
© Peter Lo 2007 X 13 M8748 © Peter Lo 2007 14

Class Exercise Pre- and Post-condition Pair


„ To what kinds of situation are decision tables particularly „ Pre-conditions allow the user and analyst to agree the
suited? precise conditions under which it should be possible for an
operation to run.
„ Post-conditions specify the intended state of the system
after the operation has executed.
„ This information can be used later by a program designer
to specify the operation, by a programmer to code it and by
a tester to check whether it works as intended.
„ Together, pre- and post-conditions describe an operation’s
interface to the rest of the system.

M8748 © Peter Lo 2007 15 M8748 © Peter Lo 2007 16


Example Class Exercise
„ Pre-conditions: „ Why is it important to specify both pre- and post-
‹ creativeStaffObject is valid conditions for an operation?
‹ gradeObject is valid
‹ gradeChangeDate is a valid date
„ Post-conditions:
‹ a new staffGradeObject exists
‹ new staffGradeObject linked to creativeStaffObject
‹ new staffGradeObject linked to previous
‹ value of previous staffGradeObject.gradeFinishDate
set equal to gradeChangeDate

M8748 © Peter Lo 2007 17 M8748 © Peter Lo 2007 18

Algorithmic Techniques Structured English


„ White box type „ Commonly used, easy to learn
„ Describe the sequence of internal logical steps that an
operation is to follow „ Also called Program Design Language (PDL), or
„ Focus on how the operation might work Pseudocode.
„ Suitable where users understand the procedure for arriving
at a result
„ Can be constructed top-down, to handle arbitrarily
complex functionality
„ Examples:
‹ Structured English

‹ Activity Diagrams

M8748 © Peter Lo 2007 19 M8748 © Peter Lo 2007 20


Control Structure in Structured Sequence Structure in Structured
English English
„ Three types of control structure, derived from „ Each instruction executed in
structured programming: turn, one after another

‹ Sequences of instructions get client contact name


sale cost = item cost * (1 - discount rate)
‹ Selection of alternative instructions (or groups calculate total bonus
of instructions) description = new description

‹ Iteration (repetition) of instructions (or groups


of instructions)

M8748 © Peter Lo 2007 21 M8748 © Peter Lo 2007 22

Selection Structure in Structured Selection Structure in Structured


English English
„ One or other alternative course is followed, depending on
result of a test:
if client contact is ’Sushila’
set discount rate to 5%
else
set discount rate to 2%
end if

M8748 © Peter Lo 2007 23 M8748 © Peter Lo 2007 24


Iteration Structure in Structured Iteration Structure in Structured
English English
„ Instruction or block of instructions is repeated
‹ Can be a set number of repeats

‹ Or until some test is satisfied, for example:

do while there are more staff in the list


calculate staff bonus
store bonus amount
end do

M8748 © Peter Lo 2007 25 M8748 © Peter Lo 2007 26

Activity Diagrams Simple Activity Diagram

„ Activities diagrams can be used to specify the „ An activity diagram showing the main steps for the
logic of procedurally complex operation. operation CreativeStaff.changeGrade().

„ Are part of UML notation set


„ Can be used for operation logic specification,
among many other uses
„ Are easy to learn and understand
„ Have the immediacy of graphical notation
„ Bear some resemblance to old-fashioned flowchart
technique
M8748 © Peter Lo 2007 27 M8748 © Peter Lo 2007 28
Activity Diagram with Selection and
Activity Diagram with Selection
Iteration
„ A activity diagram for CreativeStaff.changeGrade(), with „ Activity diagram for Prepare bonus list(),
an initial selection to check that approval has been given. showing selection and iteration structures.

M8748 © Peter Lo 2007 29 M8748 © Peter Lo 2007 30

Object Constraint Language (OCL) Component of OCL - Context

„ OCL expressions usually consist of: „ Defines domain within which expression is valid
‹ A Context within which the expression is valid „ Instance of a type, e.g. object in class diagram
(for example, a specified class); „ Link (association instance) may be a context
‹ A Property within the context to which the
expression applies (for example, an attribute of
the specified class);
‹ An Operation that is applied to the property
(for example, a mathematical expression that
tests the value of the attribute).

M8748 © Peter Lo 2007 31 M8748 © Peter Lo 2007 32


Component of OCL - Property Component of OCL - Operation

„ A property of that instance „ OCL operation is applied to the property


„ Often an attribute, association-end or query „ Operations include
operation ‹ Arithmetical operators *, +, - and /

‹ Set operators such as size, isEmpty and select

‹ Type operators such as oclIsTypeOf

M8748 © Peter Lo 2007 33 M8748 © Peter Lo 2007 34

OCL Used for Pre- and Post-


OCL Expression and Interpretation
conditions
OCL expression Interpretation
„ context CreativeStaff::changeGrade
Person In the context of a specific person, the value (grade:Grade, gradeChangeDate:Date)
self.gender of the property ‘gender’ of that person—i.e. a
person’s gender. „ pre:
‹ grade oclIsTypeOf(Grade)
Person The property ‘savings’ of the person under
self.savings >= 500 consideration must be greater than or equal ‹ gradeChangeDate >= today
to 500.
„ post:
Person If the set ‘husband’ associated with a person
self.husband->notEmpty implies is not empty, then the value of the property ‹ self.staffGrade[grade]->exists
self.husband.gender = male ‘gender’ of the husband must be male.
‹ self.staffGrade[previous]->notEmpty
Boldface denotes OCL keyword, but has no
semantic import. ‹ self.staffGrade.gradeStartDate =

Company The size of the set of the property ‘CEO’ of a gradeChangeDate


self.CEO->size <= 1 company must be less than or equal to 1.
‹ self.staffGrade.previous.gradeFinishDate =
That is, a company cannot have more than 1
Chief Executive Officer. gradeChangeDate
Company The set of employees of a company whose
self.employee->select (age < 60) age is less than 60.
M8748 © Peter Lo 2007 35 M8748 © Peter Lo 2007 36
Class Exercise Class Exercise
„ Why are non-algorithmic (or declarative) approaches „ What is an invariant?
generally preferred in object-oriented development?

M8748 © Peter Lo 2007 37 M8748 © Peter Lo 2007 38

You might also like