Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams.

Slides:



Advertisements
Similar presentations
ACM/JETT Workshop - August 4-5, :Design of Classes using CRC cards.
Advertisements

1 © Wolfgang Pelz CRC Cards CRC Cards (class-responsibility-collaborator)
Chapter 8: Designing Classes
CRC Cards (class-responsibility-collaborator)
IS514 Lecture Week 9 CRC Cards.
Georgia Institute of Technology Object-Oriented Analysis Barb Ericson June 2006.
Object-Oriented Application Development Using VB.NET 1 Chapter 5 Object-Oriented Analysis and Design.
6-1 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer problem-solving process and relate it to Polya’s.
Problem Solving and Algorithm Design
Chapter 6 Problem Solving and Algorithm Design. 6-2 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer.
Chapter 22 Object-Oriented Systems Analysis and Design and UML Systems Analysis and Design Kendall and Kendall Fifth Edition.
Chapter Goals To learn how to choose appropriate classes to implement
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Introduction To System Analysis and Design
Objects First With Java A Practical Introduction Using BlueJ Designing object-oriented programs How to write code in a way that is easily understandable,
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Solving the Problem Analysis & Design.
Lecture 4 Class Responsibility Collaboration Cards
Object-Orientated Design Unit 3: Objects and Classes Jin Sa.
Domain Modeling (with Objects). Motivation Programming classes teach – What an object is – How to create objects What is missing – Finding/determining.
Object-Oriented Design. From Analysis to Design Analysis Artifacts –Essential use cases What are the problem domain processes? –Conceptual Model What.
CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies.
OO Analysis and Design CMPS OOA/OOD Cursory explanation of OOP emphasizes ▫ Syntax  classes, inheritance, message passing, virtual, static Most.
Systems Analysis and Design in a Changing World, Tuesday, Feb 27
Big Java Chapter 12. Software Process - Waterfall Analysis Design Implementation Testing Deployment Does not work well when rigidly applied! established.
1 Chapter 2 (Cont.) The BA’s Perspective on Object Orientation.
High-Level Design With Sequence Diagrams COMP314 (based on original slides by Mark Hall)
1 On to Object Design Chapter 14 Applying UML and Patterns.
Introduction To System Analysis and Design
11 Partnership for Performance How to hear this lecture Click on the icon: to hear the narration for each slide.
Copyright 2002 Prentice-Hall, Inc. Chapter 2 Object-Oriented Analysis and Design Modern Systems Analysis and Design Third Edition Jeffrey A. Hoffer Joey.
CSC 395 – Software Engineering Lecture 13: Object-Oriented Analysis –or– Let the Pain Begin (At Least I’m Honest!)
Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies – Extreme Programming Object-Oriented Design – CRC Cards - UML.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 12 – Object-Oriented Design.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 4: Restaurant.
CS Overview of CRC CS 4311 B. Beck and W. Cunningham, A Laboratory for Teaching Object-Oriented Thinking, OOPSLA ’89, October 1-6, R. Wirfs-Brock,
CSC172 Class Design Pepper. Goals How to design classes How to think in terms of objects StarUML Code Generation.
1 Object-Oriented Modeling Using UML CS 3331 Section 2.4 Modeling Requirements with Use Cases.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
1 Examining Execution Sequences Introducing Sequence Diagrams.
CSC480 Class Design Pepper. Goals How to design classes StarUML Code Generation.
1 Chapter 5 Modeling System Requirements Finding the Use Cases Page
Design Model Lecture p6 T120B pavasario sem.
ITEC324 Principle of CS III Chapter 2 (Horstmann’s Book) – Part 1 The Object-Oriented Design Process Hwajung Lee.
12 OBJECT-ORIENTED DESIGN CHAPTER
UML Review of Use case diagrams. 2 Unified Modeling Language The Unified Modeling Language™ (UML) was developed jointly by Grady Booch, Ivar Jacobson,
CSC 480 Software Engineering PSP Project 2 August 27, 2004.
OO DomainModeling With UML Class Diagrams and CRC Cards Chapter 6 Princess Nourah bint Abdulrahman University College of Computer and Information Sciences.
1 BTS330 Visual Modeling. What is Visual Modeling? 2 Copyright © 1997 by Rational Software Corporation Computer System Business Process Order Item Ship.
Chapter 6: The Analysis Workflow Chapter 7: Classes and Objects Chapter 8: Finding Analysis Classes [Arlow and Neustadt, 2005] CS 426 Senior Projects in.
Chapter 2 (Horstmann’s Book) – Part 1 The Object-Oriented Design Process Hwajung Lee.
Star UML and CRC Cards Pepper.
CRC Cards: Overview Emerson Murphy-Hill Creative Commons Attribution 4.0 License. Material Produced by NCSU Software Engineering Faculty.
5 Systems Analysis and Design in a Changing World, Fourth Edition.
An informal, team oriented, OO design system
Chapter 12 – Object-Oriented Design
CSSE 220 Day 17 Introduction to your Vector Graphics project
The Movement To Objects
Object-Oriented Analysis and Design
Unified Modeling Language
OO Domain Modeling With UML Class Diagrams and CRC Cards
Object-Oriented Analysis
Advanced Java Programming
Object-Oriented Analysis
Slides by Steve Armstrong LeTourneau University Longview, TX
CRC Card Design A CRC Model is a collection of cards (usually standard index cards or larger) that are divided into three sections. 1. Class 2. Responsibility.
Copyright 2007 Oxford Consulting, Ltd
Chapter 12 – Object-Oriented Design
Chapter 22 Object-Oriented Systems Analysis and Design and UML
Presentation transcript:

Chapter 8 Designing Classes Designing Classes –8.1 Choosing Classes –8.2 Cohesion and Coupling –CRC Cards –UML Diagrams

Assignment Chapter 8 Review Exercises (Written) Review Exercises (Written) –R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Thursday, January 10, 2013 Due Thursday, January 10, 2013 CRC Cards – in class assignment to be completed by Friday, Jan 11, 2013 CRC Cards – in class assignment to be completed by Friday, Jan 11, 2013

Choosing classes A class should represent a single concept from the problem domain. A class should represent a single concept from the problem domain. –Point –ComplexNumber –Rational –Purse –BankAccount

Classes have responsibilities. Point Point What are some responsibilities for the Point class?

Classes have responsibilities. Point Point –getX –getY –setX –setY –findDistanceFromOrigin –findDistanceFromPoint

Classes have responsibilities. ComplexNumber ComplexNumber What are some responsibilities for the ComplexNumber class?

Classes have responsibilities. ComplexNumber ComplexNumber –getReal –getImaginary –add –subtract –multiply

Classes have responsibilities. Rational Rational What are some responsibilities for the Rational class?

Classes have responsibilities. Rational Rational –getNumerator –getDenominator –add –subtract –multiply –divide –reduce

Classes have responsibilities. Purse Purse What are some responsibilities for the Purse class?

Classes have responsibilities. Purse Purse –getTotal –getCents –getDollars –addDimes –addNickels –addQuarters –…

Classes have responsibilities. BankAccount BankAccount What are some responsibilities for the BankAccount class?

Classes have responsibilities. BankAccount BankAccount –getBalance –deposit –withdraw

Choosing Classes A class represents a single concept from the problem domain A class represents a single concept from the problem domain Name for a class should be a noun that describes concept Name for a class should be a noun that describes concept Concepts from mathematics: Concepts from mathematics:PointRectangleEllipse Concepts from real life Concepts from real lifeBankAccountCashRegister

Choosing Classes - Types Actors (end in -er, -or)–objects do some kinds of work for you Actors (end in -er, -or)–objects do some kinds of work for you –Scanner –Random // better name: RandomNumberGenerator Utility classes–no objects, only static methods and constants Utility classes–no objects, only static methods and constants –Math Program starters: only have a main method Program starters: only have a main method Don't turn actions into classes: Don't turn actions into classes: –Paycheck is better name than ComputePaycheck

The rule of thumb for finding classes is to look for nouns in the problem description The rule of thumb for finding classes is to look for nouns in the problem description ChessBoard is a good name for a class but ChessBoard is a good name for a class but NextMove is not. NextMove is not. Classes

Special Types of Classes Actor Classes Actor Classes –Do work for you. Random generates random numbers Random generates random numbers Utility Classes Utility Classes –Class has no objects but contains a collection of related methods and constants. Math has methods abs, sqrt, etc. Math has methods abs, sqrt, etc. Math has constant PI. Math has constant PI. Classes with one method main whose purpose is to start a program for you. Classes with one method main whose purpose is to start a program for you.

Buzz words (Kolling) … Coupling Coupling –interconnectedness of classes We want cooperating classes We want cooperating classes We want low degree of coupling We want low degree of coupling –If we make a change in one class, we do not want to have to change many classes. Cohesion Cohesion –One unit of code should be responsible for one task. method should accomplish one well-defined job method should accomplish one well-defined job a class should represent one type of entity a class should represent one type of entity Code duplication is usually a sign of bad cohesion. Code duplication is usually a sign of bad cohesion.

A good class design… Think ahead Think ahead –What might change? Encapsulate all information about user interface in a clearly defined set of classes. Encapsulate all information about user interface in a clearly defined set of classes. One class should encapsulate one entity. One class should encapsulate one entity. One method should do one task One method should do one task

CRC Cards Class-Responsibilities-Collaborators Class-Responsibilities-Collaborators A CRC card is an index card created in informal brainstorming sessions where the behaviors of the application to be modeled are simulated and recorded on the index cards. A CRC card is an index card created in informal brainstorming sessions where the behaviors of the application to be modeled are simulated and recorded on the index cards.

History Introduced in 1989 by Kent Beck and Ward Cunningham for teaching OOP. Introduced in 1989 by Kent Beck and Ward Cunningham for teaching OOP. CRC Cards are portable. CRC Cards are portable. Promotes good software design. Promotes good software design. Promotes team work. Promotes team work.

The idea (Adapted from Ward Cunningham Computer Research Laboratory Tektronix, Inc.) ClassResponsibilitiesCollaborators Teacher Teach Lessons Evaluates Students Student Student Learns Lessons Teacher Principal Disciplines Students Hires Staff TeacherStudent

A Picture Class name Class Responsibilities Collaborators

An Example Purse Class Responsibilities Collaborators addCoinremoveCoingetTotal

A UML Diagram UML (Unified Modeling Language) notation for classes and objects describes the dependency relationships among classes. UML (Unified Modeling Language) notation for classes and objects describes the dependency relationships among classes. A "picture." A "picture." We will look at class UML Diagrams first. We will look at class UML Diagrams first. –An object UML diagram underlines the class name, a class UML diagram does not underline the class name.

A UML Diagram Purse Coin The Purse class depends on the Coin class. The Coin class does NOT depend on the Purse class. dotted arrow with open end pointing to dependent class.

Coupling If many dependencies exist among classes, we say that coupling is high. If many dependencies exist among classes, we say that coupling is high. If few dependencies exist among classes, we say that coupling is low. If few dependencies exist among classes, we say that coupling is low. It is a good practice to minimize the coupling between classes. WHY? It is a good practice to minimize the coupling between classes. WHY?

Low Coupling

High Coupling

C-R-C A Class represents a collection of similar objects. A class can be any concept important to the system. Look for nouns in the problem description. The Class name appears across the top of the CRC card. A Class represents a collection of similar objects. A class can be any concept important to the system. Look for nouns in the problem description. The Class name appears across the top of the CRC card.

C-R-C A Responsibility is anything that the class knows or does. Responsibilities are things that the class has knowledge about itself, or things the class can do with knowledge it has. Look for verbs in the problem description A Responsibility is anything that the class knows or does. Responsibilities are things that the class has knowledge about itself, or things the class can do with knowledge it has. Look for verbs in the problem description The Responsibilities of a class appear along the left side of the CRC card. The Responsibilities of a class appear along the left side of the CRC card.

C-R-C A Collaborator is another class that is used to get information for this class or to complete actions for this class. A Collaborator is another class that is used to get information for this class or to complete actions for this class. The Collaborators of a class appear along the right side of the CRC card. The Collaborators of a class appear along the right side of the CRC card.

Procedure Organize in a group of 2-3 people. Organize in a group of 2-3 people. Group Responsibilities: Group Responsibilities: –Brainstorm to find classes in problem domain. (Look for nouns.) –Filter the list of classes and assign cards to those classes remaining. –Assign responsibilities to each class (on its index card). –Attributes CAN be assigned, but should not be emphasized.

Procedure Group Responsibilities (continued): Group Responsibilities (continued): –Often, as you are brainstorming, the responsibilities demonstrate the need for additional classes. –List the collaborators of each class. What other classes does this class interact with? What classes does the object need to fulfill its responsibilities?

Group Responsibilities: Place cards on table so that the layout is a visual representation of the solution. Classes that collaborate should be near to each other. Place cards on table so that the layout is a visual representation of the solution. Classes that collaborate should be near to each other.

Problem Definition This program will simulate the TV Lottery drawing for the Pick-6 New Jersey Lottery game. In this game, balls are placed in a container that keeps them popping around in some random movements. This program will simulate the TV Lottery drawing for the Pick-6 New Jersey Lottery game. In this game, balls are placed in a container that keeps them popping around in some random movements. The "Lottery Picker" picks a ball from the container (6 times). The "Lottery Picker" picks a ball from the container (6 times). The chosen balls are displayed. The chosen balls are displayed. Create C-R-C Cards and a UML diagram for the problem. Create C-R-C Cards and a UML diagram for the problem.

Candidate classes Ball Ball Game Game Picker Picker Container Container Ball Container Picker Game

Lottery Ball Class Responsibilities Collaborators getValue

Lottery Container holdBallsBall removeBall

Lottery Picker Class Responsibilities Collaborators chooseBall Ball (Random Generator) (Random Generator)

Lottery Game Class Responsibilities Collaborators play Picker Container Container

Problem Definition Coins are placed in a vending machine and a product is selected by pushing a button. If the inserted coins are sufficient to cover the purchase price of the product, the product is dispensed and the change is given. Otherwise the inserted coins are returned. Coins are placed in a vending machine and a product is selected by pushing a button. If the inserted coins are sufficient to cover the purchase price of the product, the product is dispensed and the change is given. Otherwise the inserted coins are returned. Create C-R-C Cards and a UML diagram for the problem. Create C-R-C Cards and a UML diagram for the problem.

Candidate Classes VendingMachine VendingMachine Product Product Coin Coin

Vending Machine Problem Vending Machine Class Responsibilities Collaborators HoldProductsProduct HoldProductsProduct TakeCoinsCoin TakeCoinsCoin GiveChange GiveChange

Vending Machine Problem Product Class Responsibilities Collaborators getPrice getPrice getName getName

Vending Machine Problem Coin Class Responsibilities Collaborators getValue getValue getName getName

Candidate Classes VendingMachine VendingMachine Product Product Coin Coin

Hmmmmm… Vending Machine Vending Machine –holds products how many? how many? are there any left? are there any left? –makes change what if machine can't make exact change? what if machine can't make exact change? how does it figure out what coins to give back? how does it figure out what coins to give back?

Maybe We should think of additional classes for this design! We should think of additional classes for this design! Possibilities? Possibilities?

Problem Definition Customers order products from a store. Invoices are generated to list the items and quantities ordered, payments received, and amounts still due. Products are shipped to the shipping address of the customer and bills are sent to the customer's billing address. Customers order products from a store. Invoices are generated to list the items and quantities ordered, payments received, and amounts still due. Products are shipped to the shipping address of the customer and bills are sent to the customer's billing address. Create C-R-C Cards and a UML diagram for the problem. Create C-R-C Cards and a UML diagram for the problem.

Invoice Problem Invoice Customer Product Address

Assignment Chapter 8 Review Exercises (Written) Review Exercises (Written) –R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Thursday, January 10, 2013 Due Thursday, January 10, 2013 CRC Cards Due Friday, January 11, 2013 CRC Cards Due Friday, January 11, 2013