300 likes | 563 Views
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.
E N D
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
Responsibilities • responsibilities • responsibilities
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.
CRC • CRC is a design method. • Why CRC? • Intuitive. • Easy to teach.
CRC Process • Identify classes. • Identify responsibilities. • Assign responsibilities to classes. • Identify collaborations. • Collect responsibilities into contracts. • Define protocols for contracts.
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
Collaboration • A collaboration occurs when one class requires the services of another class in order to fulfill its responsibilities. • Indicates classes are coupled.
Contracts • A contract is a set of related responsibilities. • A contract specifies a service provided by one class to other classes
Protocols • A protocol contains • method name and type • method parameters and types • preconditions • postconditions • A protocol is a formalization of a contract
Finding Classes • A class is a collection of objects. • Objects = “things” • Look for nouns in documents such as the requirements (SRS)
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).
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
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
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.
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
Example: Classes Likely Machine Touch Screen Voice Activated Mouse Keyboard Ballot State County City Voter Vote Questionable keycode selection
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
Example: Collaborations Machine and Vote are clients of Ballot Machine is a client of Vote
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.
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?
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.
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.
Assigning Responsibilities-3 Heuristic: Keep information about one thing in one place Do not share responsibility for maintaining specific information!! Minimize duplication
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).
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?
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