1 lecture 7: operation contracts. 2 overview what is contract ? the guidelines for writing...

26
1 Lecture 7: Operation Contracts

Upload: pauline-mitchell

Post on 14-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

1

Lecture 7: Operation Contracts

Page 2: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

2

Overview

What is contract ?

The guidelines for writing contracts for the system operations.

Use Case realizations.

Writing contracts leads to domain model updates.

Page 3: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

3

Contracts

Contracts are documents that describe system behavior.

Contracts may be defined for system operations. Operations that the system (as a black box) offers in its public interface to handle

incoming system events.

The entire set of system operations across all use cases, defines the public system interface.

Page 4: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

4

SSD for BuyItem use case

Page 5: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

5

System Operations and the System Interface

In the UML the system as a whole can be represented as a class.

Contracts are written for each system operation to describe its behavior.

Page 6: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

6

Guidelines for Contracts

Page 7: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

7

Use Case Realizations

A use case realization describes how a use case is realized in terms of collaborating objects.

UML interaction diagrams are used to illustrate use case realizations.

Recall Process Sale: from main scenario we identified a number of system events (operations).

Each system event was then described by a contract.

Page 8: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

8

Contracts

A contract is a document that describes what an operation commits to achieve.

Emphasizing what will happen rather than how it will be achieved.

Contracts can be expressed by pre- and post- conditions.

Page 9: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

9

Conceptual Model and Contracts

A Conceptual Model is a visual representation of conceptual classes or real-world objects in a domain of interest.

Contracts describe detailed system behavior in term of state changes to objects in the Domain Model, after a system operation has executed.

Page 10: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

10

Utilize Conceptual Model

To make contracts you have to use the conceptual model.

We added the conceptual model from previous slides to assist the comprehension of post conditions in the contracts.

Page 11: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

11

Domain Model

Page 12: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

12

Page 13: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

13

Contract Sections

Operation: Name Of operation, and parameters.Cross References: (optional) Use cases this can occur

within.

Preconditions:Noteworthy assumptions about the state of the system or objects in

the Domain Model before execution of the operation.

Postconditions: -The state of objects in the Domain Model after completion of the operation.

Page 14: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

14

Example: AddLineItem Contract

Contract CO2: AddLineItem

Operation: addLineItem (itemID: ItemID, quantity: integer)

Cross References: Use Cases: Process Sale.

Pre-conditions: There is a sale underway.

Post-conditions:

A SalesLineItem instance sli was created. (instance creation)

sli was associated with the current Sale. (association formed)

sli.quantity was set to quantity. (attribute modification)

sli was associated with a ProductSpecification, based on itemID match. (association formed)

Page 15: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

15

Pre- & Post- Conditions

Preconditions are assumptions about the state of the system before execution of the operation.

A Postcondition is an assumption that refers to the state of the system after completion of the operation.

The postconditions are not actions to be performed during the operation.

Describe changes in the state of the objects in the Domain Model (instances created, associations are being formed or broken, and attributes are changed).

Page 16: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

16

How to Make a Contract

1.Identify system operations from the system sequence diagrams.

2.For each system operation, construct a contract.

3.Write the preconditions section.

4.Write the postconditions section describing the state changes that occur to concepts in the conceptual model. Postconditon categories:

Instance creation and deletion.

Attribute modification.

Associations formed and broken.

Page 17: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

17

AddLineItem PostconditionsCreation and Deletion

After the itemID and quantity of an item have been entered by the cashier, what new objects should have been created?

A SaleLineItem instance sli was created.

Page 18: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

18

AddLineItem PostconditionsAttribute Modification

After the itemID and quantity of an item have been entered by the cashier, what attributes of new or existing objects should have been modified?

sli.quantity was set to quantity.

Page 19: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

19

AddLineItem PostconditionsAssociations Formed and Broken

After the itemID and quantity of an item have been entered by the cashier, what associations of new or existing objects should have been formed or broken?

sli was associated with the current Sale. sli was associated with a ProductSpecification, based on itemID match.

Page 20: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

20

Make New Sale Enumeration Example

Assume a new sale is started, we will create a new object from the conceptual class Sale, with an ID=5.

If this sale started on the register number 3, then we have to create an association between the sale number 5 with the register number 3 (associations are established by updating foreign keys).

Page 21: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

21

Writing Contracts leads to Domain Model Updates

It is also common to discover the need to record new concepts, attributes or associations in the Domain Model.

For example:

If in one of the postconditions, you wanted to create an instance to store some values in it, and it was not presented by any Concept in the domain model.

Then you should go back and correct your domain model by adding this concept.

Page 22: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

22

The most common mistake in creating contract

The most common problem in creating contracts is forgetting to include the forming of associations. Particularly, when new instances are created, it is very likely that associations to several objects need be established.

Don’t forget to include all the associations formed and broken.

Page 23: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

23

Example 1: End Sale Contract

Contract: EndSale

Operation: EndSale ()

Cross References: Use Cases: Process Sale.

Pre-conditions: There is a sale underway and all items have been entered.

Post-conditions: Sale.isComplete became true. (attribute modification)

Page 24: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

24

Example 2: Make Payment Contract

Contract: MakePayment

Operation: MakePayment (amount: Number or Quantity)

Cross References: Use Cases: Process Sale.

Pre-conditions: There is a sale underway and all items have been entered.

Post-conditions:

A Payment instance p was created. (instance creation)

p.amountTendered was set to amount. (attribute modification)

p was associated with the current Sale. (association formed)

The current Sale was associated with the Store; to add it to the historical log of completed sales. (association formed)

Page 25: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

25

Example 3: Start Up Contract

Contract: StartUp

Operation: StartUp ()

Cross References: Use Cases: Process Sale.

Pre-conditions: None.

Post-conditions:

A Store, POST, ProductCatalog and ProductSpecifications have been created. (instance creation)

ProductCatalog was associated with ProductSpecifications. (association formed)

Store was associated with ProductCatalog. (association formed)

Store was associated with POST. (association formed)

POST was associated with ProductCatalog. (association formed)

Page 26: 1 Lecture 7: Operation Contracts. 2 Overview  What is contract ?  The guidelines for writing contracts for the system operations.  Use Case realizations

26

Summary

Contracts describe detailed system behavior in terms of state changes to objects in the Conceptual model after a system operation.

Contracts have sections of Operations, Cross references, Preconditions and Postconditions. Postconditions are the most important section.

Postconditions describe changes in the state of objects in the Conceptual model.