Head First Object-Oriented Analysis and Design



Similar documents
Statistics. Head First. A Brain-Friendly Guide. Dawn Griffiths

Object Oriented Design

Object-oriented design methodologies

How To Design Software

The «include» and «extend» Relationships in Use Case Models

How Designs Differ By: Rebecca J. Wirfs-Brock

UML Tutorial: Part 1 -- Class Diagrams.

Percerons: A web-service suite that enhance software development process

Google Maps Hacks by Rich Gibson and Schuyler Erle

Software Engineering (CS550)

EVALUATING METRICS AT CLASS AND METHOD LEVEL FOR JAVA PROGRAMS USING KNOWLEDGE BASED SYSTEMS

WORKSHEET ONE FIRST ACTIVITY. Treating others with respect. List of ideas for treating others with respect

The 5 P s in Problem Solving *prob lem: a source of perplexity, distress, or vexation. *solve: to find a solution, explanation, or answer for

IT Audit Dallas Parkway, Suite 960 Addison, Texas Phone: Fax:

Writing Thesis Defense Papers

Lesson 2: How to Give Compliments to Tutees

Book Review of Rosenhouse, The Monty Hall Problem. Leslie Burkholder 1

Easy Casino Profits. Congratulations!!

Definitions. Software Metrics. Why Measure Software? Example Metrics. Software Engineering. Determine quality of the current product or process

What Are Certificates?

Bullying 101: Guide for Middle and High School Students

THE LITTLE BIG BOOK OF BADNESS

CCNA : Cisco Certified Network Associate Study Guide, 5th Edition ( ) Todd Lammle

The Top 3 Common Mistakes Men Make That Blow All Their Chances of Getting Their Ex-Girlfriend Back Which of these mistakes are you making?

CAs and Turing Machines. The Basis for Universal Computation

How To Calculate Class Cohesion

Incorporating Aspects into the UML

Exercise 8: SRS - Student Registration System

2.2 Derivative as a Function

Math: Study Skills, Note Taking Skills, And Test Taking Strategies

Chapter 1 Modeling and the UML

Lecture 1. Basic Concepts of Set Theory, Functions and Relations

May 25th, "Car Buying: How to Avoid the Extra Stress"--Mary Dittfurth

Masters of Science in Software & Information Systems

Software Development: An Introduction

PROJECT MANAGEMENT METHODOLOGY OF OBJECT- ORIENTED SOFTWARE DEVELOPMENT

Contents. Introduction and System Engineering 1. Introduction 2. Software Process and Methodology 16. System Engineering 53

Advice to USENIX authors: preparing presentation slides

Using Use Cases for requirements capture. Pete McBreen McBreen.Consulting

Why & How: Business Data Modelling. It should be a requirement of the job that business analysts document process AND data requirements

Mental Health Role Plays

Backyard Visitor by Kelly Hashway

King Midas & the Golden Touch

DISCLAIMER OVERVIEW WHY DO WE MODEL WHAT IS QUALITY? Jeff Jacobs,

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

SERVICE-ORIENTED IT ORGANIZATION

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

A UML Introduction Tutorial

Learning Objectives. Chapter 6. Market Structures. Market Structures (cont.) The Two Extremes: Perfect Competition and Pure Monopoly

In this Lecture you will Learn: Systems Development Methodologies. Why Methodology? Why Methodology?

Augmented reality enhances learning at Manchester School of Medicine

FREE CHORD BOOK Introduction

Chapter 2 How To Cheat A Barre Chord

Development Methodologies. Types of Methodologies. Example Methodologies. Dr. James A. Bednar. Dr. David Robertson

MODERN APPLICATIONS OF PYTHAGORAS S THEOREM

Software Engineering. System Models. Based on Software Engineering, 7 th Edition by Ian Sommerville

UML Activity Diagrams: Versatile Roadmaps for Understanding System Behavior

chapter >> Consumer and Producer Surplus Section 3: Consumer Surplus, Producer Surplus, and the Gains from Trade

The Commission Cutting Report

Styles of leadership. Styles of leadership

Ukulele In A Day. by Alistair Wood FOR. A John Wiley and Sons, Ltd, Publication

How To Proofread

Temptation. A Youth Lesson for Grades 3-5, 6-8, & 9-12

BBC Learning English Talk about English Business Language To Go Part 2 - Induction

Decision Making under Uncertainty

TOGAF usage in outsourcing of software development

MAXIMUS Telephone Enrollment- Phase I Call Center Script

Design methods. List of possible design methods. Functional decomposition. Data flow design. Functional decomposition. Data Flow Design (SA/SD)

Rose/Architect: a tool to visualize architecture

One natural response would be to cite evidence of past mornings, and give something like the following argument:

II. TYPES OF LEVEL A.

Vieta s Formulas and the Identity Theorem

The Purchase Price in M&A Deals

Philosophical argument

Chap 4. Using Metrics To Manage Software Risks

Programming Language Constructs as Basis for Software Architectures

Chapter 8 The Enhanced Entity- Relationship (EER) Model

(Refer Slide Time: 2:03)

SAMPLE TEST INFORMATION -- English Language Test (ELT) General Information There are three parts to the ELT: Listening, Reading, and Writing.

REALISTIC THINKING. How to Do It

Time needed. Before the lesson Assessment task:

Manufacturers versus Component Part and Raw Material Suppliers: How to Prevent Liability By Kenneth Ross *

The Doctor-Patient Relationship

Karunya University Dept. of Information Technology

Chapter 24 - Quality Management. Lecture 1. Chapter 24 Quality management

Balancing Work and Personal Life

PEER PRESSURE TEACHER S GUIDE:

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

Analyzing Java Software by Combining Metrics and Program Visualization

sales tools laying the proper groundwork for the client to say yes FOR AGENT USE ONLY

SECRET LOVE. Wonderful Illusion

Transcription:

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... Brett D. McLaughlin Gary Pollice David West Beijing Cambridge Köln Paris Sebastopol Taipei Tokyo

Head First Object-Oriented Analysis and Design by 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 corporate@oreilly.com. 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]

5 good design = flexible software: academic supplement Nothing Ever Stays the Same Molly, I hope we never have to grow up. Let s just stay like this forever! Change is inevitable. No matter how much you like your software right now, it s probably going to change tomorrow. And the harder you make it for your software to change, the more diffi cult it s going to be to respond to your customer s changing needs. In this chapter, we re going to revisit an old friend, try and improve an existing software project, and see how small changes can turn into big problems. In fact, we re going to uncover a problem so big that it will take a TWO-PART chapter to solve it! this is a new supplement 3

1. One UML element that adds to a model s understandability is called a stereotype. A stereotype lets you create new kinds of building blocks similar to existing ones but specific to your problem. (The Unified Modeling Language User Guide, by Grady Booch, James Rumbaugh, and Ivar Jacobson, Addison-Wesley) A stereotype is rendered in UML diagrams as a name enclosed by French quotation marks, called guillemets (). Stereotypes may be used on any UML modeling element. In the following diagrams, you are asked to place the appropriate stereotypes on the elements where they belong. You may need to look at a UML reference to learn what some of the notation means. (It s good to have to learn some things on your own!). A. Show stereotypes for the «include» and «extend» relationships in the following use case diagram. Yes, we know we said that you shouldn t use them, but some people do, and you should know what they look like. Enroll as part-time student Student Enroll in university Enroll in course Login Chapter 5 Academic Supplement

1. B. Show stereotypes for «interface» and «enumeration» in the following diagram. Transport IFlyer LandTransport AirTransport Jet model +B737 JetModel +B747 +A300 you are here 4 5

2. Why don t you need a stereotype, «abstract», for abstract classes? Would it be wrong to use one? 3. What is the difference between an interface and an abstract class? How do you decide which is the best one to use? (We re not talking about any specific language or implementation but about general object-oriented concepts. After all, by the time you graduate, Java will be so yesterday.) Chapter 5 Academic Supplement

4. Look at the class diagram on page 199. Does showing the properties of the class with associations make the model clearer or not? Why? 5. The name of the association, such as spec, as shown in the diagram on page 199 is called the role name. Why do you think it is called that? Do you think it s a good description of what the name stands for? you are here 4

6. In the diagram 1B earlier, the Jet is derived from AirTransport. Does this mean that the Jet is an AirTransport? Is this always the case with generalization (inheritance)? Hint: the answer may surprise you. We ll get to it later, but this will get you thinking about these deep philosophical and practical issues. 7. We introduced the notation for aggregation in this chapter. The following four associations between a Vehicle class and a Wheel class use different UML notations. Vehicle Wheel What is the difference in meaning for each? Which do you prefer and why? Does it really matter? Vehicle 1 * Wheel Vehicle 1 Wheel * Vehicle Wheel 8 Chapter 5 Academic Supplement

8. Create a UML model of your current OOA&D course. Include the people, assignments, books, and anything else that is appropriate for your class and institution. Is this analysis or design? 9. Revise your model from question 8 so that it reflects the classes you might use in implementing a simulation of your OOA&D course in software. Use interfaces and abstract classes where appropriate. Describe your design decisions (like why you used a particular interface, and so on). Is this analysis or design? Do all of the classes have behavior, or are some of them simply data containers? you are here 4

10. One popular practice for designing a system is called Test-Driven Development (or Design), abbreviated as TDD. Using this practice, you write tests first and then implement the code that makes the tests pass. Can you think of benefits of this approach? What about disadvantages? (References: http://www.testdrive.ncom, http://www.methodsandtools.com/archive/archive. php?id=20, http://www.artima.com/intv/testdriven.html, and many others.) 11. We talk about coupling and cohesion in this chapter. Do some research and come up with different definitions of coupling and cohesion. Cite your references. Which do you think is the clearest and best? Why? Warning, bad pun: You should be able to find a couple of cohesive definitions. 10 Chapter 5 Academic Supplement

12. Look at the Instrument and InstrumentSpec classes on pages 204 205. Have we done a good job of abstracting common behavior? Will this work for other types of instruments, like flutes, drums, kazoos, maracas, and the ever-popular washboard? Why or why not? How would you make the design better? (Don t look ahead!) 13. Can you describe the relationship between cohesion and coupling? Okay, we dare you to do it and make it understandable to your parents! you are here 4 11

14. Now for a patterns exercise. Look at the final version of Rick s application in this chapter. It s pretty flexible, if we do say so. Now the instrument spec is responsible for determining if it matches another instrument spec. Pretty cool, huh? But is it flexible enough? What might be likely to change, and how would you adapt the software to deal with it? 15. One metric that is used to evaluate the goodness of a design is called the Depth of Inheritance Tree (DIT) metric. If you represent your class hierarchy as a tree (yes, I know there could be a forest, but let s assume for now that there s just one tree), DIT is the length of the maximum path from a node to the root of the tree. In other words, it is a measure of how many ancestor classes any given class has. (See, it works for a forest, too!) What is better, a high value of DIT or a low value, and why? 12 Chapter 5 Academic Supplement

16. What is the DIT value for each class in the diagram for question 1B? 17. Create a model that describes all of the people in your academic institution. Describe the model as a class hierarchy with the root class being Person. Try to keep the maximum value of DIT to a minimum, while keeping the different type of people distinct (you can use helper classes to do this think delegation). Produce a UML class diagram of your model. you are here 4 13

18. Another metric, originally proposed by Chidamber and Kemerer is the Lack of Cohesion of Methods. (Reference: Chidamber, S.R. and Kemerer, C.F., Towards a metrics suite for object oriented design, Proceedings of 6th ACM Conference on Object Oriented Program, Systems, Languages and Applications (OOPSLA), Phoenix, AZ, pp. 197-211) In order to calculate the metric as it was originally proposed: Consider a Class C 1 with methods M 1, M 2,..., M n. Let { I i }= set of instance variables used by method M i. There are n such sets {I 1 },..., {I n }. LCOM = the number of disjoint sets formed by the intersection of the n sets. (Taken from: Etzkorn, Davis, and Li, http://www.cs.uah.edu/tech-reports/tr-uah-cs-1997-02.pdf). Take the final version of Rick s application from this chapter and calculate LCOM for each class. 19. Calculate LCOM for the final version of Doug s dog door application from the previous chapter. 14 Chapter 5 Academic Supplement

20. Java allows single inheritance, while C++ allows multiple inheritance. What are the benefits and disadvantages of each? Justify your answers. 21. Can you use interfaces in Java to approximate multiple inheritance? How? What can t you do with interfaces that you can with multiple inheritance? you are here 4 15

22. C++ does not have an interface in the language. How can you achieve the same results as the interface in Java? 23. We talk about encapsulating what varies in your application. A complementary principle is to abstract commonality. What do you think this means? How does this relate to encapsulating what varies? 16 Chapter 5 Academic Supplement

24. How does applying encapsulate what varies naturally lead to delegation? 25. Repeat the Sharpen your pencil exercise on page 244 for the final version of Doug s application in the previous chapter. you are here 4 17

26. We used a Map class to implement dynamic properties for Rick s application. This was a good choice for Java, but not all languages have such a simple way of handling this. What other choices can you think of? Explain their advantages and disadvantages? 18 Chapter 5 Academic Supplement

If you did question 9 earlier, implement the classes from your model. Add behavior to some of the classes, such as gradeexam for the instructor or staff, and impllement the appropriate methods. Write tests for your classes and try to get 100% coverage. you are here 4 19

An alternative to programming exercise 1: Implement the software using TDD. Write a short paper evaluating the experience and quality of the code. Is this a practice you might consider using? Why or why not? 20 Chapter 5 Academic Supplement

If you answered question 14 earlier, you hopefully realized that there might be more than one way to match instruments. For example, what if someone came in and said, My daughter wants to learn to play a stringed instrument. It doesn t matter if it s a banjo, mandolin, or guitar. I just want it to cost less than $100.00 since she ll probably want to build a model rocket next week! In fact, there are all sorts of searching algorithms you could choose to fit different needs. The Strategy pattern (go get your Head First Design Patterns book now) could help you handle these situations. Modify the code to add a Strategy pattern and implement two different strategies. Make sure you test your code. Note: This achieves some of the same functionality as we ended up with at the end of this chapter. Is this better or worse? Justify your conclusions. you are here 4 21

1 This is a research project. We ll be looking at delegation a lot as a way of designing flexible software. One principle that good designers use is to prefer delegation to inheritance. Research this issue and explain why delegation is preferred (if, in fact you agree with the statement). Give examples of how delegation makes your software more flexible than inheritance. Use UML diagrams in your answer. 22 Chapter 5 Academic Supplement

2 There are several versions of LCOM. Read http://www.cs.uah.edu/tech-reports/tr- UAH-CS-1997-02.pdf and summarize the different ways to calculate LCOM. Which do you think are the most meaningful and why? Calculate LCOM for some application (Rick s?) using each of these methods. you are here 4 23

3 There is a concept called mixin classes. Several (object-oriented) programming languages support mixin classes. Research the topic of mixin classes and write a 3 4 page report on your findings. Describe the concept and the benefits it provides. Provide an example of a mixin class for at least two such languages. Cite your references. 24 Chapter 5 Academic Supplement