27
Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault Computer Science McGill University Winter 2004 February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 1

Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Crash course in UML and the Project

Comp-303 : Programming Techniques

Lecture 4

Alexandre DenaultComputer ScienceMcGill University

Winter 2004

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 1

Page 2: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Announcements

• Requirement.&Spec. Document due in 2 weeks (in class).

• Assignment 1 will be handed out next class.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 2

Page 3: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Last lecture . . .

• Variables, Objects and Primitives

• Object variables are called references

• Objects can be mutable or immutable

• Java is strongly typed and type-safe

• Java provides automatic garbage collection

• All objects are subtypes of Object and understand toString()equals()

• Primitive types are converted to other types

• All types can be cast to other types (no conversion)

• Packages provide encapsulation and naming scope

• java.util provides Vector

• Executions starts at main() method

• Tool of the day: CVS

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 3

Page 4: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

What is UML?

• UML is a visual language used to create models of programs ormodules.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 4

Page 5: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Why learn UML?

– To communicate with others.

– To give a graphical representations to your ideas.

– To avoid confusion in design/communication

– Because you might need it for the project

– Because it looks really cool in your C.V.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 5

Page 6: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Why use UML?

• UML has guidelines for drawing several types of diagrams:

– Use Case Diagrams

– Activity Diagrams

– Interaction Diagrams

– Sequence Diagrams

– Class Diagrams

– State Diagrams

– Deployment Diagrams

• Don’t worry, we won’t need all of these.

• We will mostly work with Class Diagrams, State Diagrams andSequence Diagrams.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 6

Page 7: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Good references on UML

• Fundamentals of Object-Oriented Design in UML; MeilirPage-Jones; Addison-Wesley; 2000

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 7

Page 8: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Drawing UML with Dia

• Dia is a gtk+ based diagram creation program released underthe GPL license.

http://www.lysator.liu.se/~alla/dia/

• Dia is a free program that allows you to draw your UMLdiagrams.

• All the UML diagrams in these notes were created with Dia.

• Dia is available on Linux and Windows.

• Dia2code allows you to generate code from your UML classdiagrams.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 8

Page 9: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Class Diagrams

• This is the type of diagram we will use most often in class.

• It allows you illustrate both the structure and the relationshipsof your classes.

• There are 3 kinds of relationships between classes:

– Hierarchy

– Contains

– Uses

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 9

Page 10: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Classes in a class diagram

Three elements can be represented in a class box:

• name

• members / properties

• methods / functions

Book

-chapterList: Vector

+title: String

+author: String

+read(): String

+write(chapter:int,text:String): void

• The ”+” symbol indicates a public member

• The ”-” symbol indicates a private member

• If the ”#” symbol was used, it would indicate a protected member

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 10

Page 11: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Hierarchy Relation

• The first type of relationship is hierarchy, also known asinheritance.

Book

Manual Art Book

• An arrow is used to illustrate the inheritance relationship.

• In this diagram, Manual and Art Book are a subtype of Book.

• If the name of the class Book were written in italic, this would

indicate the class is abstract.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 11

Page 12: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Contains Relation

• The second type of relationship is contains, also known ascomposition or aggregation.

Book

Store

Page

• A full diamond indicates a composition relation. This means Class

book cannot exist without class Page.

• An empty (white) diamond indicates an aggregation relation. This

means class Book can be contained in class Store. However, class

Store does not need class Book to exist.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 12

Page 13: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Uses Relation

• The last type of relationship is uses.

BookStudent

• This diagram shows that the class Student uses the class Book.

• The class Student does not define itself with the class Book, it just

uses it.

• Notice that the arrow seems to be doing in the wrong direction.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 13

Page 14: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Cardinality of a Relation

• Sometimes, it becomes important to define the cardinality of arelation.

• In other words, we want to count the number of object thatcan be found on each side of the relation.

Book

Page1..*

0..1

• In this example, we can have either no book or one book (0..1 ).

• We also define that our scenario requires at least one page (1..* ) .

• We can’t define a scenario with zero pages because that would

violate the composition rule with class Book.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 14

Page 15: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Adding Notes

• UML is not flexible enough to describe all scenarios.

• To describe complicated scenarios, designers can use notes toannotate their designs.

Book

Page1..*

0..1

Books should

have an even

number pages.

• In this example, the design must add a constraint forcing the

number of pages to be even.

• UML provides no primitive or easy solution.

• However, the designer can simply add the constraint as a note in the

diagram.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 15

Page 16: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Combining all the previous elements

• We can combine all the previous elements to produce completeUML diagrams.

Book

Page1..*

0..1

ManualNovel

Diagram1..*

0..1

Store

Customer

0..*

0..1

Stores can hold other

types of products.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 16

Page 17: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

State Diagram

• State diagrams are used to describe the various state aprogram can reach.

• This diagram shows the various states for a typical lamp.

Light is Off

Light is On

User plugsin lamp

User presson/off button

User presson/off button

User unplugs lamp

Start State End State State Event / Message

User unplugs lamp

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 17

Page 18: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Sequence Diagram

• Sequence Diagrams are used to show the order in which objectsare instantiated and used (object’s life).

• This examples shows a program finding a file and opening it.

Main Finder Reader

find file ()

file location

read file ()

file content

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 18

Page 19: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Hints for the project

• You should have a team by now. If you don’t come and see meafter class.

• Choose a project leader (not a dictator).

• You’re team should be talking at least a hour each week.

• Have some technique to control your sources (CVS ?).

• Assign modules/packages depending on people’s skill.

• Start early. There is not penalty for finishing a project early.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 19

Page 20: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Requirements & Specifications

• Before attempting any large scale project, it is imperative tospend some time writing out the Requirements &Specifications.

• Requirements can be described a requests for a particular pieceof software.

• Specifications can be described as the proposed solutions forthe requirements.

• Validating the Requirements & Specifications with the clientbefore starting a project is always a smart idea.

• Requirement Analysis and Specifications are covered latter inthe class, so I’ll be very brief on this topic.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 20

Page 21: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Requirements

• As mentioned before, requirements are the requests for aparticular piece of software.

• For the project, the main requirements are defined by theprofessor ( programming language, size of project, etc ).

• However, the main requirements are too loss to properly defineyour project.

• Once you have chosen your project, it is up to your team todefine your requirements / goals for this project.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 21

Page 22: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Specifications

• Specifications are proposed solutions to clearly stated andapproved requirements.

• For the projects, your initial specifications should be a generaloverview of how your project will work and how you will designyour module.

• Your specifications should (at a minimum) explain the fourfollowing concepts:

– Concept : What does it do?

– Appearance : What does it look like?

– Controls : How can I use it?

– Behavior : How does it work?

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 22

Page 23: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Specifications (cont.)

• Your specification should include a few class diagrams ( doesn’tneed to have properties and method ).

• Specifications are not binding:

– It is normal to sometime deviate from the initialspecifications.

– However, when deviating from the initial specifications, it isimportant to document the changes.

– Too many deviations from the initial specifications is a clearindication of a design flaw in the specification.

– In a business context, variation on the initial specification issubject to client approval.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 23

Page 24: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Example of Requirement.&Spec. Document

This example show how you could format your document.

• Front page

• Table of content

• Requirements

• Specifications

– Concept

– Appearance

– Control

– Behavior

– Package A (class diag?)– Package B (class diag?)– Package C (class diag?)

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 24

Page 25: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

More hints for the project

• Each student is expected to produce his/her package.

• Try to find a project idea that you can easily split into 3 or 4packages.

• Simplify your project using modularity.

– Your team only needs to agree on the public members ofyour package.

– Everything private to your package is only your concern.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 25

Page 26: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Summary

• What is UML and why learn it?

• References and Tools for UML

• Class diagrams

– Elements of a class

– Relations ( Hierarchy, Contains, Uses )

– Cardinality

– Notes

– Putting it all together

• State diagrams

• Sequence diagrams

• Requirements & Specifications

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 26

Page 27: Crash course in UML and the Projectadenau/teaching/cs303/lecture4.pdf · 2004-02-16 · Crash course in UML and the Project Comp-303 : Programming Techniques Lecture 4 Alexandre Denault

Tool of the day: Labyrinth

• Java reproduction of the Labyrinth game.

• Has the following features:

– Client/Server model

– Gui Interface

– Multiplayer over Internet

– Computer playing with 8 A.I. personality

• Too complex for 1 student to complete.

• Ideal for a group of 4 students.

February 16, 2004 Lecture 4 – Comp 303 : Programming Techniques Page 27