This is a question about best practices for code organisation. What’s a good pattern for complex Thing/Actor classes which have a bunch of “parts” attached to them. Here’s a toy example to illustrate:
#charset "us-ascii"
#include <adv3.h>
#include <en_us.h>
class Blob: Thing
vocabWords = 'blob'
location = startRoom
desc() {
"It looks like your average blob. ";
}
initializeThing() {
inherited;
new Pseudopod(self);
new BlobWibble(self);
new BlobWobble(self);
new BlobBibble(self);
new BlobBobble(self);
}
;
class Pseudopod: Component 'pseudopod' 'pseudopod'
"It's looks exactly like every other pseudopod you've ever seen. "
construct(obj) {
inherited;
location = obj;
}
;
class BlobNoise: Noise
isAmbient = true
descWithSource() { "<<self.blobNoise>> "; }
hereWithSource() { "<<self.blobNoise>> "; }
construct(obj) {
inherited;
location = obj;
}
;
class BlobWibble: BlobNoise 'wibble' 'wibble'
blobNoise = 'The blob goes wibble. '
;
class BlobWobble: BlobNoise 'wobble' 'wobble'
blobNoise = 'The blob goes wobble. '
;
class BlobBibble: BlobNoise 'bibble' 'bibble'
blobNoise = 'The blob goes bibble. '
;
class BlobBobble: BlobNoise 'bobble' 'bobble'
blobNoise = 'The blob goes bobble. '
;
theBlob: Blob 'blob' 'blob'
location = startRoom
;
startRoom: Room 'Void'
"This is a featureless void. "
;
me: Actor
location = startRoom
;
versionInfo: GameID
name = 'sample'
byline = 'nobody'
authorEmail = 'nobody <foo@bar.com>'
desc = '[This space intentionally left blank]'
version = '1.0'
IFID = '12345'
;
gameMain: GameMainDef
initialPlayerChar = me
;
This creates an abstract Blob
class. Each Blob gets one Pseudopod
instance and four Noise
instances.
Clearly this is a nonsensical example, and in this “game” there’s only one Blob
that gets instanced, theBlob
, so it would be easy to just throw the “stuff” onto it using the canonical + Noise 'bibble' 'bibble';
format. But if I eventually want to create a bunch of different Blob
instances it’s convenient if this sort of thing is baked into the parent class. Basically I don’t want to have to manually add all of the Noise
and Component
instances that are common to all Blobs every time I create an new Blob instance.
The code as written more or less works, but I’ve had to twiddle the Blob
class’ initializeThing()
, as well as over-writing the Component/Noise bits’ construct()
methods. Which feels a bit clumsy and brittle (in the sense that it’s likely to break if anything in the library changes).
Am I missing something in the library or some clever design pattern that could do this more cleanly?