Design, Lecture Slides - Computer Science, Slides of Computers and Information technologies

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

2010/2011

Uploaded on 09/06/2011

stifler_11
stifler_11 🇬🇧

4.6

(9)

272 documents

1 / 41

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Design, slide 1
Design
Ian Holyer
Ian Holyer
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Design, Lecture Slides - Computer Science and more Slides Computers and Information technologies in PDF only on Docsity!

Design

Ian Holyer

Ian Holyer

Case Study: Stage

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

Where to Start the Design

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

A First Attempt

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 things;

}

class Place

{

Place north, east, south, west;

List things;

}

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 Flexibility Problem

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

The Flexibility Problem

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: ...

A Metaphor

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

Place and Thing Objects

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

The Entity Class

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?

No Names for Entities

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

Commands

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?

Hints

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?

Answered Questions

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)

Actions

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