MCS 270 Spring 2014

29
MCS 270 Spring 2014 Object-Oriented Software Development

description

MCS 270 Spring 2014. Object-Oriented Software Development. MCS 270 Object-Oriented Software Development. Today ’ s schedule. Basics of UML Class Diagrams. MCS 270 Object-Oriented Software Development. Unified modeling language (UML). - PowerPoint PPT Presentation

Transcript of MCS 270 Spring 2014

Page 1: MCS 270 Spring 2014

MCS 270 Spring 2014

Object-Oriented Software Development

Page 2: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Today’s schedule

Basics of UML

Class Diagrams

MCS 270 Object-Oriented Software Development

Page 3: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Unified modeling language (UML)1980’s and 90’s - Several competing object-oriented

notations (Babel of notation)

1994 - Rumbaugh (one of our textbook’s authors) and Booch began working at IBM Rational to standardize their notations (OMT and Booch)

Result was Unified Modeling Language (UML)Rights owned by Object Management Group (OMG),

www.omg.org

MCS 270 Object-Oriented Software Development

Page 4: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Collection of notations representing software designs from three points of view:

Class modelState modelInteraction model

Generally need all three models to describe a systemNo single model describes everything in problem

domain

Unified modeling language (UML)

MCS 270 Object-Oriented Software Development

Page 5: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Class model: describes the static structure of objects and relationships in a system

Comprises object and class diagrams

State model: describes the dynamic aspects of objects and the nature of control in a system

Interaction model: describes how objects in a system cooperate to achieve broader results

Unified modeling language (UML)

MCS 270 Object-Oriented Software Development

Page 6: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Class Model

MCS 270 Object-Oriented Software Development

Page 7: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Good Class/Object DesignMeaningful concept or “thing” in an application domain

Has identity that does not encapsulate an object within itself, does not use attribute values of another object

Classes model relationships among things in domain

MCS 270 Object-Oriented Software Development

Page 8: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Good Class/Object DesignWhat is wrong with this

definition of a city class?

MCS 270 Object-Oriented Software Development

CitycityName : Stringpopulation : int

timeZone : StringairportName : StringairportCode : String

Page 9: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Good Class/Object DesignWhat is wrong with this

definition of a city class?

MCS 270 Object-Oriented Software Development

CitycityName : Stringpopulation : int

timeZone : StringairportName : StringairportCode : String

Answer: These attributes “hide” an object (i.e., an airport) that is meaningful in this domain

Page 10: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Question

Why might it be bad to encode one object as a collection of attribute values within another?

MCS 270 Object-Oriented Software Development

Page 11: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Question

Why might it be bad to encode one object as a collection of attribute values within another?

Potential for redundancy/inconsistencysome airports serve multiple citiessome cities served by no airportssome cities served by multiple airports

Operations over Airport objects may not need to know details associated with cities, such as population

MCS 270 Object-Oriented Software Development

Page 12: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Design tip

When designing a class:Apply the identity test to each attribute (does attribute belong

to class?)Never use an attribute to model another object attribute

UML notation helps enforce this discipline

Next: how do we model connections between objects, such as Cities and Airports?

MCS 270 Object-Oriented Software Development

Page 13: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Relationships among objects

Link: Physical or conceptual connection between objects

Most (not all) links relate exactly two objects

Association: Description of a group of links with common structure and semantics

A link is an instance of an association:Links connect objects Association describes set of potential links just as a

class describes a set of potential objects

MCS 270 Object-Oriented Software Development

Page 14: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Examples of links

Houston : City

cityName = “Houston, TX”population = 3000000

HOU : Airport airportCode = “HOU”

airportName = “Hobby”timeZone = “Central”

IAH : Airport airportCode = “IAH”

airportName = “Intercontinental”timeZone = “Central”

Serves

Serves

MCS 270 Object-Oriented Software Development

Page 15: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

City

cityName : Stringpopulation : int

Airport

airportName : StringairportCode : String

timeZone : String

Serves1..* *

association name

multiplicities

Example of an association

MCS 270 Object-Oriented Software Development

Page 16: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Association End Names

Professor University

EmployerEmployee

End Names

WorksFor

Association Name

Association end name defines role of Object The class could play same or different roles in other

associations.Ex: Professor could also be classified as Teacher

MCS 270 Object-Oriented Software Development

Page 17: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Association End Names

Parent Child

Association end name is used for multiple references to same class 

2 *

Person

child *

parent

0..2

Not advisable – only use one class to represent a person

Better

MCS 270 Object-Oriented Software Development

Page 18: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

New abstraction: Generalization (Sub-Super)

Employeefirst_name : Stringlast_name : Stringhire_date : Datedepartment : int

Managerlevel : short

grou

p

*

0..1

This is the “is-a” relationRelates class to one that is “more

general”Open arrow points to Base ClassDerived class inherits all

attributes/operations of parentRelation is anti-symmetric and

transitive

MCS 270 Object-Oriented Software Development

Page 19: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Cheat Sheet

Book – front and back have summary of diagramsHandout – another summary with annotated explanations

MCS 270 Object-Oriented Software Development

Page 20: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Practice

MCS 270 Object-Oriented Software Development

Page 21: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Practice

MCS 270 Object-Oriented Software Development

Page 22: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Practice

Groups of two: Create a class model to describe undirected graphs. An undirected graph consists of edges connecting pairs of vertices.

MCS 270 Object-Oriented Software Development

An undirected graph with 6 nodes and 7 edges

en.wikipedia.org/wiki/Graph_(mathematics)

Page 23: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Practice

Groups of two: Create a class model to describe directed graphs. Here edges are directed from start to end.

MCS 270 Object-Oriented Software Development

A directed graph with 3 nodes and 3 edges

en.wikipedia.org/wiki/Graph_(mathematics)

Page 24: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Why do we use UML?

A Picture = Rich Data Encoding: we think in pictures and can relate info quickly in figures

Creative destruction: Diagrams are easily changed, programs not so much

Employment: UML is used in the workplace

MCS 270 Object-Oriented Software Development

Page 25: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Design tips

Use a UML diagram when:Brainstorming ideasSharing ideas with teamSaving blueprint of idea You can't find a bug in codeTeacher/manager requires it

MCS 270 Object-Oriented Software Development

Page 26: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Design tips

Keep it simple – only use UML when you need toManage Complexity: Draw diagrams that give most

value, e.g. do not draw a diagram for system with one or two classes. Likewise, do not put in all detail in first iteration.

MCS 270 Object-Oriented Software Development

Page 27: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Design tips

Good design practice: switch back and forth between what is said in the diagram and what is allowable in the code

Good Programming Practice: Plan code using links/associations rather than Java classes and methods.

This is good training in abstraction!

MCS 270 Object-Oriented Software Development

Page 28: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Summary

UML provides notations for modeling objects, classes, and associations of an application domainDiagrams represent models:

more abstract than codeuseful for explanation/documentation

MCS 270 Object-Oriented Software Development

Page 29: MCS 270 Spring 2014

GUSTAVUS ADOLPHUS COLLEGE gustavus.edu

Assignments

Friday - Read Chapter 5 in text.Homework: Chapter 3. Exercises 3.8, 3.11, and 3.12 Due Monday, Feb. 17

MCS 270 Object-Oriented Software Development