

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Case Study Design flexibility possible solution metaphor place and thing objects unhelpful bits entity class entity API cammands method actions actions as entities watershed carrying things pro java code con java code pro DIY bytecode oo code two word commands multi word commands public and private links tests simple loops creation
Typology: Slides
1 / 41
This page cannot be seen from the preview
Don't miss anything!


































Ian Holyer
Ian Holyer
●
Design
Design is too varied and personal to be able to develop principles
is too varied and personal to be able to develop principles
very easily
very easily
●
We will look at some OO Design issues and Design Patterns later
We will look at some OO Design issues and Design Patterns later
on, which help with design, but only
on, which help with design, but only after
after you already know what
you already know what
it is
it is
●
For now, to explore what design really
For now, to explore what design really is
is , we will just look at one
, we will just look at one
case study
case study
●
This is the Stage project, where we have already done the analysis
This is the Stage project, where we have already done the analysis
●
The things to remember during the case study are (a) we are trying
The things to remember during the case study are (a) we are trying
to design an engine, not a program and (b) we are trying to follow
to design an engine, not a program and (b) we are trying to follow
the KISS principle
the KISS principle
●
Before OO, the standard way to start a design was with
Before OO, the standard way to start a design was with
functional decomposition, i.e. knowing what the program
functional decomposition, i.e. knowing what the program
does overall, break the work into major pieces
does overall, break the work into major pieces
●
This suits a non-OO program that ends up as procedures
This suits a non-OO program that ends up as procedures
calling procedures, probably monolithic
calling procedures, probably monolithic
●
With OO, one way to start is by trying to identify nouns
With OO, one way to start is by trying to identify nouns
that might make good classes
that might make good classes
●
In this case, without making any decisions, we have
In this case, without making any decisions, we have
Player, Command, Word, Noun, Verb, Place, Direction,
Player, Command, Word, Noun, Verb, Place, Direction,
Thing, Description, Puzzle, World, Game
Thing, Description, Puzzle, World, Game
●
Let's start in the middle with Place and Thing
Let's start in the middle with Place and Thing
●
Perhaps the most obvious approach is for a Place to
Perhaps the most obvious approach is for a Place to
store places you can go, and Things at that place
store places you can go, and Things at that place
class Place
{
Place north, east, south, west;
List
}
class Place
{
Place north, east, south, west;
List
}
class Thing
{ ...
}
class Thing
{ ...
}
●
This looks natural, but has two serious problems for us
This looks natural, but has two serious problems for us
●
The second problem concerns creators of new games
The second problem concerns creators of new games
●
We don't want to restrict a creator's imagination, and
We don't want to restrict a creator's imagination, and
adventure games are about fantasy where anything goes
adventure games are about fantasy where anything goes
●
Before
Before
looking at the next slide, try to think of:
looking at the next slide, try to think of:
●
other directions
●
things inside things
●
places inside places
●
a thing which can be in two places at once
●
something which is a thing and a place at the same time
●
something which is not a thing nor a place
●
something you haven't got the imagination to predict
●
Here are some possible answers:
Here are some possible answers:
●
other directions: northeast, forward, down
●
things inside things: contents of box, bag, egg
●
places inside places: cupboard/room/building, car/road
●
things in two places: river, door, hole in wall, teleport
●
a thing and a place: tent, wormhole
●
not thing nor place: monsters, other players
●
something you haven't got the imagination to predict: ...
●
It is often useful to use metaphors, which is like finding
It is often useful to use metaphors, which is like finding
extra nouns to play with to help design classes
extra nouns to play with to help design classes
●
A good metaphor in this case is Java itself, particularly
A good metaphor in this case is Java itself, particularly
Object and Field, though those won't make good class
Object and Field, though those won't make good class
names, because they will clash with Java
names, because they will clash with Java
●
A Place is like an Object (actually, it already
A Place is like an Object (actually, it already is
is )
●
Directions are like Fields (actually, they already are)
Directions are like Fields (actually, they already are)
●
The Things that are currently in a Place are a bit like
The Things that are currently in a Place are a bit like
Fields (but they change dynamically)
Fields (but they change dynamically)
●
A Container is like an Object with Fields which refer to
A Container is like an Object with Fields which refer to
other Things as contents, and so on
other Things as contents, and so on
●
Our example looks like this, as a UML object diagram,
Our example looks like this, as a UML object diagram,
showing fields linking objects
showing fields linking objects
beach
tree
sand
spade
spade
south north
sand
here
●
The field names are the nouns used in the game
The field names are the nouns used in the game
tree.spade = spade
tree.south = beach
beach.sand = sand
beach.north = tree
●
Let's have a class called Entity
Let's have a class called Entity
●
Let's decide that it represents a place, or a thing, or a
Let's decide that it represents a place, or a thing, or a
container, or a vehicle, or a monster, or ...
container, or a vehicle, or a monster, or ...
●
Let's decide that an Entity has a set or list of named
Let's decide that an Entity has a set or list of named
links to other entities, which represent directions, or
links to other entities, which represent directions, or
contents, or any other one-way relationships
contents, or any other one-way relationships
●
Links can come and go during play
Links can come and go during play
●
The link names are the nouns that the player uses, and
The link names are the nouns that the player uses, and
we don't want to hard-wire nouns into the
we don't want to hard-wire nouns into the Stage
Stage engine,
engine,
so north/south etc. are not going to be special
so north/south etc. are not going to be special
●
Question: does an Entity have a name?
Question: does an Entity have a name?
●
An Entity can be known by different names when
An Entity can be known by different names when
viewed from different perspectives
viewed from different perspectives
city river field
east west
●
The river is known as "east" when you are in the city,
The river is known as "east" when you are in the city,
and as "west" when you are in the field
and as "west" when you are in the field
●
The player often doesn't know exactly what the entity is
The player often doesn't know exactly what the entity is
that is being referred to:
that is being referred to:
cave dynamite
candle
●
Only world creators seem to need global names for
Only world creators seem to need global names for
entities, and even they can be links
entities, and even they can be links
●
We want to get back to our overall design wanderings
We want to get back to our overall design wanderings
and think about commands, "go south" or "take spade"
and think about commands, "go south" or "take spade"
●
We have decided that we don't want nouns like "south"
We have decided that we don't want nouns like "south"
or "spade" to be special, but invented by the creator
or "spade" to be special, but invented by the creator
●
We can take the same attitude to verbs, "go" and "take"
We can take the same attitude to verbs, "go" and "take"
●
But how do we know if an entity is a place or a thing,
But how do we know if an entity is a place or a thing,
how do we know "go spade" and "take south" won't
how do we know "go spade" and "take south" won't
work, and how do we get "go" and "take" to work?
work, and how do we get "go" and "take" to work?
●
There must be a way forward from here, mustn't there?
There must be a way forward from here, mustn't there?
●
We can go back to the Object, Field metaphor, but as
We can go back to the Object, Field metaphor, but as
before we want more flexibility
before we want more flexibility
●
We can make use of our new understanding of OO,
We can make use of our new understanding of OO,
"delegate the work to the object with the data"
"delegate the work to the object with the data"
●
Does this suggest a way forward?
Does this suggest a way forward?
●
This leads to the idea that an Entity has a collection of
This leads to the idea that an Entity has a collection of
named Actions (say) associated with it
named Actions (say) associated with it
●
A "method call"
A "method call"
spade.take()
spade.take()
is implemented by
is implemented by
looking up a "take" action in the "spade" entity
looking up a "take" action in the "spade" entity
●
How do you know an entity is a thing that can be
How do you know an entity is a thing that can be picked
picked
up
up ? Answer, because it has a "take" action
? Answer, because it has a "take" action
●
How do you know an entity is a place? Answer,
How do you know an entity is a place? Answer,
because it has a "go" action
because it has a "go" action
●
The command "go south" becomes
The command "go south" becomes south.go()
south.go() so it is
so it is
the
the
place
place
that is responsible for getting you there (this is
that is responsible for getting you there (this is
unusual, bit it fits
unusual, bit it fits these
these circumstances)
circumstances)
●
Our example now looks something like this:
Our example now looks something like this:
beach
tree
sand
spade
spade
south north
sand
here
take
take
go
go
go
dig
dig
drop
drop