39
Kate Gregory 1 Week 3 • Lab 1 due now • Finding Classes in your System • CRC Cards • Collaboration Diagrams • Lab 2

Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Embed Size (px)

Citation preview

Page 1: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 1

Week 3

• Lab 1 due now

• Finding Classes in your System

• CRC Cards

• Collaboration Diagrams

• Lab 2

Page 2: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Date Week Topic Hand Out Due Back Test

6-Sep-13 1 Administrivia / Overview / Motivation, benefits of OO

13-Sep-13 2 Use Cases Lab 1: Use cases

20-Sep-13 3 CRC Cards, collab graphs Lab 2: CRC cards lab 1 5%  

27-Sep-134 start class diag lab 2 5%

4-Oct-13 5 Finish class diag, Associations Lab 3: Class Diag

11-Oct-13 6 Inh & Polymorphism / midterm review lab 3 5%

18-Oct-13 7 midterm Midterm 25%

25-Oct-13 Reading Break

1-Nov-13 8 Interaction diag / Design Patterns Lab 4: Interaction Diag    

8-Nov-13 9 Good Design / Modules & Packages / Deployment and component diagrams /Metrics / SOLID

Lab 5: Critiques lab 4 5%

15-Nov-13 10 State diagrams / Activity diagrams / Summary and Conclusion / The Future

22-Nov-13 11 Critiques critique lab (before class) 15%

29-Nov-13 12 Critiques

TBD Final Exam Final 40%

Page 3: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Need Meeting Space?

• If you are having trouble finding a quiet place for group work, the library offers study rooms you can book for up to two hours…

Kate Gregory

Page 4: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Imagine Cup

• Student programmer competition sponsored by Microsoft

• Very generous prizes

• Great opportunity to meet industry professionals and experience reality of software development

• http://imaginecup.com/

Kate Gregory 4

Page 5: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 5

What Classes does the system have?• Finding the classes in your system is the first

step in analysis and design• These classes will encapsulate your business

logic and will live in the business layer(s)• Some people think of it as looking at all the

“stuff” and sorting it into classes• Others look at it as making up classes and

then thinking of examples of them

Page 6: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 6

Techniques for finding classes

• Thinking about classifications

• Looking through use cases for nouns

• Considering common patterns

• Class responsibilities collaboration (CRC) analysis

• Collaboration diagram

Page 7: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 7

Grady Booch says…

... Intelligent classification is intellectually hard work, and it best comes about through an incremental and iterative process

... There is no such thing as the perfect class structure, nor the right set of objects. As in any engineering discipline, our design choice is compromisingly shaped by many competing factors.

Page 8: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 8

Classification Theory

• Classification is the process of checking to see if an object belongs to a category or a class

• Most humans find classification irresistible

Page 9: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 9

Objects and Classes

• A class is a specification of structure, behavior, and the description of an object. – An object is an instance of a class

• Classification is more concerned with identifying classes than identifying the individual objects in a system.

• Intelligent classification is intellectually hard work and may seem rather arbitrary.

Page 10: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 10

Use-case Driven Approach

• Describe the system

• Look at the words in the description or steps of each scenario

• Classes and objects needed for the scenario will present themselves to you

Page 11: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 11

Use case example - Banking

Customer Withdraws Money

A. Client provides Teller amount of withdrawal and account

B. Teller checks balance in account and status of account

C. Teller reduces balance of account by withdrawal amount

D. Teller gives money to customer

Page 12: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 12

Banking use case cont.

At B, if balance is lower than amount to be withdrawn, teller refuses transaction. Customer may return to A with a lower amount, or use case may end

At B, if account is frozen, teller refuses transaction. Use case ends.

At D, if customer leaves without money, teller reverses transaction and returns money to drawer

Page 13: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 13

Bank Nouns

Client

Teller

Amount

Account

Balance

Status

Withdrawal

Money

Transaction

Customer

Hold

Drawer

Page 14: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Implied Data• Look for verbs in the use case that imply storage

and retrieval of data– The clerk looks up the reservation– The receptionist records the appointment on the

calendar

• The nouns in these sentences are almost certainly classes

• This is the information that passes between use cases

Kate Gregory 14

Page 15: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 15

Syntactic Lexical Analysis• Read use cases, interviews, and requirements,

looking for nouns and noun phrases– Mark verbs too; you’ll need them later

• Make a list– Change all plurals to singular – Drop irrelevant nouns– Combine synonyms– Establish if a noun is a class or an attribute

Page 16: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 16

Drop Irrelevant Classes

• Scrap candidate classes that have nothing to do with the system under study– Out of scope– Represent an actor

• But be aware of info about an actor

– Nouns not related to the system

Page 17: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 17

Redundant Classes• Do not keep two classes that express the

same information.

• If more than one word is being used to describe the same idea, select the one that is the most meaningful in the context of the system.– Customer / Client / Member / Purchaser– Reservation / Booking / Prearranged Stay

• Remember to use the language of the user

Page 18: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 18

Adjective Classes• Adult Membership, Youth Membership

• Checking Account, Savings Account

• Does the adjective change the behaviour of the object represented by the noun?– If the use of the adjective signals that the behaviour of the object

is different, then make a new class.

– If not, keep a single class and add the type as an attribute

• It is difficult for an object to change its class (you make a whole new object and copy values) but it’s easy to change an attribute– Can you convert a savings account to a checking account? If not,

it’s more likely they are different classes

Page 19: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 19

Attribute or Class?

• Candidate classes which are used only as values should be defined or restated as attributes and not as a class.

• Example: age, salary, description.

• If it doesn't do anything, just holds information, it's usually not a class

Page 20: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

The Power of Words

Page 21: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 21

Statement of Purpose• Each class must have a purpose, must do

something, not just hold information • You must be able to formulate a statement of

purpose for each candidate class– The Account class holds and manages information

about a customer's money

– The EmployeeInformation class holds salary and contact information and performs payroll calculations

• No statement of purpose? Not a class

Page 22: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 22

Common Class Patterns

• Tangible Things and Devices

• Concepts

• Events

• People

• Places

• Organization

• Communicating with Actors

• Information about Actors

Page 23: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 23

CRC Cards

• CRC stands for Class, Responsibilities and Collaborators developed by Cunningham, Wilkerson and Beck.

• CRC analysis helps you to – identify classes you need– Work out which responsibilities each class will

have– Test your analysis

Page 24: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 24

CRC Process

Iterate

Identify Classes responsibility

Assign responsibility

Identify Collaboration

Page 25: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 25

Collaborations• An object can accomplish either a certain

responsibility itself, or it may require the assistance of other objects. – Does it know all it needs to know to do it?

• If it requires the assistance of other objects, it must collaborate with those objects to fulfill its responsibility.

Page 26: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 26

Collaborations

• Invoice asks customer to print itself

• Invoice asks invoice items to print themselves

• Schedule asks task for its effort

• Schedule sets task’s start date

Page 27: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 27

CRC Cards• CRC cards are 4" x 6" index cards. All the

information for an object is written on a card.

ClassName

Collaborators...

...Responsibilities

Page 28: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 28

Manages balance

Processes withdrawal

Processes deposit

Transaction log

Transaction log

Savings Account

Adds Transaction

Deletes Transaction

Knows list of Transactions

Transaction Log

One way

Page 29: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 29

Calculates total

Prints paper invoice

Updates inventory

Customer, Line Item

Inventory, Line Item

InvoiceLine Item

Page 30: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 30

How do you create them?

• Start with one or two obvious cards. • If the situation calls for a responsibility not already covered

by one of the classes: – Add a responsibility to a class, or– Create a new class to address that responsibility.

• Information held is a responsibility: “knows” or “manages”• Don’t create an exhaustive list of attributes

– Just say “manages contact info” for now

• Keep going until you can cover all the behaviour you listed in the use cases

Page 31: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 31

Collaboration diagram

• One box for each class, all on one diagram

• One line between boxes for each collaboration

• Arrow shows direction

Savings account

Transaction log

Page 32: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 32

Two kinds of collaboration diagram

Page 33: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 33

Overview collaboration diagram

Page 34: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Testing your CRC Cards

• Go through the use cases• Map sentences in the use cases to responsibilities

on your cards– “Clerk records start and end date”

• Yes, we have a class responsible for keeping that

– “Clerk calculates invoice total”• Yes, we have a class responsible for calculating that

– etc

Kate Gregory 35

Page 35: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

36

Naming Classes

• The class should describe a single object, so it should be the singular form of the noun.– If you think you need Employee and Employees, try

Employee and ListOfEmployees for now

• Use names that the users are comfortable with. • The name of a class should reflect its intrinsic

nature.– Not a CS concept like hash table, heap, stack– Queue and list are ok in their informal meanings

Page 36: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 37

Summary• Finding classes is not easy. You get better

with practice.• There is no such thing as one “right set of

classes.”• Finding classes is an incremental and

iterative process.• Filling out the contents of proposed

classes helps you to find more, orto gain confidence in the ones you have.

Page 37: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 38

Summary

• Unless you are starting with a lot of domain knowledge, you are probably missing more classes than you will eliminate.

• Naming a class is important.

• The class should describe a single object, so it should be a singular noun or an adjective and a noun.

Page 38: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 39

Lab 2

• You are given a list of nouns• Reject those that are not classes• Add more if you feel some are missing• Write a statement of purpose for all you keep• Make a CRC card for all you keep• Make an overview collaboration diagram that

summarizes all the collaborations at once

Page 39: Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2

Kate Gregory 40

Next Time

• Read up to where we are

• Lab 2 will be due at the start of class– I will try to have Lab 1 marked for you

• If you want to read ahead, next lecture we start class diagrams