Using UML Patterns and Java ObjectOriented Software Engineering
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 2, Modeling with UML
Overview: modeling with UML ¨ ¨ ¨ What is modeling? What is UML? Use case diagrams Class diagrams Sequence diagrams Activity diagrams Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2
What is modeling? ¨ ¨ Modeling consists of building an abstraction of reality. Abstractions are simplifications because: w They ignore irrelevant details and w They only represent the relevant details. ¨ What is relevant or irrelevant depends on the purpose of the model. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3
Example: street map Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4
Why model software? ¨ Software is getting increasingly more complex w Windows XP > 40 million lines of code w A single programmer cannot manage this amount of code in its entirety. ¨ ¨ Code is not easily understandable by developers who did not write it We need simpler representations for complex systems w Modeling is a means for dealing with complexity Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5
Application and Solution Domain ¨ Application Domain (Requirements Analysis): w The environment in which the system is operating ¨ Solution Domain (System Design, Object Design): w The available technologies to build the system Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6
Object-oriented Modeling Solution Domain (Phenomena) Application Domain (Phenomena) System Model (Concepts)(Analysis) Traffic. Control Aircraft Airport Bernd Bruegge & Allen H. Dutoit UML Package System Model (Concepts)(Design) Map. Display Traffic. Controller Flight. Plan Object-Oriented Software Engineering: Using UML, Patterns, and Java Summary Display Flight. Plan. Database Traffic. Control 7
What is UML? ¨ UML (Unified Modeling Language) w Nonproprietary standard for modeling software systems, OMG w Convergence of notations used in object-oriented methods OMT (James Rumbaugh and collegues) t Booch (Grady Booch) t OOSE (Ivar Jacobson) Current Version: UML 2. 2 t ¨ w Information at the OMG portal http: //www. uml. org/ ¨ Commercial tools: Rational (IBM), Together (Borland), Visual Architect (business processes, BCD) ¨ ¨ Open Source tools: Argo. UML, Star. UML, Umbrello Commercial and Opensource: Poseidon. UML (Gentleware) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8
UML: First Pass ¨ You can model 80% of most problems by using about 20% UML We teach you those 20% ¨ 80 -20 rule: Pareto principle ¨ (http: //en. wikipedia. org/wiki/Pareto_principle) w 80% of your profits come from 20% of your customers w 80% of your complaints come from 20% of your customers w 80% of your profits come from 20% of the time you spend w 80% of your sales come from 20% of your products Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9
UML First Pass ¨ Use case diagrams w Describe the functional behavior of the system as seen by the user ¨ Class diagrams w Describe the static structure of the system: Objects, attributes, associations ¨ Sequence diagrams w Describe the dynamic behavior between objects of the system ¨ State diagrams w Describe the dynamic behavior of an individual object Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10
UML Core Conventions ¨ All UML Diagrams denote graphs of nodes and edges w Nodes are entities and drawn as rectangles or ovals w Rectangles denote classes or instances w Ovals denote functions • Names of Classes are not underlined • Simple. Watch • Firefighter • Names of Instances are underlined • my. Watch: Simple. Watch • Joe: Firefighter • An edge between two nodes denotes a relationship between the corresponding entities Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11
UML first pass: Use case diagrams Classifier Use Case Actor System boundary Use case diagrams represent the functionality of the system from user’s point of view Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12
UML first pass: Class diagrams Association Class Multiplicity 2 Push. Button Simple. Watch 1 1 Display 1 1 1 2 Battery 1 Time Class diagrams represent the structure of the system Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13
UML first pass: Class diagrams represent the structure of the system Association Class Multiplicity 1 2 Push. Button state push() release() Attribute Bernd Bruegge & Allen H. Dutoit Watch 1 1 2 1 LCDDisplay blink. Idx blink. Seconds() blink. Minutes() blink. Hours() stop. Blinking() referesh() Battery Load Object-Oriented Software Engineering: Using UML, Patterns, and Java Time Now Operations 14
UML first pass: Sequence diagram Actor Message Object : Watch. User press. Button 1() Lifeline : Time : LCDDisplay blink. Hours() press. Button 1() blink. Minutes() press. Button 2() increment. Minutes() refresh() press. Button 1 and 2() commit. New. Time() Activation stop. Blinking() Sequence diagrams represent the behavior of a system as messages (“interactions”) between different objects Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15
UML first pass: Statechart diagrams Initial state Event button 1&2 Pressed Blink Hours Transition button 1&2 Pressed State button 2 Pressed Increment Hours button 1 Pressed Blink Minutes button 2 Pressed Increment Minutes button 1 Pressed Stop Blinking Blink Seconds button 2 Pressed Increment Seconds Final state Represent behavior of a single object with interesting dynamic behavior. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16
Other UML Notations UML provides many other notations, for example ¨ Deployment diagrams for modeling configurations w Useful for testing and for release management ¨ We introduce these and other notations as we go along in the lectures w OCL: A language for constraining UML models Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17
What should be done first? Coding or Modeling? ¨ ¨ It all depends…. Forward Engineering w Creation of code from a model w Start with modeling w Greenfield projects ¨ Reverse Engineering w Creation of a model from existing code w Interface or reengineering projects ¨ Roundtrip Engineering w Move constantly between forward and reverse engineering w Reengineering projects w Useful when requirements, technology and schedule are changing frequently. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18
UML Second Pass ¨ Use case diagrams w Describe the functional behavior of the system as seen by the user ¨ Class diagrams w Describe the static structure of the system: Objects, attributes, associations ¨ Sequence diagrams w Describe the dynamic behavior between objects of the system ¨ State diagrams w Describe the dynamic behavior of an individual object ¨ Activity diagrams w Describe the dynamic behavior of a system, in particular the workflow. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 19
UML Use Case Diagrams Used during requirements elicitation and analysis to represent external behavior (“visible from the outside of the system”) An Actor represents a role, that is, a type of user of the system Passenger A use case represents a class of functionality provided by the system Purchase. Ticket Bernd Bruegge & Allen H. Dutoit Use case model: The set of all use cases that completely describe the functionality of the system. Object-Oriented Software Engineering: Using UML, Patterns, and Java 20
Actors ¨ An actor is a model for an external entity which interacts (communicates) with the system: w User w External system (Another system) w Physical environment (e. g. Weather) Passenger Name Bernd Bruegge & Allen H. Dutoit ¨ An actor has a unique name and an optional description Optional ¨ Examples: Description w Passenger: A person in the train w GPS satellite: An external system that provides the system with GPS coordinates. Object-Oriented Software Engineering: Using UML, Patterns, and Java 21
Use Case Purchase. Ticket Bernd Bruegge & Allen H. Dutoit • A use case represents a class of functionality provided by the system • Use cases can be described textually, with a focus on the event flow between actor and system • The textual use case description consists of 6 parts: 1. 2. 3. 4. 5. 6. Unique name Participating actors Entry conditions Exit conditions Flow of events Special requirements. Object-Oriented Software Engineering: Using UML, Patterns, and Java 22
Textual Use Case Description Example Passenger 1. Name: Purchase ticket 2. Participating actor: Passenger 3. Entry condition: ¨ Passenger stands in front of ticket distributor ¨ Passenger has sufficient money to purchase ticket Purchase. Ticket 5. Flow of events: 1. Passenger selects the number of zones to be traveled 2. Ticket Distributor displays the amount due 3. Passenger inserts money, at least the amount due 4. Ticket Distributor returns change 5. Ticket Distributor issues ticket 6. Special requirements: None. 4. Exit condition: ¨ Passenger has ticket Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 23
Uses Cases can be related ¨ Extends Relationship w To represent seldom invoked use cases or exceptional functionality ¨ Includes Relationship w To represent functional behavior common to more than one use case. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 24
The <<extends>> Relationship ¨ ¨ Passenger ¨ ¨ Purchase. Ticket <<extends>> relationships model exceptional or seldom invoked cases The exceptional event flows are factored out of the main event flow for clarity The direction of an <<extends>> relationship is to the extended use case Use cases representing exceptional flows can extend more than one use case. <<extends>> Out. Of. Order <<extends>> Cancel Bernd Bruegge & Allen H. Dutoit Time. Out No. Change Object-Oriented Software Engineering: Using UML, Patterns, and Java 25
The <<includes>> Relationship ¨ ¨ Passenger Purchase. Multi. Card ¨ Purchase. Single. Ticket <<includes>> <<extends>> No. Change Bernd Bruegge & Allen H. Dutoit Collect. Money <<extends>> Cancel <<includes>> relationship represents common functionality needed in more than one use case <<includes>> behavior is factored out for reuse, not because it is an exception The direction of a <<includes>> relationship is to the using use case (unlike the direction of the <<extends>> relationship). <<extends>> Cancel Object-Oriented Software Engineering: Using UML, Patterns, and Java 26
Class Diagrams ¨ ¨ Class diagrams represent the structure of the system Used w during requirements analysis to model application domain concepts w during system design to model subsystems w during object design to specify the detailed behavior and attributes of classes. Tarif. Schedule Table zone 2 price Enumeration get. Zones() Price get. Price(Zone) Bernd Bruegge & Allen H. Dutoit * * Object-Oriented Software Engineering: Using UML, Patterns, and Java Trip zone: Zone Price: Price 27
Classes Type Name Tarif. Schedule zone 2 price get. Zones() get. Price() ¨ ¨ Tarif. Schedule Table zone 2 price Enumeration get. Zones() Price get. Price(Zone) Attributes Operations Signature Tarif. Schedule A class represents a concept A class encapsulates state (attributes) and behavior (operations) Each attribute has a type Each operation has a signature The class name is the only mandatory information Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 28
Actor vs Class vs Object ¨ Actor w An entity outside the system to be modeled, interacting with the system (“Passenger”) ¨ Class w An abstraction modeling an entity in the application or solution domain w The class is part of the system model (“User”, “Ticket distributor”, “Server”) ¨ Object w A specific instance of a class (“Joe, the passenger who is purchasing a ticket from the ticket distributor”). Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 30
Associations Tarif. Schedule Trip. Leg Enumeration get. Zones() Price get. Price(Zone) * * Price Zone Associations denote relationships between classes The multiplicity of an association end denotes how many objects the instance of a class can legitimately reference. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 31
1 -to-1 and 1 -to-many Associations Country 1 name: String 1 City name: String 1 -to-1 association Polygon * Point x: Integer y: Integer draw() 1 -to-many association Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 32
Many-to-Many Associations Company Stock. Exchange Bernd Bruegge & Allen H. Dutoit * * ticker. Symbol Object-Oriented Software Engineering: Using UML, Patterns, and Java 33
From Problem Statement To Object Model Problem Statement: A stock exchange lists many companies. Each company is uniquely identified by a ticker symbol Class Diagram: Stock. Exchange * Bernd Bruegge & Allen H. Dutoit * Lists Company ticker. Symbol Object-Oriented Software Engineering: Using UML, Patterns, and Java 34
From Problem Statement to Code Problem Statement : A stock exchange lists many companies. Each company is identified by a ticker symbol Class Diagram: Stock. Exchange * Lists * Company ticker. Symbol Java Code public class Stock. Exchange { private Vector m_Company = new Vector(); }; Associations are mapped to Attributes! public class Company { public int m_ticker. Symbol; private Vector m_Stock. Exchange = new Vector(); }; Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 35
Aggregation ¨ ¨ An aggregation is a special case of association denoting a “consists-of” hierarchy Exhaust system The aggregate is the parent class, the components are the children classes 0. . 2 1 Muffler Tailpipe diameter A solid diamond denotes composition: A strong form of aggregation where the life time of the component instances is controlled by the aggregate. That is, the parts don’t exist on their won (“the whole controls/destroys the parts”) Ticket. Machine 3 Zone. Button Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 36
Inheritance Button Cancel. Button ¨ ¨ ¨ Zone. Button Inheritance is another special case of an association denoting a “kind-of” hierarchy Inheritance simplifies the analysis model by introducing a taxonomy The children classes inherit the attributes and operations of the parent class. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 39
Packages ¨ ¨ Packages help you to organize UML models to increase their readability We can use the UML package mechanism to organize classes into subsystems Account Bank ¨ Customer Any complex system can be decomposed into subsystems, where each subsystem is modeled as a package. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 40
Object Modeling in Practice Foo Amount Customer. Id Deposit() Withdraw() Get. Balance() Class Identification: Name of Class, Attributes and Methods Is Foo the right name? Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 41
Object Modeling in Practice: Brainstorming Foo “Dada” Amount Customer. Id Deposit() Withdraw() Get. Balance() Account Amount Customer. Id Is Foo the right name? Bernd Bruegge & Allen H. Dutoit Deposit() Withdraw() Get. Balance() Object-Oriented Software Engineering: Using UML, Patterns, and Java 42
Object Modeling in Practice: More classes Account Amount Account. Id Customer. Id Bank Deposit() Withdraw() Get. Balance() Name Customer. Id 1) Find New Classes 2) Review Names, Attributes and Methods Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 43
Object Modeling in Practice: Associations Account * ? Bank has Name Amount Account. Id Customer. Id Account. I d Deposit() Withdraw() Get. Balance() * Customer owns 2 Name Customer. Id 1) Find New Classes 2) Review Names, Attributes and Methods 3) Find Associations between Classes 4) Label the generic assocations 5) Determine the multiplicity of the assocations 6) Review associations Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 44
Practice Object Modeling: Find Taxonomies Account Bank * Name Savings Account Withdraw() Bernd Bruegge & Allen H. Dutoit Amount Account. Id Customer. Id Account. I d Deposit() Withdraw() Get. Balance() Checking Account Withdraw() * Customer Has Name Customer. Id() Mortgage Account Withdraw() Object-Oriented Software Engineering: Using UML, Patterns, and Java 45
Practice Object Modeling: Simplify, Organize Account Amount Account. Id Customer. Id Account. I d Deposit() Withdraw() Get. Balance() Savings Account Withdraw() Bernd Bruegge & Allen H. Dutoit Checking Account Withdraw() Show Taxonomies separately Mortgage Account Withdraw() Object-Oriented Software Engineering: Using UML, Patterns, and Java 46
Practice Object Modeling: Simplify, Organize Bank Name Account * Amount Account. Id Customer. Id Account. I d Deposit() Withdraw() Get. Balance() * Customer Has Name Customer. Id() Use the 7+-2 heuristics or better 5+-2! Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 47
Sequence Diagrams Focus on control flow ¨ Passenger Ticket. Machine w To refine use case descriptions w to find additional objects (“participating objects”) select. Zone() ¨ insert. Coins() pickup. Change() ¨ Bernd Bruegge & Allen H. Dutoit Used during system design w to refine subsystem interfaces Ticket. Machine zone 2 price Messages -> ¨ Instances are represented by rectangles. select. Zone() Actors by sticky figures. Operations on insert. Coins() participating Object ¨ Lifelines are represented by dashed pickup. Change() lines pick. Up. Ticket() ¨ pick. Up. Ticket() Used during analysis Messages are represented by arrows Activations are represented by narrow rectangles. Object-Oriented Software Engineering: Using UML, Patterns, and Java 48
Sequence Diagrams can also model the Flow of Data Zone. Button Passenger select. Zone() Tarif. Schedule Display lookup. Price(selection) price Dataflow display. Price(price) …continued on next slide. . . ¨ ¨ The source of an arrow indicates the activation which sent the message Horizontal dashed arrows indicate data flow, for example return results from a message Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 49
Sequence Diagrams: Iteration & Condition …continued from previous slide. . . Passenger Change. Processor *insert. Change(coin) Iteration Condition Coin. Identifier Display Coin. Drop lookup. Coin(coin) price display. Price(owed. Amount) [owed. Amount<0] return. Change(-owed. Amount) …continued on next slide. . . ¨ ¨ Iteration is denoted by a * preceding the message name Condition is denoted by boolean expression in [ ] before the message name Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 50
Creation and destruction …continued from previous slide. . . Passenger Change. Processor Creation of Ticket create. Ticket(selection) Ticket print() free() ¨ ¨ Destruction of Ticket Creation is denoted by a message arrow pointing to the object Destruction is denoted by an X mark at the end of the destruction activation w In garbage collection environments, destruction can be used to denote the end of the useful life of an object. Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 51
Sequence Diagram Properties ¨ ¨ UML sequence diagram represent behavior in terms of interactions Useful to identify or find missing objects Time consuming to build, but worth the investment Complement the class diagrams (which represent structure). Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 52
Activity Diagrams ¨ ¨ ¨ An activity diagram is a special case of a state chart diagram The states are activities (“functions”) An activity diagram is useful to depict the workflow in a system Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 53
Activity Diagrams allow to model Decisions Decision Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 54
Activity Diagrams can model Concurrency ¨ ¨ Synchronization of multiple activities Splitting the flow of control into multiple threads Splitting Bernd Bruegge & Allen H. Dutoit Synchronization Object-Oriented Software Engineering: Using UML, Patterns, and Java 55
Activity Diagrams: Grouping of Activities ¨ Activities may be grouped into swimlanes to denote the object or subsystem that implements the activities. Open Incident Allocate Resources Dispatcher Coordinate Resources Archive Incident Field. Officer Document Incident Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 56
Activity Diagram vs. Statechart Diagram for Incident Focus on the set of attributes of a single abstraction (object, system) Event causes state transition Active Inactive Incident. Handled Closed Incident. Documented Archived Incident. Archived Activity Diagram for Incident (Focus on dataflow in a system) Completion of activity causes state transition Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java Triggerless transition 57
UML Summary ¨ UML provides a wide variety of notations for representing many aspects of software development w Powerful, but complex ¨ UML is a programming language w Can be misused to generate unreadable models w Can be misunderstood when using too many exotic features ¨ We concentrated on a few notations: w Functional model: Use case diagram w Object model: class diagram w Dynamic model: sequence diagrams, statechart and activity diagrams Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 58
Additional References ¨ Martin Fowler w UML Distilled: A Brief Guide to the Standard Object Modeling Language, 3 rd ed. , Addison-Wesley, 2003 ¨ Grady Booch, James Rumbaugh, Ivar Jacobson w The Unified Modeling Language User Guide, Addison Wesley, 2 nd edition, 2005 ¨ Commercial UML tools w Rational Rose XDE for Java t http: //www-306. ibm. com/software/awdtools/developer/java/ w Together (Eclipse, MS Visual Studio, JBuilder) t ¨ http: //www. borland. com/us/products/together/index. html Open Source UML tools w http: //java-source. net/open-source/uml-modeling w Argo. UML, UMLet, Violet, … Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 59
- Slides: 56