UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

Similar documents
UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

Object Fundamentals. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

Credit where Credit is Due. Lecture 4: Fundamentals of Object Technology. Goals for this Lecture. Real-World Objects

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 4 09/03/2009

Software Service Engineering

SOFTWARE DESIGN COSC 4353 / Dr. Raj Singh

Introduction to Software Engineering. 5. Modeling Objects and Classes

UML Modeling I. Instructor: Yongjie Zheng September 3, CS 490MT/5555 Software Methods and Tools

Ready for the Real World. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 8 09/18/2007

Introduction to Software Engineering. 5. Modeling Objects and Classes

INF 111 / CSE 121: Announcements Quiz #3- Thursday What will it cover?

Unified Modeling Language

FACADE & ADAPTER CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 7 09/13/2011

UML Primer. -Elango Sundaram

What s Next. INF 117 Project in Software Engineering. Lecture Notes -Spring Quarter, Michele Rousseau Set 6 System Architecture, UML

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

2 UML for OOAD. 2.1 What is UML? 2.2 Classes in UML 2.3 Relations in UML 2.4 Static and Dynamic Design with UML. UML for OOAD Stefan Kluth 1

Ready for the Real World. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 8 09/17/2009

Good-Enough Design. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 11 02/17/2009

Software Engineering Lab Manual

administrivia today UML start design patterns Tuesday, September 28, 2010

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Course "Softwaretechnik" Book Chapter 2 Modeling with UML

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Object-Oriented Software Engineering Practical Software Development using UML and Java

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

1 Reference Material for these slides is taken from many UML reference books. However, the two I most often used are: UML Explained, by Kendall Scott,

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Inheritance and Encapsulation. Amit Gupta

Inheritance and Polymorphism

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

MechEng SE3 Lecture 7 Domain Modelling

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

Programming II (CS300)

Introduction to OO Concepts

Introducing the UML Eng. Mohammed T. Abo Alroos

SHRI ANGALAMMAN COLLEGE OF ENGINEERING & TECHNOLOGY (An ISO 9001:2008 Certified Institution) SIRUGANOOR,TRICHY

Object-Oriented Introduction

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

An Introduction To Object Modeling System Concept for Object Modeling The Overall View Components of UML Diagram

Object-Oriented Programming More Inheritance

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Programming II (CS300)

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Instance Members and Static Members

OBJECT ORİENTATİON ENCAPSULATİON

LABORATORY 1 REVISION

Java Object Oriented Design. CSC207 Fall 2014

Originality is Overrated: OO Design Principles

Object-Oriented ProgrammingInheritance & Polymorphism

COP 3330 Final Exam Review

Java Session. Day 2. Reference: Head First Java

Inheritance (Outsource: )

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Chapter 5 Object-Oriented Programming

ITI Introduction to Computing II

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

On behalf of ASE crew, I welcome you onboard. We will cover this journey in roughly 50 minutes.

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

CSE 403: Software Engineering, Spring courses.cs.washington.edu/courses/cse403/15sp/ UML Class Diagrams. Emina Torlak

Object- Oriented Design with UML and Java Part I: Fundamentals

Inheritance & Polymorphism

OOAD - OBJECT MODEL. The concepts of objects and classes are intrinsically linked with each other and form the foundation of object oriented paradigm.

Unified Modeling Language (UML)

Design and UML Class Diagrams

Unified Modeling Language (UML) Class Diagram

CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Reviewing OO Concepts

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 5: Modelling with Classes

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Cmpt 135 Assignment 2: Solutions and Marking Rubric Feb 22 nd 2016 Due: Mar 4th 11:59pm

The major elements of the object-oriented model

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Inheritance, Polymorphism, and Interfaces

OVERRIDING. 7/11/2015 Budditha Hettige 82

Ingegneria del Software Corso di Laurea in Informatica per il Management. Introduction to UML

Inheritance & Polymorphism. Object-Oriented Programming

ITI Introduction to Computing II

Introduction. Chapter 1. What Is Visual Modeling? The Triangle for Success. The Role of Notation. History of the UML. The Role of Process

Objectives. Explain the purpose and objectives of objectoriented. Develop design class diagrams

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Course 3 7 March

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

Transcription:

UML & OO Fundamentals CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012 1

Goals of the Lecture Review the material in Chapter 2 of the Textbook Cover key parts of the UML notation Demonstrate the ways in which I think the UML is useful Give you a chance to apply the notation yourself to several examples Warning: I repeat important information several times in this lecture this is a hint to the future you when you are studying for the midterm. 2

Reminder CS Ice Cream Social -- Thursday, 3:30 PM to 4:30 PM CSUAC Welcome Back Event -- Thursday, 5:00 PM to 7:00 PM CODEBREAKER: Alan Turing Documentary -- Friday, Math 100, 6 PM 3

UML UML is short for Unified Modeling Language The UML defines a standard set of notations for use in modeling objectoriented systems Throughout the semester we will encounter UML in the form of class diagrams sequence/collaboration diagrams state diagrams activity diagrams, use case diagrams, and more 4

(Very) Brief History of the UML In the 80s and early 90s, there were multiple OO A&D approaches (each with their own notation) available Three of the most popular approaches came from James Rumbaugh: OMT (Object Modeling Technique) Ivar Jacobson: Wrote OO Software Engineering book Grady Booch: Booch method of OO A&D In the mid-90 s all three were hired by Rational and together developed the UML; known collectively as the three amigos 5

Big Picture View of OO Paradigm OO techniques view software systems as networks of communicating objects Each object is an instance of a class All objects of a class share similar features attributes methods Classes can be specialized by subclasses Objects communicate by sending messages 6

Objects (I) Objects are instances of classes They have state (attributes) and exhibit behavior (methods) We would like objects to be highly cohesive have a single purpose; make use of all features loosely coupled be dependent on only a few other classes 7

Objects (II) Objects interact by sending messages Object A sends a message to Object B to ask it to perform a task When done, B may pass a value back to A Sometimes A == B i.e., an object can send a message to itself 8

Objects (III) Sometimes messages can be rerouted invoking a method defined in class A may in fact invoke an overridden version of that method in subclass B a method of class B may in turn invoke messages on its superclass that are then handled by overridden methods from lower in the hierarchy The fact that messages (dynamic) can be rerouted distinguishes them from procedure calls (static) in non-oo languages 9

Objects (IV) In response to a message, an object may update its internal state return a value from its internal state perform a calculation based on its state and return the calculated value create a new object (or set of objects) delegate part or all of the task to some other object i.e. they can do pretty much anything in response to a message 10

Objects (V) As a result, objects can be viewed as members of multiple object networks Object networks are also called collaborations Objects in an collaboration work together to perform a task for their host application 11

Objects (VI) UML notation Objects are drawn as rectangles with their names and types (class names) underlined Ken : Person The name of an object is optional. The type is required : Person Note: The colon is not optional. 12

Objects (VII) Objects that work together have lines drawn between them This connection has many names object reference reference link Messages are sent across links Links are instances of associations (see slide 31) 13

sit() Skippy: Dog Ken: Person feed() 3 objects; 3 classes; 2 links; 2 messages Felix: Cat You can think of the names as the variables that a program uses to keep track of the three objects 14

Classes (I) A class is a blueprint for an object The blueprint specifies a class s attributes and methods attributes are things an object of that class knows methods are things an object of that class does An object is instantiated (created) from the description provided by its class Thus, objects are often called instances 15

Classes (II) An object of a class has its own values for the attributes of its class For instance, two objects of the Person class can have different values for the name attribute Objects share the implementation of a class s methods and thus behave similarly i.e. Objects A and B of type Person each share the same implementation of the sleep() method 16

Classes (III) Classes can define class-based (a.k.a. static) attributes and methods A static attribute is shared among all of a class s objects That is, all objects of that class can read/write the static attribute A static method is a method defined on the Class itself; as such, it does not have to be accessed via an object; you can invoke static methods directly on the class itself In Lecture 2 s Java code: String.format() was an example of a static method 17

Classes (IV) Classes in UML appear as rectangles with multiple sections The first section contains its name (defines a type) The second section contains the class s attributes The third section contains the class s methods Song artist title play() 18

Class Diagrams, 2nd Example Name Atts Methods Airplane speed: int getspeed(): int setspeed(int) All parts are optional except the class name A class is represented as a rectangle This rectangle says that there is a class called Airplane that could potentially have many instances, each with its own speed variable and methods to access it 19

Translation to Code Class diagrams can be translated into code straightforwardly Define the class with the specified name Define specified attributes (assume private access) Define specified method skeletons (assume public) May have to deal with unspecified information Types are optional in class diagrams Class diagrams typically do not specify constructors just the class s public interface 20

Airplane in Java 1 public class Airplane { Using Airplane Airplane a = new Airplane(5); a.setspeed(10); System.out.println( + a.getspeed()); 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 } private int speed; public Airplane(int speed) { this.speed = speed; } public int getspeed() { return speed; } public void setspeed(int speed) { this.speed = speed; } 21

Relationships Between Classes Classes can be related in a variety of ways Inheritance Association Multiplicity Whole-Part (Aggregation and Composition) Qualification Interfaces 22

Relationships: Inheritance One class can extend another notation: a white triangle points to the superclass the subclass can add attributes Hippo adds submerged as new state the subclass can add behaviors or override existing ones Hippo is overriding makenoise() and eat() and adding submerge() food type location makenoise() eat() roam() Animal Hippo submerged: boolean makenoise() eat() submerge() 23

Inheritance Inheritance lets you build classes based on other classes and avoid duplicating code Here, Jet builds off the basics that Airplane provides Airplane speed: int getspeed(): int setspeed(int) Jet MULTIPLIER: int accelerate() 24

Inheriting From Airplane (in Java) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class Jet extends Airplane { private static final int MULTIPLIER = 2; public Jet(int id, int speed) { super(id, speed); } public void setspeed(int speed) { super.setspeed(speed * MULTIPLIER); } public void accelerate() { super.setspeed(getspeed() * 2); } } Note: extends keyword indicates inheritance super() and super keyword is used to refer to superclass No need to define getspeed() method; its inherited! setspeed() method overrides behavior of setspeed() in Airplane 25

Polymorphism: Many Forms Being able to refer to different derivations of a class in the same way, Implication: both of these are legal statements Airplane plane = new Airplane(); Airplane plane = new Jet();...but getting the behavior appropriate to the derived class being referred to when I invoke setspeed() on the second plane variable above, I will get Jet s method, not Airplane s method 26

Encapsulation Encapsulation lets you hide data and algorithms in one class from the rest of your application limit the ability for other parts of your code to access that information protect information in your objects from being used incorrectly 27

Encapsulation Example The speed instance variable is private in Airplane. That means that Jet doesn t have direct access to it. Nor does any client of Airplane or Jet objects Imagine if we changed speed s visibility to public The encapsulation of Jet s setspeed() method would be destroyed 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Airplane... public void setspeed(int speed) { this.speed = speed; }... Jet... public void setspeed(int speed) { super.setspeed(speed * MULTIPLIER); }... 28

Reminder: Abstraction Abstraction is distinct from encapsulation It answers the questions What features does a class provide to its users? What services can it perform? Abstraction is the MOST IMPORTANT concern in A&D! The choices you make in defining the abstractions of your system will live with you for a LONG time 29

The Difference Illustrated The getspeed() and setspeed() methods represent Airplane s abstraction Of all the possible things that we can model about airplanes, we choose just to model speed Making the speed attribute private is an example of encapsulation; if we choose to use a linked list to keep track of the history of the airplane s speed, we are free to do so 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Airplane { private int speed; public Airplane(int speed) { this.speed = speed; } public int getspeed() { return speed; } public void setspeed(int speed) { this.speed = speed; } } 30

Relationships: Association One class can reference another (a.k.a. association) notation: straight line ViewController View This (particular) notation is a graphical shorthand that each class contains an attribute whose type is the other class ViewController view : View View controller : ViewController 31

Roles Roles can be assigned to the classes that take part in an association Person defendant Lawsuit plaintiff Person Here, a simplified model of a lawsuit might have a lawsuit object that has relationships to two people, one person playing the role of the defendant and the other playing the role of the plaintiff Typically, this is implemented via plaintiff and defendant instance variables inside of the Lawsuit class 32

Labels Associations can also be labelled in order to convey semantic meaning to the readers of the UML diagram Professor Instructs Course In addition to roles and labels, associations can also have multiplicity annotations Multiplicity indicates how many instances of a class participate in an association 33

Multiplicity Associations can indicate the number of instances involved in the relationship this is known as multiplicity An association with no markings is one to one An association can also indicate directionality if so, it indicates that the knowledge of the relationship is not bidirectional Examples on next slide 34

Multiplicity Examples A B One B with each A; one A with each B A 1 1 B Same as above A A 1 * Zero or more Bs with each B A; one A with each B * * Zero or more Bs with each B A; ditto As with each B A 1 2..5 B Two to Five Bs with each A; one A with each B A * B Zero or more Bs with each A; B knows nothing about A 35

Multiplicity Example A 1 2..5 B :A :A :B :B :B :B :B 36

Self Association * Person parent-of Ken : Person Manny: Person Moe: Person Jack: Person 37

Relationships: whole-part Associations can also convey semantic information about themselves In particular, aggregations indicate that one object contains a set of other objects think of it as a whole-part relationship between a class representing a group of components a class representing the components Notation: aggregation is indicated with a white diamond attached to the class playing the container role 38

Example: Aggregation Aggregation Crate Bottle Composition Book Section * Chapter Composition will be defined on the next slide Note: multiplicity annotations for aggregation/composition is tricky Some authors assume one to many when the diamond is present; others assume one to one and then add multiplicity indicators to the other end I prefer the former 39

Semantics of Aggregation Aggregation relationships are transitive if A contains B and B contains C, then A contains C Aggregation relationships are asymmetric If A contains B, then B does not contain A A variant of aggregation is composition which adds the property of existence dependency if A composes B, then if A is deleted, B is deleted Composition relationships are shown with a black diamond attached to the composing class 40

Relationships: Qualification An association can be qualified with information that indicates how objects on the other end of the association are found This allows a designer to indicate that the association requires a query mechanism of some sort e.g., an association between a phonebook and its entries might be qualified with a name Notation: a qualification is indicated with a rectangle attached to the end of an association indicating the attributes used in the query 41

Qualification Example PhoneBook name Entry Qualification is not used very often; the same information can be conveyed via a note or a use case that accompanies the class diagram 42

Relationships: Interfaces A class can indicate that it implements an interface An interface is a type of class definition in which only method signatures are defined A class implementing an interface provides method bodies for each defined method signature in that interface This allows a class to play different roles, with each role providing a different set of services These roles are then independent of the class s inheritance relationships 43

Example food type location Dog food type location Dog makenoise() eat() roam() makenoise() eat() roam() Pet Pet Person Other classes can then access a class via its interface This is indicated via a ball and socket notation 44

Class Summary Classes are blue prints used to create objects Classes can participate in multiple types of relationships inheritance, association (with multiplicity), aggregation/composition, qualification, interfaces 45

Sequence Diagrams (I) Objects are shown across the top of the diagram Objects at the top of the diagram existed when the scenario begins All other objects are created during the execution of the scenario Each object has a vertical dashed line known as its lifeline When an object is active, the lifeline has a rectangle placed above its lifeline If an object dies during the scenario, its lifeline terminates with an X 48

Sequence Diagrams (II) Messages between objects are shown with lines pointing at the object receiving the message The line is labeled with the method being called and (optionally) its parameters All UML diagrams can be annotated with notes Sequence diagrams can be useful, but they are also labor intensive (!) 49

:DogDoorSimulator :System.out «create» «create» «create» :DogDoor :BarkRecognizer :Remote println("fido starts barking.") recognize("woof") «create» schedule() open() println("barkrecognizer: Heard a 'Woof'.") println("the dog door opens.") :Timer sleep(5000) println("fido has gone outside...") println("fido's all done...") sleep(10000) close() println("the dog door closes.") println("...but he's stuck outside!") println("fido starts barking.") recognize("woof") Insert another copy of the interaction shown above here println("fido's back inside...") 50

Coming Up Next Lecture 4: More OO Fundamentals Homework 1 due on Thursday at class Upload your work into Moodle BEFORE class starts Be sure to bring a printout to class do not wait until the last minute to print!!! Lecture 5: Example problem domain and traditional OO solution Read Chapters 3 and 4 of the Textbook 51