33
Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email: [email protected]

CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Designing for Visibility &

Mapping to Code CSSE 574: Session 4, Part 3

Steve Chenoweth

Phone: Office (812) 877-8974

Cell (937) 657-3885 Email:

[email protected]

Page 3: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Visibility

An object B is visible to an object A if A

can send a message to B

Related to, but not the same as:

Scope

Access restrictions (public, private, etc.)

What are four common ways that

B can be visible to A?

Page 4: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Attribute Visibility

Object A has attribute visibility to object B

if … A has an attribute that stores B

Quite permanent

Most common

: RegisterenterItem

(itemID, quantity)

: ProductCatalog

desc = getProductDesc( itemID )

public void enterItem( itemID, qty )

{

...

desc = catalog.getProductDesc(itemID)

...

}

class Register

{

...

private ProductCatalog catalog;

...

}

Page 5: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Parameter Visibility

Object A has parameter visibility to object B if …

B is passed in as an argument to a method of A

Not permanent, disappears when method ends

Second most common

Methods often convert parameter visibility to attribute visibility

2: makeLineItem(desc, qty)enterItem(id, qty)

1: desc = getProductDesc(id)

2.1: create(desc, qty)

:Register :Sale

:Product

Catalog

sl : SalesLineItemmakeLineItem(ProductDescription desc, int qty)

{

...

sl = new SalesLineItem(desc, qty);

...

}

Register has method-

return visibility to

ProductDescription

Page 6: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Local Visibility

Object A has local visibility to object B if

B is referenced by a local variable in a method

of A

Not permanent, disappears when leaving

variable‟s scope

Third most common

Methods often convert local visibility to

attribute visibility

Page 7: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Global Visibility

Object A has global visibility to object B if …

B is stored in a global variable accessible from A

Very permanent

Least common (but highest coupling risk)

Page 8: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Cartoon of the Day

Used with permission. http://notinventedhe.re/on/2009-9-23

Page 9: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Before we get into Code

Created Domain Model from requirements

and use cases

Used System Sequence Dia-

grams to identify system

operations

Clarified system operations with

Operation Contracts

Assigned “doing” responsibilities with

Interaction Diagrams (Communication and

Sequence Diagrams)

Assigned “knowing” responsibilities with

Design Class Diagrams

Depending on the

system, many of

these steps might

just be sketches!

Page 10: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

10

Moving from Design to Code

Design provides starting point for Coding

DCDs contain class or interface names,

superclasses, method signatures, and simple

attributes

Two primary tasks 1. Define classes & interfaces

2. Define methods

Elaborate from associations to add reference attributes

Page 11: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

11

Example: Defining Register Class

ProductCatalog

...

getProductDesc(...)

Sale

isComplete : Boolean

time : DateTime

becomeComplete()

makeLineItem(...)

makePayment(...)

getTotal()

Register

...

endSale()

enterItem(id: ItemID, qty : Integer)

makeNewSale()

makePayment(cashTendered : Money)

public class Register

{

private ProductCatalog catalog;

private Sale currentSale;

public Register(ProductCatalog pc) {...}

public void endSale() {...}

public void enterItem(ItemID id, int qty) {...}

public void makeNewSale() {...}

public void makePayment(Money cashTendered) {...}

}

1

1

catalog

currentSale

Page 12: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Create Class Definitions from DCDs

Page 13: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Create Methods from Interaction Diagrams

Page 14: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

Collections

public class Sale {

private List<SalesLineItem> lineItems = new ArrayList<SalesLineItem>();

}

Guideline: If an object implements an interface,

use the interface type for the variable.

Page 15: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

What Order? Typically, least coupled

to most coupled. Why?

Page 16: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

16

Parameter Visibility Converted to

Attribute Visibility

2: makeLineItem(desc, qty)enterItem(id, qty)

2: desc = getProductDesc(id)

2.1: create(desc, qty)

:Register :Sale

:Product

Catalog

sl : SalesLineItem

// initializing method (e.g., a Java constructor)

SalesLineItem(ProductDescription desc, int qty)

{

...

description = desc; // parameter to attribute visibility

...

}

SalesLineItem has association with ProductDescription

Page 17: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

17

Local Visibility: Register Assigns Method

Return to A Local Variable

: RegisterenterItem

(itemID, quantity)

: ProductCatalog

desc = getProductDesc( itemID )

enterItem(id, qty)

{

...

// local visibility via assignment of returning object

ProductDescription desc = catalog.getProductDes(id);

...

}

Register has local visibility to ProductDescription

Page 18: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

18

Mapping from Class Diagram to Code

public class SalesLineItem

{

private int quantity;

private ProductDescription description;

public SalesLineItem(ProductDescription desc, int qty) { ... }

public Money getSubtotal() { ... }

}

SalesLineItem

quantity : Integer

getSubtotal() : Money

ProductDescription

description : Text

price : Money

itemID : ItemID

...

1

description

Constructor

usually not

Shown on

diagram

Page 19: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

19

Reference Attributes and Role Names

Reference Attributes are indicated by associations

and navigability in a class diagram

Example: A product specification reference on a Sales

Line Item

productSpec

Described-by SalesLineItem

productSpec

Product

Specification

Description

Page 20: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

20

Consider enterItem() System Operation

2: makeLineItem(desc, qty)enterItem(id, qty)

1: desc = getProductDesc(id)2.1: create(desc, qty)

1.1: desc = get(id)

:Register :Sale

:Product

Catalog

sl: SalesLineItem

lineItems :

List<SalesLineItem>: Map<ProductDescription>

2.2: add(sl)

Page 21: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

21

Dynamic View: enterItem() Communication Diagram

2: makeLineItem(desc, qty)enterItem(id, qty)

1: desc = getProductDesc(id)2.1: create(desc, qty)

1.1: desc = get(id)

:Register :Sale

:Product

Catalog

sl: SalesLineItem

lineItems :

List<SalesLineItem>: Map<ProductDescription>

2.2: add(sl)

by Expert

by Controllerby Creator

add the newly created

SalesLineItem instance to the List

Why choice of Map for ProdDesc and List for SaleLineItems?

Page 22: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

22

Mapping Messages to Methods

2: makeLineItem(desc, qty)enterItem(id, qty)

1: desc := getProductDescription(id)

:Register :Sale

:Product

Catalog

{

ProductDescription desc = catalog.ProductDescription(id);

currentSale.makeLineItem(desc, qty);

}

Each message maps to a method call within enterItem()

Page 23: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

23

Adding Collection from 1 to Many Association

SalesLineItem

quantity : Integer

getSubtotal()

1..*

Sale

isComplete : Boolean

time : DateTime

becomeComplete()

makeLineItem()

makePayment()

getTtotal()

public class Sale

{

...

private List lineItems = new ArrayList();

}

A collection class is necessary to

maintain attribute visibility to all the

SalesLineItems.

lineItems

Declared as „List‟ not

ArrayList! Interface type

Is more generic

Page 24: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

24

Implementing Methods from IDs

{

lineItems.add( new SalesLineItem(desc, qty) );

}

2: makeLineItem(desc, qty)enterItem(id, qty)

2.1: create(desc, qty)

:Register :Sale

sl: SalesLineItemlineItems :

List<SalesLineItem>

2.2: add(sl)

Sale.makeLineItem(), part of the enterItem()

interaction diagram

Notice how simply the method can be implemented!

Page 25: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

25

Implementing Classes in Order

SalesLineItem

quantity : Integer

getSubtotal()

ProductCatalog

...

getProductDesc(...)

ProductDescription

description : Text

price : Money

itemID : ItemID

...

Store

address : Address

name : Text

addSale(...)

Payment

amount : Money

...

1..*

1..*

Register

...

endSale()

enterItem(...)

makeNewSale()

makePayment(...)

Sale

isComplete : Boolean

time : DateTime

becomeComplete()

makeLineItem(...)

makePayment(...)

getTotal()

...

1

1

1

1

1

1

*1

23

4

56

7

Page 26: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

26

Working Example: PM

Page 27: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

27

PM: Use Case Diagram

Page 28: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

28

PM: Class Diagram

Page 29: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

29

PM: Class to Code

class WorkPackage;

class Project;

class Activity;

class Task;

class WorkProduct;

class Resource;

class Skill;

class ResourceXSkill;

Page 30: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

30

PM: Class to Code

class WorkPackage

{ // Details omitted };

class Project : public WorkPackage

{ private: CollectionByVal<Activity> theActivity; };

class Activity : public WorkPackage

{ private: Project *theProject;

CollectionByVal<Task> theTask;

CollectionByRef<WorkProduct>

theWorkProduct; };

Page 31: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

31

PM: DCD Mapping

Page 32: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

32

PM: DCD Code

class Project

{ private:

char *Name;

char *Descr;

Date StartDate;

static int NumberOfProjects;

public:

Project (char *Name);

Project (void); ~Project (void);

char *getName (void);

void setName (char *theName);

void setDescr (char *Descr);

char *getDescr (void);

void setStartDate (Date

theStartDate);

Date getStartDate (void);

void addActivity (const Activity &theActivity);

CollectionByRef<Activity> getAllAcitivities (void);

static int getNumberOfProjects (void);

void save (void);

void load (char *Name);

protected:

bool hasActivities (void); };

int Project::NumberOfProjects = 0;

Page 33: CSSE 574: Session 4, Part 3 · Designing for Visibility & Mapping to Code CSSE 574: Session 4, Part 3 Steve Chenoweth Phone: Office (812) 877-8974 Cell (937) 657-3885 Email:

33

PM: Sequence Diagram