'Decorator' pattern implemented by extending objects with mixins- need guidance.

  • Thread starter Asfand Yar Qazi
  • Start date
A

Asfand Yar Qazi

Hi,

I'm a newb at this language, and now that I've seen it, I think I will
use it for every project I do from now on (apart from my software 3D
engine :).

Anyway, here's the thing: I'm writing a game, and need to implement the
following behaviour.

A tree is made of Node objects.

Each Node can have 0 or more child-nodes (the Node becomes their parent
node.)

Each Node can have several Attrs (attributes) attached to it.

Some Attrs are inheritable, i.e. if a parent node has an inheritable
Attr, its child nodes do as well. However, not all Attrs are inheritable.

Attrs should be able to be added and removed dynamically.

Some Attrs require other Attrs, either from the current node, an
ancestor node or from a child node. This relationship could be
compulsory, or optional (in which case the relationship should be
removed if the relavent Attr is removed from its node.)

Some Attrs are Singleton Attrs, i.e. they can only exist on one Node in
a Tree. It should be able to find these Singleton Attrs without having
to traverse the tree, i.e. their location should be cached in a Hash or
something.


I don't know which of Ruby's features are best for implementing this
behaviour. The Delegate model is nice for the inherited Attrs, however
not all Attrs are meant to be inheritable.

Creating attributes as mixins and extending the Nodes with them is a
good way of adding attributes dynamicallly, but then they can't be
removed (I think.)

Just using a Hash and using 'Node.add/remove(Attr)' methods is a
technique that springs to mind, but I was thinking of a "Ruby" way of
doing things.

Any thoughts?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top