design class diagrams

21
Design Class Diagrams Monday, 22 October 12

Upload: zhichaowang

Post on 16-Jan-2016

6 views

Category:

Documents


0 download

DESCRIPTION

f

TRANSCRIPT

Page 1: Design Class Diagrams

Design Class Diagrams

Monday, 22 October 12

Page 2: Design Class Diagrams

Why make Design Class Diagrams?

• allow us to create and show the specification for the software classes that we’ll build

• principally they show:

• classes and their associations

• attributes with their types and visibilities

• methods with their parameters, return types and visibilities

• and also:

• interfaces with their operations

• dependencies

• DCDs can be created in parallel with sequence diagrams

Monday, 22 October 12

Page 3: Design Class Diagrams

Example

1

Captures

Sale

DateisComplete : Booleantime

addLineItem(…)…

Register

1 makeLineItem()

Three section box Navigability

methods; parameters not specified Type information

from Ch 19 Applying UML & Patterns (Larman 2004)

Monday, 22 October 12

Page 4: Design Class Diagrams

Domain versus Design

from Ch 19 Applying UML & Patterns (Larman 2004)

1Captures

SaleDateisComplete : BooleantimeaddLineItem(…)

Register

1

makeLineItem()

Captures

Sale

DateisComplete : Booleantime

Register

1Domain Model

Design Model

*

Monday, 22 October 12

Page 5: Design Class Diagrams

How to Create a DCD

• draw for the whole system or for a subsystem

• identify which classes participate

• from sequence diagrams

• add attributes

• look to the domain model for inspiration

• but note that not all classes and attributes from the domain model will be used

• likewise, new classes and attributes will be needed which aren’t in the domain model

Monday, 22 October 12

Page 6: Design Class Diagrams

Example: identify classes & attributes

from Ch 19 Applying UML & Patterns (Larman 2004)

Register

Store

ProductCatalog

SalesLineItem

quantity

Sale

Payment

addressname date

isCompletetime

amountquantity

ProductSpecificationdescriptionpriceitemID

Monday, 22 October 12

Page 7: Design Class Diagrams

...How to Create a DCD...

• add methods

• again from sequence diagrams

• aim for a 1-to-1 match (revise docs in parallel to achieve this)

• expect to identify more methods as you progress (and maybe lose some)

• some points:

• replace create() with actual constructor detail

• leave out simple getters and setters

• e.g leave out getHeight() for the attribute height

• but do include important, derived getters

• e.g. getTotal() for Sale class

• add types

Monday, 22 October 12

Page 8: Design Class Diagrams

Example: add methods & types

from Ch 19 Applying UML & Patterns (Larman 2004)

Monday, 22 October 12

Page 9: Design Class Diagrams

...How to Create a DCD...

• add associations and navigability

• rather like the domain model

• derive from the sequence diagrams

• show when:

• A sends a message to B

• A creates an instance of B

• A needs to maintain a connection to B

• direction of arrow:

• usually the source class has an instance of the target class as an attribute

Monday, 22 October 12

Page 10: Design Class Diagrams

Example: add associations

from Ch 19 Applying UML & Patterns (Larman 2004)

Monday, 22 October 12

Page 11: Design Class Diagrams

...Example expanded

from Ch 19 Applying UML & Patterns (Larman 2004)

Monday, 22 October 12

Page 12: Design Class Diagrams

...How to Create a DCD...

• avoid needless effort!

• add visibilities?

• by convention attributes are private and methods public, so we usually don’t need to represent these

• so just use:

• + for public

• - for private

• when an attribute or method breaks convention

• model user interface classes?

• for sheer practicality do not waste time modelling the huge number of attributes and methods contained within a user interface

• just show it’s a <<user interface>> below its name, and show its association to other classes

Monday, 22 October 12

Page 13: Design Class Diagrams

...How to Create a DCD...

• avoid clutter

• if you try to model every class, complete with attributes and methods in a single diagram it will look like a circuit board

• impressive but impractical

• so it’s better to provide separate diagrams for each class specification, then just show the class names and key attributes in an overall DCD

• so it will look a lot like the domain model

Monday, 22 October 12

Page 14: Design Class Diagrams

Mapping Designs to Code

• an iterative process

• very difficult to design completely on paper before building

• far quicker and easier to get started with the coding and feed back ideas and observations

• context helps:

• as you sit down and code you’ll think of things which need to change with the design

• attributes, methods, classes to add or remove

• types (representations) to change

• associations (and hence attributes) to add

Monday, 22 October 12

Page 15: Design Class Diagrams

Example: design to code

from Ch 20 Applying UML & Patterns (Larman 2004)

1

ProductSpecification

description : Textprice : MoneyitemID : ItemID

SalesLineItem

quantity : Integer

getSubtotal():Money

* Described-by

public class SalesLineItem {

private int quantity;

public SalesLineItem(ProductSpecification spec, int, qty) {...}public Money getSubtotal() {…}…

}

Monday, 22 October 12

Page 16: Design Class Diagrams

Example: adding reference attributes

from Ch 20 Applying UML & Patterns (Larman 2004)

Monday, 22 October 12

Page 17: Design Class Diagrams

Example: using role names

from Ch 20 Applying UML & Patterns (Larman 2004)

Monday, 22 October 12

Page 18: Design Class Diagrams

Order of Implementation

from Ch 20 Applying UML & Patterns (Larman 2004)

• essentially, code the classes with fewest dependencies first

• i.e. least coupled to most coupled

Monday, 22 October 12

Page 19: Design Class Diagrams

Error Handling

• error handling can be modelled in sequence diagrams

• using asynchronous messages and alternative flow

• but for now we’ll skip that

• however, do aim to write code which captures and handles the exceptions listed in the use case descriptions

• expect to find many other possible errors to handle

• a good idea is to build a robust UI which prevents problems before they arise

• for example, if you require the user to enter a 3 digit number, then prevent other symbols and string lengths from being entered

Monday, 22 October 12

Page 20: Design Class Diagrams

To conclude

• we can generate class diagrams alongside sequence diagrams

• we can use them to specify in great detail the classes, methods and attributes which will form the design solution

• but we shouldn’t wait too long to get coding

• some coding now will speed up the analysis and design processes

• when coding:

• build the easiest, least coupled classes first

• a little time spent now forcing user actions to be legal will save a lot of time debugging and error handling later

• avoid clutter:

• as with other diagrams, split these up if need be

Monday, 22 October 12

Page 21: Design Class Diagrams

References & Further Reading

• base structure and figures taken from:

• Applying UML & Patterns (Larman 2004), Chapters 19, 20

• for good coding principles:

• Program Development in Java (Liskov & Guttag)

Monday, 22 October 12