Showing Relationships in UML

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
UML Class Diagram. UML Class Diagrams2 Agenda What is a Class Diagram? Essential Elements of a UML Class Diagram Tips.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Unified Modeling Language
Structural Modeling: Class Diagrams Copyright © 2009 John Wiley & Sons, Inc. Copyright © 2005 Pearson Education Copyright © 2009 Kannan Mohan CIS 4800.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
Systems Analysis and Design in a Changing World, 6th Edition 1 Chapter 4 - Domain Classes.
CH06: Considering Objects TECH Computer Science  Set, Class, Type  …of…  Objects, Actors, Agents  Data and Actions Object-Oriented Design and Development.
The Static Analysis Model Class Diagrams Prof. Hany H. Ammar, CSEE, WVU, and Dept. of Computer Science, Faculty of Computers and Information, Cairo University.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Interfaces.
UML The Unified Modeling Language A Practical Introduction Al-Ayham Saleh Aleppo University
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
12 OBJECT-ORIENTED DESIGN CHAPTER
CS212: Object Oriented Analysis and Design Lecture 33: Class and Sequence Diagram.
UML Class Diagram notation Indicating relationships between classes SE-2030 Dr. Mark L. Hornick 1.
Topics Inheritance introduction
Chapter 16 UML Class Diagrams 1CS6359 Fall 2012 John Cole.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Class Diagram Associations Class Diagrams, Class Stereotypes, Class Associations Dr. Neal CIS 480.
2-1 © Prentice Hall, 2004 Chapter 2: Introduction to Object Orientation Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph.
Chapter 12 – Object-Oriented Design
Lecture 8 D&D Chapter 9 Inheritance Date.
Class Inheritance Part I
Unified Modeling Language (UML)
UML Diagrams: Class Diagrams The Static Analysis Model
Object-Oriented Modeling
Unified Modeling Language
Lecture 12 Inheritance.
OBJECT ORIENTED CONCEPT
Interface, Subclass, and Abstract Class Review
Chapter 11 Object-Oriented Design
Collaborations and Hierarchies
Class diagram Description
Object Oriented Analysis and Design
The Basics of Class Diagrams for a single class
Modeling with Inheritance
Software Engineering Lecture #11.
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
UML Class Diagram.
Advanced Programming Behnam Hatami Fall 2017.
Unified Modelling Language
Software Engineering System Modeling Extra examples Dr.Doaa Sami
Two big words Inheritance Polymorphism
Understand and Use Object Oriented Methods
Basics of OOP A class is the blueprint of an object.
Lecture 14: Inheritance Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Chapter 12 – Object-Oriented Design
An Example of Inheritance
Introduction to UML Sources:
Object Oriented System Design Class Diagrams
Information System Design
ITEC324 Principle of CS III
ITEC324 Principle of CS III
Presentation transcript:

Showing Relationships in UML Class Diagram with more than one class

Dependency One class uses or depends on another class. Includes "association". Game GameConsole public class GameConsole { // the play method depends on Game. public void play(Game game) { ... boolean correct = game.guess( number ); String hint = game.getMessage( );

More Dependency Main depends on (uses) Game and GameConsole +play(game: Game) <<creates>> Main <<creates>> Game public class Main { public static void main(String[] args) { Game game = new Game(1000000); GameConsole ui = new GameConsole(); ui.play( game );

Dependency Example A Student uses the Registrar to enroll in a Course, but he doesn't save a reference to the Registrar. public class Student { private long id; //NO Registrar attribute! public void addCourse(Course course) { Registrar regis = Registrar.getInstance(); regis.enroll(this, course);

Association Association means one object has an attribute of another class. Game GameConsole game public class GameConsole { private Game game; /** constructor */ public GameConsole(Game game) { this.game = game;

Association with Multiplicity You can indicate multiplicity of the association. A card deck contains exactly 52 cards. CardDeck 52 Card public class CardDeck { private Card[] cards; public CardDeck() { cards = new Card[52]; for(int k=0; k<52; k++) { cards[k] = new Card("..."); }

Association with Variable Multiplicity A MailBox may contain 0 or more mail messages. * = any number (0 or more) 1..n = 1 to n n = exactly n * MailMessage MailBox public class MailBox { private List<MailMessage> messages; public MailBox() { messages = new ArrayList<MailMessage>(); } public void addMessage(MailMessage m) { message.add( m );

Vehicle has at least 2 Wheels Only vehicles with at least 2 wheels are allowed on roads. A vehicle must have at least 2 wheels. 2..* Vehicle Wheel public class Vehicle { private Wheel[] wheels; public Vehicle(int n) { if (n<2) throw new IllegalArgumentException( "Must have at least 2 wheels."); wheels = new Wheel[n]; }

Class with Many Associations A Student has one physical Address and 0 or more Email address. public class Student { private Address homeAddress; /** his email addresses. He may have many. */ private List<Email> emailAddress;

Bidirectional Association. If each object has a reference to the other object, then it is bidirectional. * Person employer: Company Company employees: List<Person> This is rare, in practice. Try to avoid bidirectional associations.

Aggregation: whole-parts relationship One class "collects" or "contains" objects of another MailBox MailMessage * public class MailBox { private List<MailMessage> messages; /* a MailBox consists of MailMessages */ Aggregation often shows a whole-parts relationship The parts can exist without the whole. (MailMessage can exist outside of a MailBox.)

When to use Aggregation? One object "collects" or "aggregates" components. Advice: Don't show aggregation. (UML Distilled, Ch. 5.) Just show it as association. If it is really "composition" then show composition.

Composition: ownership relation One class "owns" objects of the other class. If the "whole" is destroyed, the parts are destroyed, too. Square ChessBoard 64 public class ChessBoard { private Square[][] squares = new Square[8][8];

A Student owns his Email Addresses Composition: A Student owns his Email addresses. 1) No one else can have the same email address. 2) When he is destroyed, we destroy his addresses, too! public class Student { /** student uniquely owns his email addresses*/ private List<Email> emailAddress;

Inheritance Object Number is a subclass of Object. Number inherits all the methods of Object. But, it overrides the definition of some methods, and adds new methods.. Double is a subclass of Number. Double inherits all the methods of Number. Double overrides the definition of some methods, and adds new methods. Number Double

Other names for Inheritance Specialization - a subclass is a specialization of the superclass. Generalization - the superclass generalizes behavior of a hierarchy of subclasses.

Implements an Interface

<<interface>> Comparable The String class implements Comparable interface <<interface>> Comparable +compareTo( o: Object) : int write <<interface>> above the name You don't need to write "implements" on the diagram, but you MUST use a dashed arrow and triangle arrowhead as shown here. implements String ... +compareTo( o: Object) : int

Inheritance & Implements You can have both in one class. public class Student extends Person implements Comparable<Student> {

Errors To communicate clearly, use the correct notation. Example: in a circuit diagram, if you draw a diode in the wrong direction, the circuit might explode. wrong Student Person wrong <<interface>> Comparable No partial credit for wrong relationships or incorrect notation.

Exercise: draw UML class diagram A Coin has a value (double). class Coin { private double value; public Coin(double value) ...; public double getValue( ) { ... } public boolean equals(Object other) { ... } } A Purse contains zero or more coins: class Purse { private List<Coin> list = new ArrayList<Coin>(); public boolean add(Coin coin) {...}

Exercise: Guessing Game Draw UML class diagram of Guessing Game w/ relations Include the classes: NumberGame - superclass of all games * see the lab sheet for attributes and methods GuessingGame - subclass of NumberGame GameConsole - plays a NumberGame * has only one method: play(NumberGame game) * which class does it depend on? Main - creates the objects

Extra features useful for showing design Designing with UML Extra features useful for showing design

Association Names You can write text on the middle of an association to show the nature or meaning of the association. Useful for design, but not required. works for Person Company

Roles You can write the name of the association on the opposite end of the association. Useful for design. For a Person, Company is the "employer". For a Company, Person is an "employee". Person Company employee employer Role names.