Head First Object-Oriented Analysis and...

27
Beijing Cambridge Köln Paris Sebastopol Taipei Tokyo Brett D. McLaughlin Gary Pollice David West Head First Object-Oriented Analysis and Design Wouldn’t it be dreamy if there was an analysis and design book that was more fun than going to an HR benefits meeting? It’s probably nothing but a fantasy...

Transcript of Head First Object-Oriented Analysis and...

Page 1: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

Beijing • Cambridge • Köln • Paris • Sebastopol • Taipei • Tokyo

Brett D. McLaughlin Gary Pollice David West

Head First Object-Oriented Analysis and Design

Wouldn’t it be dreamy if there was an analysis and

design book that was more fun than going to an HR benefits

meeting? It’s probably nothing but a fantasy...

Page 2: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

Head First Object-Oriented Analysis and Designby Brett D. McLaughlin, Gary Pollice, and David West

Copyright © 2007 O’Reilly Media, Inc. All rights reserved.

Printed in the United States of America.

Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected].

Series Creators: Kathy Sierra, Bert Bates

Series Editor: Brett D. McLaughlin

Editor: Mary O’Brien

Cover Designer: Mike Kohnke, Edie Freedman

OO: Brett D. McLaughlin

A: David West

D: Gary Pollice

Page Viewer: Dean and Robbie McLaughlin

Printing History:November 2006: First Edition.

The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Head First series designations, Head First OOA&D, and related trade dress are trademarks of O’Reilly Media, Inc.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps.

While every precaution has been taken in the preparation of this book, the publisher and the authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.

In other words, if you use anything in Head First OOA&D to, say, write code that controls an American space shuttle, you’re on your own.

No dogs, rabbits, or woodchucks were harmed in the making of this book, or Todd and Gina’s dog door.

ISBN-10: 0-596-00867-8

ISBN-13: 978-0-596-00867-3

[M]

Page 3: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

this is a new supplement 3

gathering requirements: academic supplement2

Everybody loves a satisfied customer. You already know

that the fi rst step in writing great software is making sure it does what the

customer wants it to. But how do you fi gure out what a customer really

wants? And how do you make sure that the customer even knows what

they really want? That’s where good requirements come in, and in this

chapter, you’re going to learn how to satisfy your customer by making

sure what you deliver is actually what they asked for. By the time you’re

done, all of your projects will be “satisfaction guaranteed,” and you’ll be

well on your way to writing great software, every time.

Give Them What They WantI hope you like it... I’ve been

paying attention to every word you’ve said lately, and I think this is just perfect for you!

Page 4: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

�    Chapter 2 Academic Supplement

gathering requirements

Short Answers

OOA&

D 2

1. Go back to Chapter 1 and find use cases for the Rick’s Guitars inventory application. Identify only those that you can justify from the text in the chapter, not ones that you think should be in the inventory system. Provide a name for each use case, a brief description (we called them goals in the text), and the actors (external initiators and other external participants) that participate in the use case.

2. The inventory system from Chapter 1 is incomplete. There are several things that could be added (you may have done this if you answered the right question from Chapter 1) to make it complete. If you were Rick, what would you add? Specify these as use cases. Provide the name, brief description, and actors for each.

Page 5: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

you are here 4    �

gathering requirements

Short Answers

OOA&D 2

3. Research other types of requirements specification techniques. Consider looking at the following type of requirements specifications:

FeaturesUser storiesSoftware Requirement Specification (SRS)

Compare them to use cases. Where do you think they might work better than use cases? Why? Where are use cases better? Why?

A.B.C.

4. Write user stories for the dog door system that you built for Doug in this chapter.

Page 6: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

�    Chapter 2 Academic Supplement

gathering requirements

Short Answers

OOA&

D 2

5. When identifying use cases, we find a recurring pattern. For any type of object—like inventory items for Rick, sounds for the animal door, and so on—we will usually have to create an item, retrieve an item, update an item, and delete an item. These are called the CRUD operations. There are three options for writing use cases in this situation:

Write a separate use case for each operation.Write one use case for all of the operations (a CRUD use case).Omit the use cases from the specification.

Which of the choices do you think is most appropriate in general? Why?

A.B.C.

6. Name all of the actors involved in the final system you developed in Chapter 2.

Page 7: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

you are here 4    �

gathering requirements

Short Answers

OOA&D 2

7. We’ve written quite a few requirements in this chapter in the form of use cases. But they’re not quite what the UML lawyers would consider real use cases. They’re more like a set of specific scenarios. Look in a UML book or some of the Web references for UML, and see if you can see why what we’ve done (like Todd and Gina’s Dog Door Version 2.0 on page 70) is not really a use case. Can you fix the example on page 70 to make the UML lawyers happier?

8. We defined a requirement on page 62 of the text as a specific thing your system has to do to work correctly. There is a more formal definition in the Scholar’s Corner on that page. These are not the only definitions used to define a requirement. There are many others that are used by different authors and practitioners. Do a little research and find five other definitions of a requirement. Write each of them along with a reference to where you found them. Do they all convey the same basic idea? If not, how do they differ? Which definition do you prefer and why?

Page 8: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

�    Chapter 2 Academic Supplement

gathering requirements

Short Answers

OOA&

D 2

9. Consider the conversation with Todd and Gina on page 63. What might you need clarification about? What questions would you ask to get the information you need?

10. Requirements must be testable. Does the test simulator program on page 83 test each requirement? Add comments to the program to show which requirements are tested by the code. If not all requirements are tested, identify which of them are missing from your simulator.

Page 9: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

you are here 4    �

gathering requirements

Short Answers

OOA&D 2

11. Can you think of a way to keep track of which requirements are tested by your test programs?

12. Do you think it’s easier or harder to write JUnit tests compared to the type of test programs like the simulator programs used in this chapter? Why?

Page 10: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

10    Chapter 2 Academic Supplement

gathering requirements

Short Answers

OOA&

D 2

13. An object has behavior and state. Look at one of the dog door implementations. Identify the behavior and state of each object (class instance). Does the implementation provide a way to determine the state? Is it necessary to provide a way to do so? Why or why not?

14. On page 69, we mention that the more problems you can think of, the less fragile you can make your code. What does it mean for code to be fragile? How would you describe code that is not fragile? What are possible ways to avoid fragile code?

Page 11: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

you are here 4    11

gathering requirements

Short Answers

OOA&D 2

15. Look at each of the use cases in this chapter and the versions of the program we implemented to satisfy those requirements. Based upon the information provided by the requirements and the code, think about what can go wrong? Make a list of all the problems you can think of, and write down a way to ensure that each problem can be avoided.

16. We’ve concentrated on user requirements in this chapter. Certainly, the user (customer) is the main person we have to satisfy, since they are the one who will ultimately determine if our product is successful or not by purchasing it. However, the customer is not the only one who has an interest in our product. There are many such people—we call them stakeholders—that we must satisfy. Consider the application in this chapter. Who, besides the dog owners, are stakeholders? Can you think of the type of requirements they might provide for you?

Hint: The problems are not always in the

software; it just seems that way. For example,

something might go wrong with the hardware.

Page 12: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

12    Chapter 2 Academic Supplement

gathering requirements

Short Answers

OOA&

D 2

17. There are many things other than what we’ve shown in the book that can go into use cases. As your application or project becomes larger, and uses a more formal development process, you may choose to add many of these to your use cases. (Reference: Writing Effective Use Cases by Alistair Cockburn [Addison-Wesley]).

Two features of use cases that can be useful are preconditions and post-conditions. Preconditions are statements (predicates) that must be true before the use case be started. Post-conditions are statements (predicates) that must be true when the use case ends successfully. Add preconditions and post-conditions to the use cases in this chapter.

18. The Object Management Group (OMG) is the organization that manages the development of the Unified Modeling Language (UML). If you go to the OMG web site (http://www.omg.org), you can find information on the Object Constraint Language that is used to specify constraints on classes and objects in a formal specification language.

Try to take the preconditions and post-conditions you wrote in the previous problem and write them using OCL. (Reference: “Formally speaking: How to apply OCL,” http://www.ibm.com/developerworks/rational/library/5390.html)

Advanced

Page 13: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

you are here 4    13

gathering requirements

Short Answers

OOA&D 2

19. If you were to add a graphical user interface (GUI) to the final dog door system, what would it look like? Write the additional requirements for this GUI.

20. You can write use cases at many different levels of abstraction for a system. If you write them at the very lowest level of detail, you will have many use cases for even a simple system. Most of these will describe a fairly simple scenario. Managing many use cases can add quite a lot of overhead. You would like to have as few use cases as possible, but as many as you need, for your requirements. In this chapter we wrote several use cases for different customers. Look at the use cases we wrote, starting on page 95, and see if you can reduce the number of use cases where each use cases captures the essence of the problem, goal, and requirements. Write the new use cases and show how they cover the same requirements as the use cases in the chapter.

Hint: A good requirement states precisely what the system must do, not how it will do it. Make sure that you don’t design the solution while specifying the requirements.

Hint: For each of the different customers, the specific use cases all describe the same thing—letting the dog out to go to the bathroom.

Page 14: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

1�    Chapter 2 Academic Supplement

gathering requirements

Short Answers

OOA&

D 2

21. Use cases are not the only way to specify functional requirements. User stories (see question #3) are another method that has become popular for Agile teams (References: http://www.agilemanifesto.org, http://www.agilealliance.org) Write user stories that describe the same functional requirements for the final dog door system in this chapter (References: http://codepoets.co.uk/node/250, http://codebetter.com/blogs/scott.bellware/archive/2005/12/30/136036.aspx, http://xp123.com/xplor/xp0002g/index.shtml, and many others).

Page 15: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&D 2

you are here 4    1�

gathering requirements

Write JUnit tests for the dog door programs in this chapter.

Page 16: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&

D 2

1�    Chapter 2 Academic Supplement

gathering requirements

If you answered question 15, write code to fix the problems and test your solution.

Page 17: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&D 2

you are here 4    1�

gathering requirements

Write a small GUI for the final dog door system like you specified in question 19.

Page 18: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&

D 2

1�    Chapter 2 Academic Supplement

gathering requirements

If you didn’t write tests for question 3, write them now.Were you able to use JUnit? Why or why not?

Page 19: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&D 2

you are here 4    1�

gathering requirements

Make the dog door remain open for a variable amount of time after the dog passes through. Make sure that you provide a way for the customer to set the value.

Page 20: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&

D 2

20    Chapter 2 Academic Supplement

gathering requirements

Take the final version of the dog door application in this chapter and identify all possible variable parts (such as the amount of time that the door is open).Modify your application to allow the customer to configure the application with default values. Test to make sure your modifications work.Write a short description of the “design process” you went through when you made the modifications.

Page 21: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&D 2

you are here 4    21

gathering requirements

Good programs are not just correct, they are elegant. Just as a good writer can craft a beautiful story using just the right words and combinations of styles, good programmers can write programs that are works of art of a different kind. Good programs must be readable and understandable.This exercise requires you to work in pairs. Each person (A) will give one of their programs to the other person in the pair (B) to review. The program should include the tests as well as the implementation code. The reviewer will add comments to the program as they look over it. The reviewer should address the following areas:

Naming: Are the class, field, and method names meaningful and consistently formatted?Indentation: Does the author use a consistent indentation scheme?Other formatting: Does the author use a consistent style for placing parentheses, braces, and other syntactic elements in the program.Readability and understandability: Can you read the program without difficulty? Is the organization such that it makes sense to you? Do you understand how the program is designed and what each method and variable is used for?Correctness: Understandable programs are not necessarily correct. What problems can you find in the program? You should not fix the program (that’s not the reviewer’s job), but just point out the problem and why it’s a problem.

The partners should then meet again and go over the reviews. Each should write a synopsis of their impressions of the review. Is it worth the time? What did they find out about their program and programming style? What problems were uncovered in their program, and why were the problems there?

A.B.C.

D.

E.

Page 22: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&

D 2

22    Chapter 2 Academic Supplement

gathering requirements

1

Your rich Aunt Millie gave you a great graduation gift, enough money to retire on before you even start your career (you always did like Aunt Millie). With your newfound wealth, you decided to become a world traveler. This gave you the opportunity to acquire some very good photography equipment and pursue your passion for taking great pictures of people and places all over the world. You’ve become recognized as a fine photographer and amassed many thousands of digital pictures.You’d like a program to help organize, catalog, and manage your files. You plan on offering gallery-quality images for sale on a web site (that will come later). You need as much flexibility as possible in cataloging the photos as well as locating just the perfect photograph, based upon some sort of specification. There are many things that might be important for your specific needs.

You may have used some programs that let you manage digital photographs and liked many of their features or found some of them lacking. Do some research on at least three (your instructor may require more) programs and construct a table that shows the features available for each of the programs.

a)

This is a good place to start identifying requirements. Even if you aren’t the customer, it certainly helps to understand the type of things that are available in your competitors’ products.

Page 23: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&D 2

you are here 4    23

gathering requirements

Write down the features you think are important in the program you want to have developed to manage your photographs. Describe each feature briefly (one or two sentences) to make sure the programmer has enough to know what they don’t know and ask some intelligent questions (other than how much will you pay for the program).

b)

Continues on the next page.

Take the features from your list in (b) and rank them from the most important to the least important. That is, the most important one is something that the program must have, or you won’t pay for it. As you go down the list, you should get to things that you could be talked out of if it really would cost too much, as well as things that would be nice to have (like perhaps the ability to add voice files to describe the photo) that you really could do without, but that would delight you if they were there.

c)

Page 24: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&

D 2

2�    Chapter 2 Academic Supplement

gathering requirements

1 (continued)

If you are the programmer who is going to develop the photo manager, where would you start? Why?

d)

What classes do you think you might need in order to implement the functionality described in (d)? How did you arrive at that specific set of classes?

e)

Page 25: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&D 2

you are here 4    2�

gathering requirements

Draw a class diagram for the classes in (e). Show just the class name and important methods for each class and draw an undirected association (that’s a fancy way of saying draw a line) between classes that actually collaborate directly with each other (for example, the DogDoor and Remote classes from the early dog door example in the text).

f)

We’ll get to associations later, but it’s not too early to start thinking about collaborating objects.

Page 26: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&

D 2

2�    Chapter 2 Academic Supplement

gathering requirements

2

Your friend Chris heard of your awesome idea for a photo manager. He’s an avid audiophile and has thousands of songs on his computer (all legally purchased, of course). He’d like a program to manage his music that seems pretty similar to what you want for your photos. You give him the number of your programming company (555-GEEK) and tell him to give them a call.

If you were the programmer who implemented the photo manager, what would you do first?

a)

Do you think you could reuse some of the photo manager program for the music manager program? Why or why not?

b)

Assuming that the set of requirements for the music manager is quite similar to the one for the photo manager (okay, you’re not going to print gallery-quality songs), and you would start with the same, or similar, functionality described in (d) of the previous exercise, what changes would you have to make?

c)

Page 27: Head First Object-Oriented Analysis and Designheadfirstlabs.com/books/hfooad/academic/HeadFirstOOAD_as02.pdf · this is a new supplement 3 2 gathering requirements: academic supplement

OOA&D 2

you are here 4    2�

gathering requirements

How might you restructure your classes to make your program more flexible so that when Chris’s sister Sally wants to manage her address book, including comments about all of her friends, especially the boyfriends she’s had, you would be able to adapt the work you’ve done without having to make massive changes?

d)

Do you think it’s practical to think about how you can take into account the future adaptations of your program before you begin the first, photo manager program? Why or why not?

e)