02 Design OOP
02 Design OOP
Development- Lecture 2
3
OO Design Process
• Capture narratives of the environment, application
(app)
– Via observations, talking with domain experts, stakeholders,
end users
– Identify specific “circumstances” where an app would be used
– Capture using text, storyboards, sketches
• Identify domain classes, assign responsibilities;
collaborators
• Evaluate using OO Checklist
• Develop the application flow: use cases, screen flows
• Map domain model to Android framework model
• Map app flow to framework model; connect UI to
4
domain model; identify collaborators, update
Identify Objects and
Classes
• Examine nouns, noun phrases as
candidates
• Adjectives: candidate attributes or
subtypes
• Group into categories: potential abstract
classes, superclasses
• Write down the purpose of each class
• Eliminate redundant or non-domain
classes
5
Identify, Assign
Responsibilities
• Start with verbs and verb phrases (i.e.
actions)
• Assign to the appropriate classes
– Distribute evenly: don’t give one class too
much work
– Don’t break the class definition
– Locate responsibility with information
– Locate related information together
6
Map Domain Model to Framework
Patterns
• Usually a variation of MVC
7
Identify Collaborations
• Examine narratives, storyboards,
use cases
• Create scenarios
• Walkthrough scenarios
• Identify interactions between classes
– These are the collaborations
8
Evaluate Using Design
Checklist
• Each class must have:
– Clear name
– Cohesive description of responsibility
– Long-lived state
– Collaborators
9
Record on CRC Cards (1)
14
Consolidate: Verbs
• Take turn, goes, play – retain play
• Mark vs. place vs. …? Use place
symbol
• Remove implement – irrelevant to
game
• Retain display, accumulate, exit and
reset
15
Candidate Classes,
Responsibilities
• Classes: Symbol, Player, Human,
Computer, Board, Row, and Game
(with attribute Score)
• Instances: O, X of the class Symbol
• Responsibilities: play, place,
display, accumulate (scores), quit,
and reset.
16
Allocate Responsibilities to
Classes
• Class Game is allocated the
responsibilities: play,
accumulateScores, quit, and reset.
• Class Board has Display
responsibilities.
• Class GameGrid has Place.
• Symbol, Player, Human, Computer,
and Row have no responsibilities yet.
Keep?
17
Map Domain Model to Framework
Patterns
• Controller classes map to Activities,
e.g. GameSession
• Visual elements (if any, remember
we’re doing domain object design)
map to views
• Pure domain objects map to “plain
old Java object” (POJO) hierarchies
18
General Scenario
• Start a new game.
• Determine who plays first: the human or the computer. Assign
the X symbol to the first player; assign the O symbol to the
second player.
• The first player places his symbol at an empty location on the
board. The second player does likewise. Repeat until one player
has three of his symbols in a row, column, or diagonal, or no
more squares are in play, in which case the game ends in a
draw.
• Accumulate scores for the players. The winning player’s score
increments by 1; the losing player’s score does not change. In a
draw, both players’ scores remain the same.
19
Screens and
Screen
Flows in
Tic-Tac-Toe
20
Scenario Walkthrough – Verification,
Identifying Collaborators
• No class to respond to starting new game.
Create one:
– GameController?
– GameController and Game collaborate
• Symbol creation, placement? Symbol and
Board.
• placeSymbol invokes Play?
• Game needs checkResult?
• Board, GameGrid and Game are collaborators.
• Etc.
21
Final Classes,
•
Responsibilities
Game: Represents a single Tic-Tac-Toe game.
– Responsibilities: play, checkResult
– Collaborators: GameSession, GameView, Grid.
• GameView: Represents the visual display of a Tic-Tac-Toe game.
– Responsibilities: placeSymbol, showScores
– Collaborators: Game
• GameGrid: Represents 3×3 Tic-Tac-Toe grid.
– Responsibilities: placeSymbol, getEmptySquares
– Collaborators: Game
• GameSession: Represents Tic-Tac-Toe play session (multiple
games)
– Responsibilities: playNewGame, quit, decidePlayers, accumulateScores
– Collaborators: Game, GameView
• Symbol – represents a Tic-Tac-Toe symbol (i.e., an X or an O)
– Responsibilities: None
22
– Collaborators: Game
Contracts
• Game:
– play(Grid, Symbol, x, y) returns Success,
Failure
– checkResultAndSetState(Grid) returns nothing
– isActive() returns true or false
– isWon() returns true or false
– isDrawn() returns true or false
• GameView:
– placeSymbol(Symbol, X, Y) returns Success,
Failure
23
– showScores(PlayerOneScore, PlayerTwoScore)
Credits
25