object oriented software development 7. implementing a model

43
Object Oriented Software Development 7. Implementing a model

Upload: sandra-bishop

Post on 13-Jan-2016

222 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Object Oriented Software Development 7. Implementing a model

Object Oriented Software Development

7. Implementing a model

Page 2: Object Oriented Software Development 7. Implementing a model

UML models

UML may be used to visualise, specify, construct and document the artefacts of a software system

Part of a software development method To produce a working application the

artefacts in the model must be implemented using development tools, languages and frameworks

Object Oriented Software Development 7. Implementing a model2

Page 3: Object Oriented Software Development 7. Implementing a model

Implementing a model

We will use Visual Studio, C# and the .NET framework to begin the implementation of a UML model

Based on examples from Software Modelling Analysis and Design 1

That module and this one represent two closely related aspects of the same process

Object Oriented Software Development 7. Implementing a model3

Page 4: Object Oriented Software Development 7. Implementing a model

UML diagrams and C# code Class Diagram – a C# application is constructed

as classes, which generally correspond to classes in the class diagram

Sequence/Collaboration Diagrams – these describe the messages which pass between objects, and relate to method calls in C# code

Object Diagram – represents the objects which exist as the program runs

Use Case Flow of Events/Activity Diagrams – describe sequences of events and workflows which must be implemented in C# code

Object Oriented Software Development 7. Implementing a model4

Page 5: Object Oriented Software Development 7. Implementing a model

Example – order system

In the system, an order consists of a number of order lines

An order line specifies a single product and the quantity of that product ordered

Each order is associated with a customer, who has a discount level which is applied to all his or her orders

Sample code in OrderSystem project

Object Oriented Software Development 7. Implementing a model5

Page 6: Object Oriented Software Development 7. Implementing a model

Use case – calculate order price

We will look at the use case, the classes involved in it and the sequence diagram for it

Use Case Description Calculates the total price of a single order

including the customer’s discount Use Case Flow of events

Calculate the price of each order line as (product price) x (quantity) and add to total

Apply the customer’s discount to the total and calculate the final price

Object Oriented Software Development 7. Implementing a model6

Page 7: Object Oriented Software Development 7. Implementing a model

Class diagram

Object Oriented Software Development 7. Implementing a model7

orderline only exists as part of an order - composition

orderline only exists as part of an order - composition

customer can be associated with many orders

customer can be associated with many orders

order line contains one product onlyorder line contains one product only

-CalcDiscPrice()+CalcPrice()

-dateReceived : Date-ordNumber

Order

+GetDiscount()

-lastName : string-firstName : string-discount : float

Customer

+GetLinePrice()

-quantity : int

OrderLine

+GetPrice()

-name : string-productID : string-price : decimal

Product

* 1

1

-orderLines*

1

-product

1

Page 8: Object Oriented Software Development 7. Implementing a model

Sequence diagram

Object Oriented Software Development 7. Implementing a model8

responsibility for calculating its price belongs to order

responsibility for calculating its price belongs to order

collaborates with other objects to do socollaborates with other objects to do so

anOrder anOrderLine aProduct aCustomer

GetPrice()

GetLinePrice()

CalcPrice()

CalcDiscPrice()

GetDiscount()

[for each order line]loop

price

linePrice

discount

Page 9: Object Oriented Software Development 7. Implementing a model

Create classes using class diagram

Create a new C# class for each class in the diagram

Declare instance variables based on the attributes in the diagram

Define (empty) methods based on the operations in the diagram

Methods which simply access instance variables (e.g. GetDiscount) can be implemented in C# as properties

Object Oriented Software Development 7. Implementing a model9

Page 10: Object Oriented Software Development 7. Implementing a model

Create classes using class diagram

Consider whether properties should be read-write or read-only

Define constructor to set values of instance variables

Object Oriented Software Development 7. Implementing a model10

Page 11: Object Oriented Software Development 7. Implementing a model

Implement relationships

Add code to implement relationships between classes shown in the diagram

May need to refine model to clarify the meaning of the relationship in some cases

Purpose of relationship is to allow objects to collaborate, so sequence diagram may help clarification by showing what collaborations are needed

Object Oriented Software Development 7. Implementing a model11

Page 12: Object Oriented Software Development 7. Implementing a model

OrderLine – Product association

OrderLine is associated with a single product OrderLine sends a message to Product Product does not need to send message to

OrderLine, so doesn’t need an OrderLine reference

Association is navigable in one direction only, OrderLine to Product

Object Oriented Software Development 7. Implementing a model12

Page 13: Object Oriented Software Development 7. Implementing a model

OrderLine-Product implementation

Implement this with an instance variable in OrderLine of type Product

Product has no reference to OrderLine C# implementation option – create LinePrice

property instead of a GetLinePrice method Cleaner syntax but no difference conceptually –

accessing a property is a message, just like a method call

Object Oriented Software Development 7. Implementing a model13

Page 14: Object Oriented Software Development 7. Implementing a model

OrderLine-Product

Object Oriented Software Development 7. Implementing a model14

Note – when constructor would only set properties, can omit it and create objects with object initialiser syntax in C#

Note – when constructor would only set properties, can omit it and create objects with object initialiser syntax in C#

sends message to product object to ask for its pricesends message to product object to ask for its price

Page 15: Object Oriented Software Development 7. Implementing a model

Coding patterns

This is an example of a one-to-one “has-a” association OrderLine has-a Product

Common type of association, usually implemented using the same coding pattern

One class has an instance variable whose type is the name of the other class

Can also provide a property or accessor method for this instance variable if required

Object Oriented Software Development 7. Implementing a model15

Page 16: Object Oriented Software Development 7. Implementing a model

Coding patterns

A pattern is a general reusable solution to a common problem

Patterns can be applied to many aspects of software design and programming, from loops to complex groups of collaborating classes

Here we are using patterns for relationships between two classes (binary relationship coding patterns)

Object Oriented Software Development 7. Implementing a model16

Page 17: Object Oriented Software Development 7. Implementing a model

Coding patterns

The coding pattern for a relationship includes:

Implementation The code artefact without which the relationship

does not exist Helpers

Properties and methods which may be required to make use of the relationship in particular situations

Object Oriented Software Development 7. Implementing a model17

Page 18: Object Oriented Software Development 7. Implementing a model

Coding pattern: one-to-one “has-a” association

Implementation Instance variable in one class whose type is the

name of the other class Helpers

Property in the first class which can be used to get or set the associated object

Object Oriented Software Development 7. Implementing a model18

Page 19: Object Oriented Software Development 7. Implementing a model

Order – OrderLine association

Order is associated with many OrderLines Order “has-a” set of OrderLines OrderLine only exists as part of an order –

composition This is a whole-part relationship Order sends a message to OrderLine

(GetLinePrice) Association is navigable in one direction only,

Order to OrderLineObject Oriented Software Development 7. Implementing a model

19

Page 20: Object Oriented Software Development 7. Implementing a model

Order – OrderLine implementation

Implement this with an instance variable in Order which is a collection of type OrderLine

OrderLine has no reference to Order Order will call GetLinePrice method of each

of its OrderLines C# implementation option – what kind of

collection should we use? No need to search, will only add to end of

collection, best option is List<OrderLine>

Object Oriented Software Development 7. Implementing a model20

Page 21: Object Oriented Software Development 7. Implementing a model

Order - OrderLine

Object Oriented Software Development 7. Implementing a model21

sends message to OrderLine object to get its line price

sends message to OrderLine object to get its line price

Page 22: Object Oriented Software Development 7. Implementing a model

Order – OrderLine helper method Need a method which can add a new

OrderLine to an Order Composition – the new OrderLine object is

created within this method OrderLine is added to List using its Add

method

Object Oriented Software Development 7. Implementing a model22

Page 23: Object Oriented Software Development 7. Implementing a model

Coding pattern: one-to-many “has-a” association

Implementation Instance variable in one class whose type is a

collection which holds instances of the other class Helpers

Method to add objects to the collection (will create objects if the association is composition)

Method to remove an object Method to return a specific object Method to return the whole collection Only need to implement helper(s) as needed

Object Oriented Software Development 7. Implementing a model23

Page 24: Object Oriented Software Development 7. Implementing a model

Coding pattern example code

CodingPatterns solution OneToManyCompositionPattern project

Demonstrates pattern with a full set of helpers

Object Oriented Software Development 7. Implementing a model24

Page 25: Object Oriented Software Development 7. Implementing a model

Order-Customer association

Customer can have many Orders Customer “has-a” set of Orders But Order is not part of a Customer

Customer does not create Order Association or aggregation, not composition

Order is associated with one Customer Order “has-a” Customer

Object Oriented Software Development 7. Implementing a model25

Page 26: Object Oriented Software Development 7. Implementing a model

Order-Customer association

In this use case, Order needs to send message to Customer

Order class needs reference to Customer object

Order is associated with one Customer Order “has-a” Customer Association or aggregation

So the relationship between Order and Customer is navigable in both directions

Object Oriented Software Development 7. Implementing a model26

Page 27: Object Oriented Software Development 7. Implementing a model

Order-Customer association

Actually, this use case does not require any messages from Customer to Order

Don’t strictly need to implement association in that direction

Implementing it here because it is likely that other use cases will require it

e.g. Show orders for a customer

Object Oriented Software Development 7. Implementing a model27

Page 28: Object Oriented Software Development 7. Implementing a model

Aggregation and association

Aggregation Implies ownership Whole-part, or “has-a” relationship Part may be shared with other Wholes, unlike

composition Association

No implication of ownership “uses-a” relationship

Difference in meaning - makes little difference to code

Object Oriented Software Development 7. Implementing a model28

Page 29: Object Oriented Software Development 7. Implementing a model

Aggregation and association

“Few things in the UML cause more consternation than aggregation and composition, in particular how they vary from regular association...”

Martin Fowler, 2003 See link below for full quote:

http://www.martinfowler.com/bliki/AggregationAndComposition.html

Object Oriented Software Development 7. Implementing a model29

Page 30: Object Oriented Software Development 7. Implementing a model

Class diagram with navigabilities

Object Oriented Software Development 7. Implementing a model30

-CalcDiscPrice()+CalcPrice()

-dateReceived : Date-ordNumber

Order

+GetDiscount()

-lastName : string-firstName : string-discount : float

Customer

+GetLinePrice()

-quantity : int

OrderLine

+GetPrice()

-name : string-productID : string-price : decimal

Product

* 1

1

-orderLines*

1

-product

1

Page 31: Object Oriented Software Development 7. Implementing a model

Order-Customer implementation

Need an instance variable in Customer which is a collection of type Order

Need an instance variable in Order which is a reference to a Customer

Order will call GetDiscount method of its Customer

Need some code to make relationship consistent Order object should be part of the orders

collection belonging to its associated CustomerObject Oriented Software Development 7. Implementing a model

31

Page 32: Object Oriented Software Development 7. Implementing a model

Order-Customer implementation

Object Oriented Software Development 7. Implementing a model32

implemented as a Dictionary for fast searching

implemented as a Dictionary for fast searching

Page 33: Object Oriented Software Development 7. Implementing a model

Order-Customer consistency

Object Oriented Software Development 7. Implementing a model33

method in Customermethod in Customer

constructor of Orderconstructor of Order

when you create an order, you specify the customer it’s for, and add it to that customer’s orders

when you create an order, you specify the customer it’s for, and add it to that customer’s orders

Page 34: Object Oriented Software Development 7. Implementing a model

Creating objects

Object Oriented Software Development 7. Implementing a model34

create Customer objectcreate Customer object

create Order objectcreate Order object

Page 35: Object Oriented Software Development 7. Implementing a model

Coding pattern: one-to-many bidirectional association

Implementation Implement as a one-to-many and a one-to-one in

opposite directions Code in constructor of one class ensures

consistency Helpers

Same as one-to-many Remove method may need additional code to

deal with consistency

Object Oriented Software Development 7. Implementing a model35

Page 36: Object Oriented Software Development 7. Implementing a model

Coding pattern example code

OneToManyAssociationPattern project OneToManyBidirectionalPattern project

Demonstrate patterns with a full set of helpers

Object Oriented Software Development 7. Implementing a model36

Page 37: Object Oriented Software Development 7. Implementing a model

Order – Product association

We didn’t show an association on the class diagram between Order and Product

No message passed between Order and Product in sequence diagram

But there is a relationship Order “uses-a” Product to create an

OrderLine Simple association Sometimes modelled as a dependency

Object Oriented Software Development 7. Implementing a model37

Page 38: Object Oriented Software Development 7. Implementing a model

Order – Product association

AddOrderLine method in order has a parameter of type Product

Temporary association – only exists while this method runs

Sets up permanent association between Product and OrderLine

Object Oriented Software Development 7. Implementing a model38

Page 39: Object Oriented Software Development 7. Implementing a model

Class diagram with Order-Product

Object Oriented Software Development 7. Implementing a model39

-CalcDiscPrice()+CalcPrice()

-dateReceived : Date-ordNumber

Order

+GetDiscount()

-lastName : string-firstName : string-discount : float

Customer

+GetLinePrice()

-quantity : int

OrderLine

+GetPrice()

-name : string-productID : string-price : decimal

Product

* 1

1

-orderLines*

1

-product

1

Page 40: Object Oriented Software Development 7. Implementing a model

Coding pattern: simple association

Implementation One class has a reference to instance(s) of the

other in one of the following places: Method parameter Method return value Local variable

Object Oriented Software Development 7. Implementing a model40

Page 41: Object Oriented Software Development 7. Implementing a model

Coding pattern example code

SimpleAssociationPattern project

Object Oriented Software Development 7. Implementing a model41

Page 42: Object Oriented Software Development 7. Implementing a model

Other relationship examples

These examples and patterns don’t capture all possible types of relationship

Provide examples on which to base solutions to real problems

Object oriented systems model the real world, and reality can be complicated!

Object Oriented Software Development 7. Implementing a model42

Page 43: Object Oriented Software Development 7. Implementing a model

What’s next?

We will go on to look at some practical issues which are important in developing robust programs, including handling error situations with exceptions, debugging and testing

Object Oriented Software Development 7. Implementing a model43