3.3 Class Diagrams. Class Diagrams: Overview

Similar documents
An Introduction To UML Class Diagrams Classes

11 November

Java (12 Weeks) Introduction to Java Programming Language

Umbrello UML Modeller Handbook

Using UML Part One Structural Modeling Diagrams

Object Oriented Design

Lectures 2 & 3: Introduction to Modeling & UML. Getting started

What is Modeling language? What is UML? A brief history of UML Understanding the basics of UML UML diagrams for NES UML Profiles UML Modeling tools

Managing Variability in Software Architectures 1 Felix Bachmann*

Getting Started With UML Class Modeling. An Oracle White Paper May 2007

The UML Class Diagram

UML FOR OBJECTIVE-C. Excel Software

III. Class and Object Diagrams

Lab Manual: Using Rational Rose

Communication Diagrams

Tutorial - Building a Use Case Diagram

Design and UML Class Diagrams. Suggested reading: Practical UML: A hands on introduction for developers

How To Draw A Cell Phone Into A Cellphone In Unminimal Diagram (Uml)

A technical discussion on modeling with UML 06/11/03 Entity Relationship Modeling with UML

Design and UML Class Diagrams

CPS211 Lecture: Class Diagrams in UML

Case studies: Outline. Requirement Engineering. Case Study: Automated Banking System. UML and Case Studies ITNP090 - Object Oriented Software Design

Chapter 8 The Enhanced Entity- Relationship (EER) Model

How To Design Software

History OOP languages Year Language 1967 Simula Smalltalk

Visio Tutorial 1BB50 Data and Object Modeling (DOM) How to make a UML Class Diagram 2004/2005

UML Class Diagrams. Lesson Objectives

Incorporating Aspects into the UML

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

2. Analysis. The goal of this is to understand the problem. The key question is What?.

Unit 2.1. Data Analysis 1 - V Data Analysis 1. Dr Gordon Russell, Napier University

Software Project Management and UML

Introduction to Object-Oriented Programming

Tool Support for Software Variability Management and Product Derivation in Software Product Lines

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

Data Analysis 1. SET08104 Database Systems. Napier University

Object-Oriented Data Modeling

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Glossary of Object Oriented Terms

Source Code Translation

Principles of Software Construction: Objects, Design, and Concurrency. Domain Modeling and Specifying System Behavior. toad

Applying Object-Oriented Principles to the Analysis and Design of Learning Objects

Object Oriented Software Models

2. Conceptual Modeling using the Entity-Relationship Model

3 Development process for multimedia projects

UML Tutorial: Part 1 -- Class Diagrams.

UML basics. Part III: The class diagram. by Donald Bell IBM Global Services

Description of Class Mutation Mutation Operators for Java

Johannes Sametinger. C. Doppler Laboratory for Software Engineering Johannes Kepler University of Linz A-4040 Linz, Austria

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

A UML Introduction Tutorial

Structural Modeling and Analysis

IRA 423/08. Designing the SRT control software: Notes to the UML schemes. Andrea Orlati 1 Simona Righini 2

Object Oriented Databases. OOAD Fall 2012 Arjun Gopalakrishna Bhavya Udayashankar

Announcements. SE 1: Software Requirements Specification and Analysis. Review: Use Case Descriptions

Scenario-based Requirements Engineering and User-Interface Design

How to Make a Domain Model. Tutorial

Programming Language Constructs as Basis for Software Architectures

Chapter 2: Entity-Relationship Model. Entity Sets. " Example: specific person, company, event, plant

Introduction. UML = Unified Modeling Language It is a standardized visual modeling language.

Chapter 3. Data Modeling Using the Entity-Relationship (ER) Model

Best of Both Worlds - A Mapping from EXPRESS-G to UML

SysML Modelling Language explained

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Bitrix Site Manager 4.1. User Guide

Domain Modeling. Domain Modeling

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

Content Author's Reference and Cookbook

Introducing Variance into the Java Programming Language DRAFT

A terminology model approach for defining and managing statistical metadata

CSE 1020 Introduction to Computer Science I A sample nal exam

How To Understand The Purpose Of A Class Diagram In Java.Io (Java)

UNIVERSITY OF SURREY. BSc Programmes in Computing. Level 1 Examination. CS183: Systems Analysis and Design. Time allowed: 2 hours Spring Semester 2006

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

C++ INTERVIEW QUESTIONS

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Chapter 6: Programming Languages

Implementation Aspects of OO-Languages

IV. The (Extended) Entity-Relationship Model

UML for C# Modeling Basics

UML: Unified Modeling Language

CSC 742 Database Management Systems

Adding diagrams to Scaladoc

Entity-Relationship Model. Purpose of E/R Model. Entity Sets

Checking Access to Protected Members in the Java Virtual Machine

Graphical Systems Modeling with UML / SysML Class diagrams

State of Illinois Web Content Management (WCM) Guide For SharePoint 2010 Content Editors. 11/6/2014 State of Illinois Bill Seagle

Chapter 1 Fundamentals of Java Programming

Questions? Assignment. Techniques for Gathering Requirements. Gathering and Analysing Requirements

i. Node Y Represented by a block or part. SysML::Block,

CS 341 Software Design Homework 5 Identifying Classes, UML Diagrams Due: Oct. 22, 11:30 PM

Infrastructure that supports (distributed) componentbased application development

Fundamentals of Java Programming

A technical discussion of UML 06/11/03 Relational Modeling with UML

Getting Started with Protege-Frames

Exercise 8: SRS - Student Registration System

User experience storyboards: Building better UIs with RUP, UML, and use cases

OOP? What is OOP? Why? OOP in a nutshell. Stéphane Ducasse 2.1

Transcription:

3.3 Class Diagrams Subject/Topic/Focus: Class Diagrams: Modeling Static Structure Summary: Perspectives: Conceptual, Specification, Implementation Attributes, Operations and Methods Associations, Navigability, Aggregation, Composition, Association Classes Generalization, Interfaces, Abstract Classes Multiple and Dynamic Classification Parameterized Classes Literature: Fowler Rumbaugh 3.3. Class Diagrams: Overview Class diagrams describe the types of of objects in in the system and the various kinds of of static relationships that exist among them. There are two principal kinds of static relationships: associations a customer may rent a number of videos subtypes a student is a kind of person Class diagrams also show the attributes and operations of a class and the constraints that apply to the way objects are connected. 3.3.2 3.3.

Role of Class Diagrams Use Case Diagrams Activity Diagrams structures are refined by Interaction Diagrams inter-class behavior: describe control flow between classes Class Diagrams scenarios: describe typical interaction sequences between classes structuring Package Diagrams intra-class behavior: describe states and state transitions in classes State Diagrams Class Class diagrams are are central central for for analysis, design design and and implementation. Class Class diagrams are are the the richest richest notation in in UML. UML. 3.3.3 From Use Cases to Class Diagrams The requirements list of a company includes the following textual description of the use case order : Textfile customer order Textfile Order: Order: We We have have customers who who order order our our products. We We distinguish corporate customers from from personal customers, since since corporate customers are are billed billed monthly whereas personal customers need need to to prepay prepay their their orders. orders. We We want want our our orders orders to to be be lined lined up up product product by by product. Each Each line line should should contain contain the the amount amount and and the the price price of of each each product. 3.3.4 3.3.2

Example: Order - Associations Order Association Multiplicity Order: Order: We We have have customers who who may may order order several several products. We We distinguish corporate customers from from personal customers, since since corporate customers are are billed billed monthly whereas personal customers need need to to prepay prepay their their orders orders with with a credit credit card. card. We We want want our our orders orders to to be be lined lined up up product product by by product. Each Each line line should should contain contain the the amount amount and and the the price price of of each each product. 3.3.5 Example: Order - Generalization Order Generalization Corporate Personal Order: Order: We We have have customers who who order order our our products. We We distinguish corporate customers from from personal customers, since since corporate customers are are billed billed monthly whereas personal customers need need to to prepay prepay their their orders orders with with a credit credit card. card. We We want want our our orders orders to to be be lined lined up up product product by by product. Each Each line line should should contain contain the the amount amount and and the the price price of of each each product. 3.3.6 3.3.3

Example: Order - More Associations Role name Order Line item Order Line Product Corporate Personal Order: Order: We We have have customers who who order order our our products. We We distinguish corporate customers from from personal customers, since since corporate customers are are billed billed monthly whereas personal customers need need to to prepay prepay their their orders orders with with a credit credit card. card. We We want want our our orders orders to to be be lined lined up up product product by by product. Each Each line line should should contain contain the the amount amount and and the the price price of of each each product. 3.3.7 Example: Order- Attributes & Operations Order datereceived isprepaid number:string price:money dispatch() close() Attributes Operations name address creditrating() Order: Order: We We have have customers who who order order our our products. We We distinguish corporate customers from from personal customers, since since corporate customers are are billed billed monthly whereas personal customers need need to to prepay prepay their their orders orders with with a credit credit card. card. We We want want our our orders orders to to be be lined lined up up product product by by product. Each Each line line should should contain contain the the amount and and the the price price of of each each product. 3.3.8 3.3.4

Example: Order - Full Class Diagram Order datereceived isprepaid number:string price:money dispatch() close() Multiplicity: mandatory Association Generalization Constraint name address creditrating() Role name Line item Order Line quantity:integer price:money issatisfied:bool {If Order.customer.creditRating is poor, then Order.isPrepaid must be true} Attributes Operations Product Corporate contactname creditrating creditlimit Remind() billformonth(int) Personal creditcard# Multiplicity: optional Multiplicity: many-valued 0.. Employee 3.3.9 Perspectives There are three perspectives (views) you can use in drawing class diagrams: Conceptual represents the concepts relating to the classes provides language independence Specification represents the software interfaces hides the implementation Implementation shows the real classes used in the programming language maps directly to the implementation 3.3.0 3.3.5

Attributes Attributes name name address Attributes may be specified at different levels of detail: At the conceptual level a customer s name attribute indicates that customers have names. At the specification level, this attribute indicates that a customer object can tell you its name and you can set the name. At the implementation level, a customer has a field or an instance variable for its name. The UML syntax for attributes, depending on the level of detail: visibility name: type = default-value + identifier :: String String = Mr. Mr. Noname 3.3. Operations Operation creditrating() Operations are the processes that a class knows to perform. They correspond to the methods of a class in an OO language. At specification level operations correspond to public methods on a class. Normally you do not show those methods that simply set or get attribute values. In the implementation view usually private and protected methods are shown. The use of UML syntax for operations may vary with the level of detail: visibility name(parameter-list) : return-type-expression {property string} + creditrating(for :: Year) Year) :: Rating Rating {abstract} 3.3.2 3.3.6

UML Meta Description visibility name(parameter-list) : return-type-expression {property string} Visibility is + : for public, i.e., every other class can see this. : for private, i.e., only this class can see this. # : for protected, i.e., only subclasses can see this. Identifier is defined by a string. Parameter-list contains (optional) arguments whose syntax is the same as that for attributes, i.e., name, type and default value. Return-type-expression Property-string is an optional, language-dependent specification that specifies the type of the return value (if any). indicates property values that apply to the given operation, e.g., if this operation is abstract (not implemented in this class, but in subclasses). + creditrating(for :: Year) Year) :: Rating Rating {abstract} 3.3.3 Operations vs. Methods An operation is something that is invoked on an object (or a message that is sent to an object) while a method is the body of a procedure, i.e., the implementation that realizes the operation or method. This distinction facilitates polymorphism. anobject.move(dx,dy)... Operation Method Class Class Object Object move(dx,dy)... apoint.move(dx,dy) acircle.move(dx,dy) move(dx,dy) Class Class Point Point move(dx,dy) Class Class Circle Circle extends Point Point move(dx,dy) 3.3.4 3.3.7

Associations Associations represent relationships between instances of classes. Peter and Mary are employed by IBM. Instances are are marked marked Peter IBM Mary by by underlining. From the conceptual perspective, associations represent conceptual relationships between classes. Person Company Companies employ persons. Each association has two roles that may be named with a label. Employer Person Company Employee Multiplicities indicate how many objects may participate in a relationship. A person is employed by a (exactly one) company. A company may employ many persons. Person Company 3.3.5 Associations: Multiplicities A customer may have many orders. An order comes from only one customer. Order The represents the range 0..Infinity. The stands for... An order must have been placed by exactly one customer. For more general multiplicities you can have a single number like soccer players, a range, for example, 2..4 players for a canasta game, a discrete set of numbers like 2,4 for doors in a car. 3.3.6 3.3.8

Navigability To indicate navigability with associations, arrows are added to the lines. Order Order Navigation direction In a specification view this would indicate that an order has a responsibility to tell which customer it is for, but a customer has no corresponding ability to tell you which orders it has. In an implementation view, one would indicate, that order contains a pointer to customer but customer would not point to orders. If a navigability exists in only one direction it is called uni-directional association otherwise bi-directional association. 3.3.7 Aggregation Aggregation is the part-of relationship. Computer CPU A CPU is part of a computer. Car A car has an engine and doors as its parts. Aggregation vs. attributes : 2..4 Engine Door Attributes describe properties of objects, e.g. speed, price, length. Aggregation describe assemblies of objects. Aggregation vs. association: Is a company an aggregation over its employees or is it an association between its employees? Company Employee? Company Employee 3.3.8 3.3.9

Composition Composition is a stronger version of aggregation: The part object may belong to only one whole. The parts usually live and die with the whole. Example: Aggregation: A company has employees. The employees may change the company. Composition: The company has a tax registration. The tax registration is tied to the company and dies with it. Aggregation Company Composition Employee Tax Registration 3.3.9 Example: Aggregation & Composition A polygon contains an ordered collection of points. Aggregation Polygon A graphics icon is created and destroyed with the polygon and cannot be changed. Composition {ordered} 3.. Point These points may be changed as the polygon is edited. Graphical Icon Icon color color texture texture The attributes can be changed, but the icon cannot be replaced by another object. 3.3.20 3.3.0

Association Classes Example: Persons are employed by companies for a period of time. Question: Where does the period attribute go? Association classes allow you to model associations by classes, i.e., to add attributes, operations and other features to associations. Person Person employee employer 0.. Company Employment Association class period:daterange Note: a person and a company are associated only by one employment period here. 3.3.2 Association Classes vs. Full Classes If a person may return to a company, you have to use a full class: /employer Person Person 0.. Employment period:daterange 0.. Company But: A person may have only one competency level for each skill: Person Person Skill Skill Competency level level Association class 3.3.22 3.3.

Generalization Generalization captures similarities between several classes in a superclass Specialization refines and adds differences in subclasses. Similarities are placed in a general superclass. name name address address creditrating() creditrating() The differences are separated in specialized subclasses. Corporate contactname contactname creditrating creditrating creditlimit creditlimit Remind() Remind() billformonth(int) billformonth(int) Personal creditcard# creditcard# 3.3.23 Generalization: Perspectives In a specification context, generalization means that the interface of a subclass includes all elements of the interface of the superclass. Generalization/Specialization can also be understood through the principle of substitutability. Any operation carried out on a customer can be performed on a corporate customer, too. The corporate customer probably responds differently from the regular customer by the principles of polymorphism. Generalization at the implementation perspective is associated with inheritance in programming languages. 3.3.24 3.3.2

Interfaces An interface is a (abstract) class with no implementation. An interface is implemented (refined) by (different) classes. The implementation can be changed without changing the clients. Example: A portable text editor displays its windows using a window interface that is implemented differently for Windows 95 and Mac OS. Dependency (see below) Text Text Editor Editor <<interface>> Window tofront() toback() Generalization/ Refinement Windows 95 95 Window tofront() toback() Mac Mac OS OS Window tofront() toback() 3.3.25 Abstract Classes An abstract class is a class without a (full) implementation. Some methods are deferred, i.e., they are not implemented. The deferred methods are implemented by subclasses only. Example: The window move operation is implemented by using hide and show methods which are implemented by subclasses. Text Text Editor Editor void move(int dx, int dy) { hide(); x = x+dx; y = y+dy; show(); } Window {abstract} x, x, y hide() hide() {abstract} show() {abstract} move(dx, dy) dy) Implementation Windows 95 95 Window hide() show() Mac Mac OS OS Window hide() show() 3.3.26 3.3.3

Example: Interfaces and Abstract Classes Example from Java class libraries: InputStream is an abstract class, i.e., some methods are deferred. DataInput is an interface, i.e., it implements no methods. DataInputStream is a subclass of InputStream; it implements the deferred methods of InputStream, and the methods of the interface DataInput. OrderReader uses only those methods of DataInputStream that are defined by the interface DataInput. InputStream {abstract} <<interface>> DataInput OrderReader Generalization Dependency DataInputStream Refinement 3.3.27 Lollipop Notation The interfaces or abstract classes are represented by small circles (lollipops), coming off the classes that implement them. DataInput Interface OrderReader Abstract class DataInputStream Dependency Implementation class InputStream 3.3.28 3.3.4

Interfaces vs. Abstract Classes There is no distinction between refining an interface and subclassing an abstract class. Both define an interface and defer implementation. However, abstract classes allow to add implementation of some methods. An interface forces you to defer the implementation of all methods. Interfaces are used to emulate multiple inheritance, e.g., in Java. A (Java) class cannot be subclass of many superclasses. But it can implement different interfaces. InputStream DataInput InputStream <<interface>> DataInput DataInputStream DataInputStream 3.3.29 Multiple and Dynamic Classification Classification refers to the relationship between an object and its type, e.g., Paul is a Person (i.e., object Paul belongs to class Person). Most Most methods make make certain certain assumptions about about the the type type of of relationship between an an object object and and its its type. type. Jim Jim Odell Odell questioned the the restrictive single, single, static static classification and and proposed multiple and and dynamic classification for for conceptual modeling. Issues: May an object belong to different classes, e.g. Patient and Doctor? May an object change its class, e.g., from Patient to Doctor? In single classification, an object belongs to a single class, which may inherit from superclasses. In multiple classification, an object may be described by several classes, that are not necessarily connected by inheritance. 3.3.30 3.3.5

Multiple Classification Example: Somebody in a hospital may be a patient, a doctor, a nurse or simply a person. profession Patient Discriminator patient Person profession Doctor Nurse Multiple classification allows an object to be related to many classes. Example: Paul may be patient and doctor. The discriminators patient and profession point out legal combinations: All subclasses with the same discriminator (e.g., profession ) are disjoint. Example: Paul could not be doctor and nurse - only doctor or nurse. Usage of multiple classification: May be of importance in early A&D phases. Mapping multiple classifications into object-oriented languages is usually not straight forward. 3.3.3 Multiple Classification vs. Multiple Inheritance Multiple classification: multiple classes for an object without defining a specific class for the purpose. Patient patient Person profession Doctor Multiple inheritance: a class may have many superclasses but for each object a single class must be defined. Person Doctor Patient DoctorAsPatient 3.3.32 3.3.6

Multiple Classification: Discriminators The discriminator constraint {complete} indicates that the superclass has no instances (i.e., is an abstract class); instead, all instances must be of one of the subclasses; e.g., a person must be either male or female. Female Person Male Person sex {complete} Person profession patient Patient Physiotherapist Nurse Doctor Surgeon Legal combinations are, e.g., Illegal combinations are, e.g., (Female, Patient, Nurse) (Patient, Doctor) sex missing (Male, Physiotherapist) (Male, Doctor, Nurse) two roles (Female, Patient) Family Doctor 3.3.33 Dynamic Classification Dynamic classification allows objects to change class within the subclass structure. Dynamic classification combines types and states. Example: A person s profession can change over time. Paul may be a Physiotherapist and become a Doctor (he could not be both at the same time!). Person profession <<dynamic>> Physiotherapist Nurse Note: this is a stereotype! Doctor 3.3.34 3.3.7

Parameterized (Template) Classes Often you need generic classes over elements of a single type, especially for collections, e.g., lists, sets, bags of elements,... Generic classes abstract from the elements they work on, e.g., set of integer, set of person,... Template class Set Set T Template parameter insert(t) includes(t) Parameter binding Refinement Bound class PersonSet <<bind>> <Person> Note: Note: In In Java Java there there are are no no type type parameters or or templates. 3.3.35 When and How to Use Class Diagrams Class diagrams are the backbone of nearly all object-oriented methods. Especially they facilitate code generation. The trouble with class diagrams and their rich notation is that they can be extremely detailed and therefore confusing. Do not try to use all the notations available to you, if you do not have to. Fit the perspective from which you are drawing the diagrams to the stage of the project. If you are in analysis, draw conceptual models. When working with software, concentrate on specification. Draw implementation models only when you are illustrating a particular implementation technique. Don t draw diagrams for everything; instead concentrate on the key areas. 3.3.36 3.3.8