1 / 30

CRC: Classes, Responsibilities, and Collaborations

CRC: Classes, Responsibilities, and Collaborations. CS 4311. 12/09. OO Design. Identify or define collections of data and operations on the data Define classes Determine how classes interact. Objects Should. Characteristics of a Good Class. Responsibilities.

ozzy
Télécharger la présentation

CRC: Classes, Responsibilities, and Collaborations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CRC: Classes, Responsibilities, and Collaborations CS 4311 12/09

  2. OO Design • Identify or define collections of data and operations on the data • Define classes • Determine how classes interact

  3. Objects Should

  4. Characteristics of a Good Class

  5. Responsibilities • responsibilities • responsibilities

  6. Aside: Objects are not just “data and methods” At this point in design, you must consider objects at the conceptual level. An object is something that has responsibilities.

  7. CRC • CRC is a design method. • Why CRC? • Intuitive. • Easy to teach.

  8. CRC Process • Identify classes. • Identify responsibilities. • Assign responsibilities to classes. • Identify collaborations. • Collect responsibilities into contracts. • Define protocols for contracts.

  9. CRC Card • One class per card • Write subclasses on lines below the name • List attributes and purpose on back of card • Assign responsibilities Concrete Class: Numeric Keypad Superclass: Keypad Subclasses: None Responsibilities Collaborations

  10. Collaboration • A collaboration occurs when one class requires the services of another class in order to fulfill its responsibilities. • Indicates classes are coupled.

  11. Contracts • A contract is a set of related responsibilities. • A contract specifies a service provided by one class to other classes

  12. Protocols • A protocol contains • method name and type • method parameters and types • preconditions • postconditions • A protocol is a formalization of a contract

  13. Finding Classes • A class is a collection of objects. • Objects = “things” • Look for nouns in documents such as the requirements (SRS)

  14. Finding Classes: Advice Be careful of nouns outside the system. Does an object need to act on behalf of the user? Model known interfaces to world (user interfaces, interfaces to programs).

  15. Identifying Responsibilities • Responsibilities are doing and knowing • This suggests verbs in English • Look for verbs in documents • Actions that must be performed • Modes of maintaining information

  16. Assigning Responsibilities Assign every responsibility to some class Evenly distribute the intelligence and work-load State responsibilities as generally as possible Keep behavior with related information Collect information about 1 thing in 1 place

  17. Example There is a voting system with several types of voting machines. These include touch screen machines, voice activated machines (for the sight impaired), and a mouse/keyboard machine (prototyping an interface for web-based voting). There are several different ballots for the machines, one for the city, one for the county, and one for the state. A voter approaches the machine, enters the keycode given to her or him by the election clerk, and the system presents the ballots, one at a time. The voter makes selections, and then casts each of the ballots. Once cast, a vote cannot be modified.

  18. Example: Nouns system Voting machine Machine Touch Screen Machine Voice Activated Machine Mouse/Keyboard Machine Interface Ballot One Keycode City County State Her/Him Election Clerk Time Voter Selection Each Vote

  19. Example: Classes Likely Machine Touch Screen Voice Activated Mouse Keyboard Ballot State County City Voter Vote Questionable keycode selection

  20. Example: Responsibilities Machine Knows which ballots are in the election Can present ballots to a voter Can validate a keycode Knows what selections a voter has made Can cast a ballot Vote Knows what ballot is being voted on Knows a voters selection for items on the ballot Ballot Knows the type of item (issue or candidate) Knows the seats and candidates Knows the issues and the choices

  21. Example: Collaborations Machine and Vote are clients of Ballot Machine is a client of Vote

  22. Problem #1 Missing Classes If you can’t easily assign a responsibility, you may be missing a class. Try to reduce complexity Example: grouping elements in a drawing. The element need not know it’s part of a group. The drawing need not know about groups. But a group class can handle the responsibilities. The group class knows about its members and propagates actions to them.

  23. Problem #2 Arbitrary Assignment If you don’t know what class to assign a responsibility to, consider What are the possibilities (what classes can you assign to)? What are the implications? Which is most obvious? Which is most efficient?

  24. Two Approaches on Distributing Intelligence

  25. Assigning Responsibilities-1 • Heuristic: Evenly distribute intelligence • Intelligence is what the system knows, actions that it can perform, and impact it has on other systems and users with which it interacts • A class gets intelligence based on how much it knows or can do, and how many other objects it can affect.

  26. Assigning Responsibilities-2 Heuristic: State responsibilities as generally as possible. Can find common responsibilities that can be shared Heuristic: Keep behavior with related information. If information changes, no update messages must be sent between objects The object that needs to know that something has changed will know it.

  27. Assigning Responsibilities-3 Heuristic: Keep information about one thing in one place Do not share responsibility for maintaining specific information!! Minimize duplication

  28. Advice Try to express responsibilities in general terms. You are still exploring. Try to keep all of a class’s responsibilities at the same conceptual level (same level of abstraction).

  29. Walk-through What is happening when the machine is idle? What happens when a card is inserted? What happens when the user chooses “withdrawal” from main menu? What happens when the user presses the cancel key?

  30. Finding abstract classes Re-examine list of candidate classes and group related classes Identify those that share attributes Identify those that share behavior Goal is to identify as many abstract classes as possible

More Related