











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 specification of entity classes naming link operations implementing links implementing actions implementing hiding Imp class world class stage class testing the package
Typology: Study notes
1 / 19
This page cannot be seen from the preview
Don't miss anything!












Ian Holyer
Ian Holyer
●
The Stage project is a nice implementation example The Stage project is a nice implementation example
●
Here, we'll discuss an implementation in Java to sort out
Here, we'll discuss an implementation in Java to sort out
the Object Oriented aspects the Object Oriented aspects
●
Later we'll look at implementing it in other languages,
Later we'll look at implementing it in other languages,
including JavaScript to see whether a scripting language
including JavaScript to see whether a scripting language
is a better match is a better match
●
If there is any conclusion, it is just that the choice of
If there is any conclusion, it is just that the choice of
language doesn't matter much, other than for silly
language doesn't matter much, other than for silly
reasons (e.g. web-based deployment favours JavaScript) reasons (e.g. web-based deployment favours JavaScript)
●
From the API specification, you may notice a personal From the API specification, you may notice a personal
preference
preference
●
This is a preference for one-word method names, This is a preference for one-word method names,
without without get get or or set set or or is is prefixes prefixes
●
The convention being used is The convention being used is
●
a normal method name is a verb
●
a getter or setter name is a noun
●
a boolean getter or setter name is an adjective
●
Let's have a first stab at representing links Let's have a first stab at representing links
●
What are the main Entity methods involving links?
What are the main Entity methods involving links?
cave.link("candle", dynamite) add a link
river = city.find("east") follow a link
... = here.links() for printing
●
Link order seems important (e.g. controls print order) Link order seems important (e.g. controls print order)
and a fixed order such as alphabetical gives no control, and a fixed order such as alphabetical gives no control,
so let's specify order of insertion
so let's specify order of insertion
●
What does What does links links () () return? There could be a Link return? There could be a Link
class containing a name and Entity, but it is simpler to
class containing a name and Entity, but it is simpler to
return a list of link names and use
return a list of link names and use find
find to get to the
to get to the
entities entities
●
We can store We can store both both a hash table a hash table and and a matching list: a matching list:
class Entity
{
private Map<String,Entity> links;
private List
void link(String n, Entity e) {...}
Entity find(String name) {...}
List
}
class Entity
{
private Map<String,Entity> links;
private List
void link(String n, Entity e) {...}
Entity find(String name) {...}
List
}
●
Question: what bad feature does this code have? Question: what bad feature does this code have?
●
The problem is with this: The problem is with this:
List
List
●
The private list The private list names names is being exposed, and the caller is being exposed, and the caller
could (accidentally) change the list, spoiling the links
could (accidentally) change the list, spoiling the links
●
One solution is to make the list immutable: One solution is to make the list immutable:
List
{
return Collections.unmodifiableList(names);
}
List
{
return Collections.unmodifiableList(names);
}
●
Question: what bad feature does this code still have? Question: what bad feature does this code still have?
●
Here's one approach: Here's one approach:
class Entity ...
class Action extends Entity ...
class Entity ...
class Action extends Entity ...
class Entity ...
{
boolean active() { return script != null; }
}
class Entity ...
{
boolean active() { return script != null; }
}
●
When you try to use an entity as an action, e.g. When you try to use an entity as an action, e.g. spade spade
sand
sand , it should respond just as if the action didn't exist
, it should respond just as if the action didn't exist
●
Since Action just adds one field (a script), it is much
Since Action just adds one field (a script), it is much
simpler to just implement: simpler to just implement:
●
We want each link to be public or private We want each link to be public or private
●
One approach is for the link name to have a prefix, e.g.
One approach is for the link name to have a prefix, e.g.
+spade"
+spade" or "-
or "- spade"
spade"
●
Is this OK? Only if:
Is this OK? Only if:
●
you realise it is a dirty trick, and make sure the trick
cannot be detected from outside the class
●
that means adding and removing the prefixes at
suitable points
●
and making sure there are never two links with the
same name, +x and -x
●
Probably the "right" way to implement hidden links is: Probably the "right" way to implement hidden links is:
Map<String,Pair<Boolean,Entity>> links;
Map<String,Pair<Boolean,Entity>> links;
●
If Java had a convenient, ready to use, library class for
If Java had a convenient, ready to use, library class for
pairs, this would be attractive pairs, this would be attractive
●
As Java doesn't have such a thing, you have to define a As Java doesn't have such a thing, you have to define a
class for yourself, which ends up quite ugly
class for yourself, which ends up quite ugly
●
Which of the 3 techniques should you use? Answer, Which of the 3 techniques should you use? Answer,
whatever you like, so long as you are accurately aware whatever you like, so long as you are accurately aware
of the pitfalls, and implement it correctly so that the
of the pitfalls, and implement it correctly so that the
outside world can't tell which technique you are using
outside world can't tell which technique you are using
●
This one little simple class has thrown up several points: This one little simple class has thrown up several points:
●
specifications (even just in comments or tests) need to
be complete, e.g. does the order of the links matter?
●
defensive copying may be needed to protect internal
structures
●
beware structures which may change while you are
iterating through them
●
any dirty implementation tricks can be used, provided
you are absolutely certain that the external view is
clean
●
The next class to be developed was the World class, The next class to be developed was the World class,
which loads and saves games using a simple text format
which loads and saves games using a simple text format
●
In the file, each entity needs a unique name, and for In the file, each entity needs a unique name, and for
testing it is convenient to use the same name on saving testing it is convenient to use the same name on saving
●
Instead of storing an id in the entity, this is kept local to
Instead of storing an id in the entity, this is kept local to
the World class by storing ids and entities in a map the World class by storing ids and entities in a map
●
When a game starts, it needs to print an initial message, When a game starts, it needs to print an initial message,
and the easiest way to do that is to obey an initial
and the easiest way to do that is to obey an initial
command (not typed in by the user)
command (not typed in by the user)
●
Originally, the command was "start game", but this is Originally, the command was "start game", but this is
English-only, so now it uses the first noun attached to
English-only, so now it uses the first noun attached to
the starting place, and the first verb attached to that
the starting place, and the first verb attached to that
●
The Stage class provides the user interface for the The Stage class provides the user interface for the
program, printing out text justified within a fixed page
program, printing out text justified within a fixed page
width, printing prompts, and reading commands
width, printing prompts, and reading commands
●
The Stage class also provides the main program, and The Stage class also provides the main program, and
does minor language-dependent adjustments to the text, does minor language-dependent adjustments to the text,
e.g. standard messages when something isn't found, and
e.g. standard messages when something isn't found, and
converting
converting "you find a apple"
"you find a apple" into
into "you find an apple"
"you find an apple"
●
To help these language adjustments, the text from To help these language adjustments, the text from
obeying a command is passed back as a list of
obeying a command is passed back as a list of
fragments, and the adjustments are only done between
fragments, and the adjustments are only done between
fragments fragments
●
Maybe this should be done using a language adapter Maybe this should be done using a language adapter
class plugged into the Imp
class plugged into the Imp
●
An irritating feature of Java packages is that you have to An irritating feature of Java packages is that you have to
compile and run the classes from outside the package,
compile and run the classes from outside the package,
and also that Java command lines can get long
and also that Java command lines can get long
●
Both problems are solved in this project by including Both problems are solved in this project by including
some small (Unix) shell scripts inside the package some small (Unix) shell scripts inside the package
●
That allows you to sit inside the package directory while That allows you to sit inside the package directory while
editing the classes editing the classes
●
In a bigger project, you can also arrange for the
In a bigger project, you can also arrange for the
generated class files to be stored elsewhere so they don't
generated class files to be stored elsewhere so they don't
clutter up the source directory while you are working clutter up the source directory while you are working