Chapter 1 Modeling and the UML



Similar documents
UML Tutorial: Part 1 -- Class Diagrams.

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

Ready, Set, Go! Math Games for Serious Minds

VHDL Test Bench Tutorial

Using UML Part One Structural Modeling Diagrams

The Crescent Primary School Calculation Policy

Instructional Systems Design

Socci Sport Alternative Games

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

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

1 of 7 9/5/2009 6:12 PM

Open XML Court Interface (OXCI) Architecture Proposal

Board Games They are adaptations of classic board games. Examples of board games include Chess, Checkers, Backgammon, Scrabble and Monopoly.

Part 1 Foundations of object orientation

Chapter 6: Programming Languages

UML Class Diagrams (1.8.7) 9/2/2009

UML FOR OBJECTIVE-C. Excel Software

Grade 7 Mathematics. Unit 2. Integers. Estimated Time: 15 Hours

UML Diagram Types. Use Cases do the Following. Use Case Diagram

There are a number of superb online resources as well that provide excellent blackjack information as well. We recommend the following web sites:

Tell Your Friends! IMPORTANT! PLEASE READ BEFORE USING CONTROLLER!

Lab 11. Simulations. The Concept

Sequence Diagram Tutorial. From: UML Distilled, Third Edition, Chapter 4 M. Fowler

Object Oriented Programming. Risk Management

That s Not Fair! ASSESSMENT #HSMA20. Benchmark Grades: 9-12

Acceleration of Gravity Lab Basic Version

Fundamentals of Probability

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

OFFICIAL BASKETBALL RULES SUMMARY OF CHANGES 2014

ATM Case Study Part 1

Introduction to Fractions, Equivalent and Simplifying (1-2 days)

KE =? v o. Page 1 of 12

CONNECTING LESSONS NGSS STANDARD

UML for the C programming language.

Addition Methods. Methods Jottings Expanded Compact Examples = 15

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

The Progression from 4v4 to 11v11

Blender Notes. Introduction to Digital Modelling and Animation in Design Blender Tutorial - week 9 The Game Engine

The resulting tile cannot merge with another tile again in the same move. When a 2048 tile is created, the player wins.

Chapter 7: Momentum and Impulse

ACTIVITY 6: Falling Objects

A flowchart is not a state machine

SOFTWARE DESIGN TECHNIQUES. Nagalaxmi Telkar CSCI 5828 Presentation Slides

How to Play. It s a Random, Rapid-Fire Card Shoot-Out!

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

History OOP languages Year Language 1967 Simula Smalltalk

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Umbrello UML Modeller Handbook

Technical document. Group 3 Mate Tomin Pieter van Ede Raymond Weijermars Daniel Faustino Stefan Hospes

Force on Moving Charges in a Magnetic Field

2 SYSTEM DESCRIPTION TECHNIQUES

Current California Math Standards Balanced Equations

Decimals and Percentages

ARCHERY Environmental Education Lesson EDWARDS CAMP AND CONFERENCE CENTER

Hoover High School Math League. Counting and Probability

8.2. Solution by Inverse Matrix Method. Introduction. Prerequisites. Learning Outcomes

Toad for Oracle 8.6 SQL Tuning

Name of Lesson: Properties of Equality A Review. Mathematical Topic: The Four Properties of Equality. Course: Algebra I

L20: GPU Architecture and Models

Going Interactive: Combining Ad-Hoc and Regression Testing

Work, Energy & Momentum Homework Packet Worksheet 1: This is a lot of work!

Using Rational Rose to Create Object-Oriented Diagrams

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

MATHEMATICS: REPEATING AND GROWING PATTERNS First Grade. Kelsey McMahan. Winter 2012 Creative Learning Experiences

Object-oriented design methodologies

Speed, velocity and acceleration

Fifth Grade Physical Education Activities

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

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

Team Defending. Understand the terms that describe the roles of players and parts of the field. The person you are marking.

CmpSci 187: Programming with Data Structures Spring 2015

Use Square Roots to Solve Quadratic Equations

Friction and Gravity. Friction. Section 2. The Causes of Friction

Multiplication. Year 1 multiply with concrete objects, arrays and pictorial representations

CHAPTER 14 Understanding an App s Architecture

Ch5: Discrete Probability Distributions Section 5-1: Probability Distribution

25 Integers: Addition and Subtraction

SHOOTING FUNDAMENTALS

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

Converting from Fractions to Decimals

Numeracy Targets. I can count at least 20 objects

Unification of AOP and FOP in Model Driven Development

SIXTEEN PLAYERS, TWO DICE, ONE SHOOTER

Unit 6 Number and Operations in Base Ten: Decimals

WELCOME TO PAYDAY 2 CRIME.NET

Dr. Pat Mirenda. Software Design Specification Document

CSE 1020 Introduction to Computer Science I A sample nal exam

IDPA 3-gun Shoot Spring 2013 Stage instructions

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

Chapter 3. Technology review Introduction

10 FREE BASKETBALL DRILLS

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Charlesworth School Year Group Maths Targets

Rose/Architect: a tool to visualize architecture

not necessarily strictly sequential feedback loops exist, i.e. may need to revisit earlier stages during a later stage

LAB 6 - GRAVITATIONAL AND PASSIVE FORCES

Transcription:

Chapter 1 Modeling and the UML The source code of an object-oriented software application is an implementation, in some programming language, of an object-oriented model. model: an approximate mental or visual image of a software system that serves as an aid in designing the system and in implementing the system in a programming language; a picture is worth a thousand words (or many lines of code) The Unified Modeling Language (UML) was created as a standard means of representing models for object-oriented software. The UML consists of several kinds of diagram, but we will restrict our use to the class diagram, or static-structure diagram. The class diagram allows the modeler to describe classifiers (kinds of software entities) and relationships among classifiers. The remainder of this chapter will be devoted to creating a UML class diagram for a small object-oriented software architecture. Our final diagram will show the four primary kinds of relationship between classifiers. Your objective for this chapter should be to understand those four relationships and their expression in the UML. 1.1 First-Person Shooter You have undoubtedly played, or at least seen played, a first-person shooter game. A key enjoyment in first-person shooter games is accumulating exotic weapons with which to kill one's opponents. We will model the weapons in such a game. In an actual game, our weapons model would be embedded in a much larger model that would include players, buildings, weather, etc.; for the sake of simplicity, we will not consider these other components.

2 Chapter 1 Modeling and the UML 1.1.1 Classifiers In the author's favorite first-person shooter there are only projectile weapons, like shotguns or rail guns, and hand weapons, like fists or chainsaws. It seems reasonable to distinguish between weapons of these two types in our model, given their striking differences, the most obvious of which is that a projectile weapon has ammo while a hand weapon does not. A UML representation of the distinction between projectile and hand weapons appears below in Figure 1.1. Fig. 1.1. UML class diagram representing two kinds of weapon in a computer game Notice that each classifier is represented by a rectangle having three compartments. The classifier's name appears in the top compartment and the classifier's attributes and operations appear in the middle and bottom compartments, respectively. attribute: a characteristic, or trait, of a classifier; the values of an object's attributes are collectively referred to as the object's state operation: a behavior, or action, of a classifier Like any kind of real-world entity, a classifier's identity is determined by its characteristics and by the behaviors that follow from those characteristics. Balls can roll because they are round. The attribute round gives rise to the behavior roll.

1.1 First-Person Shooter 3 1.1.2 Generalization The classifiers ProjectileWeapon and HandWeapon are connected to Weapon by solid arrows with unfilled triangular heads. This type of arrow simultaneously denotes generalization and specialization. generalization: a relationship between classifiers in which one classifier is said to be the kind of the other; e.g., a chair is a kind of furniture is a kind of manmade object, and so we might say that manmade object generalizes furniture, which generalizes chair; specialization is generalization viewed from the other direction chair specializes furniture, which specializes manmade object; the acronym ako, which signifies "a kind of," may be used in place of the term specializes The generalization arrows in Figure 1.1 tell us that Weapon generalizes, and therefore is specialized by, both ProjectileWeapon and Hand- Weapon. When classifier P generalizes classifier C, P is said to be the parent of C and C is said to be the child of P. A child classifier inherits all protected and public attributes and operations of its parent. A child classifier extends its parent in some way. For example, every Weapon has a real-number attribute called its mass. Because Weapon is the parent of ProjectileWeapon, a ProjectileWeapon also has the mass attribute. (A Weapon's mass might be used by the game to calculate the Weapon's weight; the weight would be used to adjust the acceleration and top speed of the player carrying the Weapon. A rocket launcher will slow its carrier down far more than will a silenced pistol.) In addition, ProjectileWeapon has an integer attribute called maxammo, which serves to distinguish ProjectileWeapons from HandWeapons. Class Weapon's second attribute, attackinterval, is of type int. This attribute corresponds to the time required to reload or recharge a projectile weapon, or in the case of a hand weapon, the time required to, say, throw another punch or draw back a whip in preparation for another crack.

4 Chapter 1 Modeling and the UML 1.1.3 Association A Weapon also possesses a String called name. Because String is a class type rather than a primitive type, and we are creating a class diagram, we include the classifier String and show its relationship to Weapon. The UML term for the relationship is association, and it is denoted by a solid line. The term has-a is often used in place of association: a Weapon has-a String. Figure 1.2 shows the updated diagram. Fig. 1.2. UML class diagram depicting the has-a relationship between classes Weapon and String Each Weapon object will of course need to be displayed, or rendered, many times as the game is played, and the Weapon's appearance must vary depending on the perspective from which it is being viewed. This can be accomplished by giving each Weapon a collection of images, with each image showing the Weapon as it should appear from a particular perspective. In our model, class Weapon will have an array, images, of type Bitmap. Figure 1.3 shows this has-a relationship between classes Weapon and Bitmap.

1.1 First-Person Shooter 5 Fig. 1.3. UML class diagram depicting Weapon's association with Bitmap A projectile weapon, like a shotgun, makes a sound when it is fired or reloaded. And hand weapons also make sounds. A chainsaw, for example, idles when it is not being used to cut down an opponent, and makes a loud and rather annoying whine when its throttle is depressed. We will incorporate this characteristic of weapons into our model by giving class Weapon an array of type Sound. Figure 1.4 shows the new diagram. Fig. 1.4. UML class diagram depicting Weapon's association with Sound

6 Chapter 1 Modeling and the UML 1.1.4 Dependency When a Weapon object is called upon to render itself, the object must be given certain information about how and where to do so. A Weapon might need to know where it is to appear on the display, by how much it should scale its size so as to create the illusion of distance, or how bright it should appear given the light level in its virtual environment. This information is collectively known as the object's current display context. The display context must not be an attribute of the Weapon object, for the Weapon's display context at any given moment has nothing to do with the Weapon itself, but is instead determined entirely by the Weapon's virtual environment. In other words, a given Weapon's display context is provided when the game requires the object to appear on the display. If the Weapon must be displayed again a fraction of a second later, its display context will probably have changed, and so the game must provide the Weapon with the new context. The Weapon will then use the new context to carry out its rendering operation. Class Weapon's render operation, and that operation's dependence on a fleeting display context, are shown in Figure 1.5. The relationship between classes Weapon and DisplayContext is called dependency, or uses-a. A Weapon uses-a DisplayContext to render itself. The dependency relationship is expressed in the UML by a dashed arrow with an open head. Fig. 1.5. UML class diagram depicting the uses-a relationship between classes Weapon and DisplayContext

1.1 First-Person Shooter 7 1.1.5 Realization It is typical of first-person shooter games that at least several of each type of weapon are scattered throughout the game world, waiting to be found by players. It is desirable, then, that our model include some mechanism for copying a Weapon. It just so happens that the Java platform offers an interface called Cloneable for this very purpose. Any Java class that realizes interface Cloneable includes a method called clone. The clone method returns a copy of the object that called it. Figure 1.6 shows the realization relationship between class Weapon and interface Cloneable. In the UML, realization is indicated by a dashed arrow with an unfilled triangular head. Cloneable is stereotyped to make clearer the fact that it is an interface rather than a class. The «and» are called guillemets. We will soon have a closer look at interfaces. For now it suffices to say that realization is a kind of relationship between a class and an interface. Fig. 1.6. UML class diagram depicting Weapon's realization of Cloneable

8 Chapter 1 Modeling and the UML 1.2 From UML to Code The diagram of Figure 1.6 is not merely a pictorial representation of our model's structure. The diagram is also equivalent to Java code that implements the structure. That code can be generated from the UML diagram by most any UML software package. The programmer may then complete the implementation by coding bodies for the stubbed methods and by adding any fields or methods not shown in the model. The Java code corresponding to the diagram of Figure 1.6 is shown below. Each class definition would of course be in its own file. public class Bitmap {} public class DisplayContext {} public class Sound {} public abstract class Weapon implements Cloneable { protected float mass; protected int attackinterval; protected String name; protected Bitmap[] images; protected Sound[] sounds; } public void render(displaycontext dc) {} public Object clone() {} public class HandWeapon extends Weapon { protected float range; } public class ProjectileWeapon extends Weapon { protected int maxammo; }

Exercises 9 Exercises 1. Define association. 2. Define dependency. 3. In our weapons model, the dependency between classes Weapon and DisplayContext occurs by way of an operation's parameter. What other forms can a dependency take? 4. What relationship is missing from Figure 1.6? Hint: Consider your answer to Exercise 3. 5. Extend the model of Figure 1.6 so that it includes ammo. 6. Does the Java platform offer a class that we could use in place of the fictitious class Bitmap? 7. Does the Java platform offer a class that we could use in place of the fictitious class Sound? 8. Model a class called PlayingCard, a deck(s) of which could be used in a Blackjack simulation or interactive game. Class PlayingCard should realize java.io.serializable. A Serializable object can easily be transmitted across a network, which would be useful to someone developing an online casino application, for example.