Object-Oriented Design
Identifying Objects
- Identify the nouns (objects) in the problem description.
- Identify more objects in your developing problem solution.
- From these, determine what new types (classes) you need.
Example: BlackJack Program
- cards
- a deck
- a dealer
- player(s)
- Do different cards represent different types? Or are suit and rank attributes of a single card type?
- Are the dealer and player instances of the same class? I.e., objects of the same type?
- Should there be a separate Hand class?
Identifying Operations
- Identify what can be done to objects. or
- Identify what objects can do for themselves. or
- Identify who is responsible for doing what.
- Terminology: operations/responsibilities/contracts/message passing
Example: BlackJack Program
- Card: display, report value (report suit? maybe internally) (modify value/suit? or set these in constructor only?)
- Deck: shuffle, deal next card, report how many cards left in deck
- Dealer: deal cards, add card to hand, show (sometimes partial) hand, calculate (partial) hand value, number of cards in hand, hit?, determine winner, reset hand
- Player: add card to hand, show hand, calculate hand value, number of cards in hand, hit?, reset hand
Identifying Relationships
- Identify object relationships, e.g. what objects need to know about what other objects, what objects are part of other objects.
- Terminology: encapsulation; aggregation/has-a relationships; knows-about relationships
(avoid is-a relationships: inheritance!)
Example: BlackJack Program
- Card:
- Deck: has cards
- Dealer: has deck, has cards (or hand, which has cards), knows about player(s)
- Player: has cards (or has hand, which has cards)
Scenarios
- Develop scenarios of how the objects interact to see if you have identified the right objects, the right operations, and the right relationships.
- Develop common scenarios (this is how the thing should usually work).
- Remember to also develop uncommon scenarios, "weird cases," boundary conditions.
- Design is incremental and iterative! (Grady Booch)
Implementation
- Design the public interface (public member function declarations).
- Design the internal data representation (private data members). Include information about relationships.
- Implement the public member functions. Identify useful internal helper functions (private member functions).
- Test individual objects and member functions before putting everything together.
Alyce Brady, Kalamazoo College