Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005...

26
Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, In Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800 Kannan Mohan Department of CIS Zicklin School of Business, Baruch College

Transcript of Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005...

Page 1: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Structural Modeling: Class Diagrams

Copyright © 2009 John Wiley & Sons, Inc.Copyright © 2005 Pearson EducationCopyright © 2009 Kannan Mohan

CIS 4800Kannan Mohan

Department of CISZicklin School of Business, Baruch College

Page 2: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Classes, Attributes, & Operations

• ClassesTemplates for instances of people, places, or things

• AttributesProperties that describe the state of an instance of a class (an object)

• OperationsActions or functions that a class can perform

Page 3: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Rules of Thumb• Limited responsibilities and collaboration

– About 3 to 5 responsibilities

• No class stands alone

• Watch out for –– Too many small classes or too few large classes

– Deep inheritance trees – 3 or more levels

– Functoids – Too many classes with ‘DoIt’ methods – normal procedural function disguised as a class

– Omnipotent classes – such as ‘system’ or ‘controller’

Page 4: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Finding Analysis Classes

• Noun/verb analysis on documents

• CRC card analysis

Page 5: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Front-Side of a CRC Card

Page 6: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Back-Side of a CRC Card

Page 7: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

More Elements of Class Diagrams

Page 8: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Association Example

Page 9: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Multiplicity

• How many instances of one class may relate to a single instance of an associated class at any point in time?

• 1..1, 1..n, 0..n, etc.

Page 10: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Multiplicity

• Company employs many persons

• A person is employed by one company

• Are these reasonable constraints? Depends on the system and requirements

• Documents business rules

Page 11: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Navigability

• Messages sent in the direction of the arrow

• Company sends messages to Person

Company Person1 1..*

employs

Order Product1..* 1..*

Not navigableA Product object does not store a list of

Orders

An Order object stores a list of ProductsNavigable

source target

navigability

Page 12: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Association Class

Company Person1..* 1..*

employs

Company Person1..* 1..*

Job

salary:double

the association class consists of the class, the association and the dashed line

association class

Company PersonJob

salary:double

** 11

Page 13: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Association Class Example

CourseEnrollment

Grade

0..*

1..1

Course

CourseNumberTitleDescriptionCreditHours

0..*

Student

StudentIDNameMajor

Register( )PayFees( )

0..*

CourseSection

SectionNumberStartTimeRoomNumber

0..*

1..1

0..*0..*

offered as

registers

Page 14: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Reflexive/Recursive Associations

• A class is associated with itself

• Manager/Subordinate – “Role” names

Employee

NameAddress

1..1

1..*

Manager

Subordinate

1..1

1..*

Works for

Page 15: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Aggregation• Part-whole’ relationship

– Engine is ‘a part of’ car

– Chassis is ‘a part of’ car

• A special form of association

• Composition– Parts live inside the whole and are destroyed if the whole is

destroyed

• Shared aggregation– Parts can become parts of different wholes (Team - persons)

Page 16: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Generalization / Specialization

Payment

AmountDate

CreditCardPayment

CreditCardNumberExpiryDate

CheckPayment

CheckNumberRoutingNumberAccountNumber

CashPayment

Denomination

Page 17: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Abstract Classes• Methods are not implemented, only signatures are

specified

• They are implemented in specialized sub-classes

• Cannot create instances of abstract classes

• Those classes to which you can create instances of, are called as concrete classes

Shape

Draw( )

Polygon Circle Ellipse

• Draw method does not have an implementation in the super class

• Why do we need this? Interface uniformity among sub-classes

Page 18: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Levels of Abstraction

• What is wrong with this model?

Vehicle

JaguarXJS Truck

Page 19: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Polymorphism

• Many forms

• A polymorphic operation has many implementations

• What about overriding concrete operations?

Shape

draw( g : Graphics )getArea() : intgetBoundingArea() : int

Square Circle

draw( g : Graphics )getArea() : int

draw( g : Graphics )getArea() : int

polymorphicoperations

concrete subclasses

abstract superclass

Canvas

*

1

A Canvas object has a collection of Shape objects where each Shape may be a Square or a Circle

shapes

Page 20: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Multiple Inheritance• In single inheritance we discriminated based on

one attribute (say type of transaction – cash, credit and check)

• In Multiple inheritance we discriminate based on more than one attribute

SalesStaff Manager

SalesManager

• Sales Manager inherits properties and methods from both Sales staff and Manager

Page 21: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Best Practices• Favor object composition over class

inheritance– White box reuse/inheritance vs. black box

reuse/composition

– Encapsulation and interfaces respected in composition

• Design to an interface, not to an implementation– Commit to an interface defined by the abstract class

(Gamma et al, 1994)

Page 22: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Comparing Traditional and OO:Traditional Viewpoint

• Focus on procedures• Functionality is vested in procedures

(modules)• Data and Procedures separated• Data exist to be operated upon by procedures• Procedures know about the structure of data• Responsibility of what can be done to a piece

of data is implicit

Page 23: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Traditional View – An Example

If CUST.credit > CUST.balanceORD.status = “Approved”

elseORD.status=“Not Approved”

CUST ORD

PRODUCT DEPT

Procedure

Data

Page 24: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Object-Oriented View – An Example

CUSTOMER

If Balance < LimitStatus = “Good”

ElseStatus = “bad”

ORDER

If Answer= “Good”Self.status= “Approved”

ElseSelf.status=“Not Approved”

What is your credit status?

Good

Page 25: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

How many classes?

Page 26: Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.

Summary• How do we identify classes, relationships, attributes,

and behavior for a given case?

• What are different types of relationships?

• What is multiplicity and how do we specify it?

• How do we create a domain class diagram using a CASE tool?

• Explain the following:– Class, object, message, method

• What is abstraction?

• What is encapsulation and why is it important?