Undergraduate Topics in Computer Science Martina Seidl · Marion Scholz Christian Huemer · Gerti Kappel UML @ Classroom An Introduction to Object-Oriented Modeling Undergraduate Topics in Computer Science Undergraduate Topics in ComputerS cience ( UTiCS)d eliversh igh-qualityin structionalco ntentfo r undergraduates studying in all areas of computing and information science. From core foundational and theoretical material tof inal-yeart opicsa nda pplications,U TiCSb ooksta kea fr esh,co ncise,an d modern approach and are idealf or self-studyo rf ora o ne-o rt wo-semesterc ourse.T hete xtsar eal l authored by established experts in their fields, reviewed by an international advisory board, and contain numerous examples and problems. Many include fully worked solutions. More information about this series at http://www.springer.com/series/7592 Martina Seidl • Marion Scholz Christian Huemer • Gerti Kappel UML @ Classroom An Introduction to Object-Oriented Modeling Martina Seidl Marion Scholz Johannes Kepler University Linz Vienna University of Technology Linz, Austria Vienna, Austria Christian Huemer Gerti Kappel Vienna University of Technology Vienna University of Technology V ienna, Austria Vienna, Austria Tanslator Tracey Duffy TSD Translations SeriesEditor IanMackie AdvisoryBoard SamsonAbramsky,UniversityofOxford,Oxford,UK KarinBreitman,PontificalCatholicUniversityofRiodeJaneiro,RiodeJaneiro,Brazil ChrisHankin,ImperialCollegeLondon,London,UK DexterKozen,CornellUniversity,Ithaca,USA AndrewPitts,UniversityofCambridge,Cambridge,UK HanneRiisNielson,TechnicalUniversityofDenmark,KongensLyngby,Denmark StevenSkiena,StonyBrookUniversity,StonyBrook,USA IainStewart,UniversityofDurham,Durham,UK Copyright © 2012 by dpunkt.verlag GmbH, Heidelberg, Germany. Title of the German original: UML @ Classroom ISBN 978-3-89864-776-2 Translation Copyright © 2014 by Springer International Publishing AG. All rights reserved. ISSN1863-7310 ISSN2197-1781 (electronic) Under graduate T opics in Computer Scien ce ISBN 978-3-319-12741-5 ISBN 978-3-319-12742-2 (eBook) DOI 10.1007/978-3-319-12742-2 Library of Congress Control Number: 2015930192 S pringer Cham Heidelberg New York Dordrecht London © Springer International Publishing Switzerland 2015 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made. Printed on acid-free paper Springer International Publishing AG Switzerland is part of Springer Science+Business Media (www.springer.com) Preface The challenges in today’s software development are diverse and go far beyond implementation tasks. They range from requirement spec- ification over system design and implementation to maintenance and further adaptation of the software—to name just a few phases in the software life cycle. In all of these phases of the software develop- mentprocess,manypeoplewithdifferentbackgroundsandexperiences are usually involved. These people need a common language for ef- ficient communication. Obviously, such a language should be as pre- ciseaspossiblewithouttheambiguitiesofanaturallanguage.Forthis purpose, modeling languages have emerged. They are used to create sketches and blueprints for software systems, which in turn serve as a basis for the implementation or even automatic generation of exe- cutable code. In the area of object-oriented software development, the Unified Modeling Language (UML) was able to prevail. Of course, to usethelanguagecorrectlyandefficiently,itisnecessarytounderstand theconceptsofferedbyUML.Since2006,wehaveofferedthecourse “Object-Oriented Modeling” at the Vienna University of Technology. Thiscourseismandatoryforcomputerscienceandbusinessinformat- icsstudentsintheirfirstyear.Overall,wehaveupto1,000studentsper year who attend our course. To deal with such a huge number of stu- dentswhilekeepinghighqualitystandards,muchefforthasbeenspent on the preparation of such a course. This includes the overall organi- zation,coursematerial,ande-learningsupport.Partsofthecoursede- signhavebeenpresentedattheEducators’SymposiumoftheMODELS conference[8,9,10,11,7,46].Weteachthebasicsofobject-oriented modelingbymeansofUML. v vi Preface Inparticular,weteach • classandobjectdiagrams, • sequencediagrams, • statemachinediagrams, • activitydiagrams,and • usecasediagrams aswellastheirinterrelations.Forthispurpose,weintroducethesyntax (thenotationofthelanguageelements),thesemantics(themeaningof the language elements), and the pragmatics (how to use the language elements)oftheseUMLdiagrams.Theycoverthemostessentialcon- ceptsofobject-orientedmodelingandareusedinmanydifferentstages of the software development process. The course is designed for stu- dentswhoalreadyknowthebasicconceptsofobject-orientedprogram- ming languages such as Java or C#, but still have no practical experi- enceinsoftwareengineering.Basedonourcomprehensiveexperience inteachingUML,[email protected], weaddressbothreaderswhowishtolearnUMLinacompactbutnev- ertheless precise manner and teachers whom we want to provide with inspiration for their own course exercises with our extensive example repertoire.WeteachUMLasclosetothestandardaspossibleandillus- trateallconceptsusingintuitiveexamples.Thebookiscomplemented byawebsite,whichcontainsacompletesetofslidestoteachthecon- tents of the book as well as teaching videos and e-learning material (http://www.uml.ac.at/). Softwaremodelingisaveryyoungfieldofcomputerscience.Itex- periencedanincrediblegrowthwithinthelasttwodecades.Today,the usageofmodelsgoesfarbeyondpuredocumentation.Techniquesfrom the area of modeling continually replace conventional programming. Models are far more than just pictures, and modeling is far more than just drawing. With our book UML@Classroom, we want to provide a solidfoundationanddeepunderstandingofthemostimportantobject- orientedmodelingconcepts.Weaimforrisinginterestandenthusiasm for this exciting and extremely important field of computer science. UML@Classroom is a textbook, which explicitly addresses beginners and people with little or no modeling experience. It introduces basic conceptsinaveryprecisemanner,whileabstainingfromtheinterpre- tationofrarespecialcases.UML@Classroomiskeptverycompactin ordertoallowthereadertofocusonthemostcommonlyusedconcepts ofUML.Muchemphasisisspentonillustrativeexamplesbreathinglife intothetheorywepresent. Preface vii Acknowledgments Wewouldliketothankthemanypeoplewhosupportedusinthesuc- cessfulcompletionofthisbook.Veryspecialthanksgotoourfamilies whoshowedgreatpatienceforthisbookproject.Wearedeeplyindebted toKatjaHildebrandt(ViennaUniversityofTechnology)fordrawingall the figures of this book and for supporting us with a million of other things.WewouldliketothankRalfGerstnerfromSpringerandChrista Preisendanzfromdpunkt.verlagformakingthisEnglishversionpossi- ble.Further,wewouldliketothankTraceyDuffyforthegoodcollabo- rationonthetranslationoftheGermanversionofthisbookintoEnglish andJeremyGibbons(UniversityofOxford)forthecarefulproofreading andtheveryvaluablefeedback.Finally,wewouldliketoacknowledge theinputwegotfromourstudentsovertheyearswhichwasoneofthe mainmotivatorsforwritingthisbook. LinzandVienna,September2014 MartinaSeidl MarionScholz ChristianHuemer GertiKappel Contents 1 Introduction........................................ 1 1.1 Motivation ..................................... 1 1.2 Models ........................................ 2 1.3 ObjectOrientation............................... 6 1.3.1 Classes.................................. 6 1.3.2 Objects.................................. 6 1.3.3 Encapsulation ............................ 7 1.3.4 Messages................................ 7 1.3.5 Inheritance............................... 7 1.3.6 Polymorphism............................ 8 1.4 TheStructureoftheBook ........................ 9 2 AShortTourofUML ............................... 11 2.1 TheHistoryofUML............................. 11 2.2 Usage ......................................... 14 2.3 Diagrams ...................................... 15 2.3.1 StructureDiagrams........................ 17 2.3.2 BehaviorDiagrams........................ 19 2.4 DiagramsPresentedinthisBook................... 21 3 TheUseCaseDiagram............................... 23 3.1 UseCases...................................... 24 3.2 Actors......................................... 25 3.3 Associations.................................... 27 3.4 RelationshipsbetweenActors ..................... 28 3.5 RelationshipsbetweenUseCases .................. 30 3.6 ExamplesofRelationships........................ 33 3.7 CreatingaUseCaseDiagram ..................... 34 3.7.1 IdentifyingActorsandUseCases............ 34 3.7.2 DescribingUseCases ..................... 35 ix