Erlang/OTP Forums

Author Message

<  Erlang  ~  State handling with module inhertance

flodis
Posted: Sat Oct 31, 2009 3:46 pm Reply with quote
User Joined: 09 Jul 2008 Posts: 27
I am having some thoughts how implement an easy state management with module inheritance.

I have these modules:
obj.erl
living.erl
animal.erl
player.erl

Each of them -extends() the lower module in a natural order.

My problem is how to best handle state, each object type has its own state, but the lower object only operates on their own state. For example when accessing a function in animal it wants the animal state, not the player state.

This gives me the obvious problem that I have to extract the correct state, which makes the code messy and impossible to maintain.
Adding a function to the obj.erl would require updates in all other objects.

I thought of using a generic state of all objects, and store type specific data in the process dictionary.
Is there anyone with an alternative solution for my problem?
View user's profile Send private message
Allan
Posted: Sun Nov 01, 2009 9:30 am Reply with quote
User Joined: 29 Jun 2009 Posts: 30
flodis wrote:
My problem is how to best handle state, each object type has its own state, but the lower object only operates on their own state. For example when accessing a function in animal it wants the animal state, not the player state.


Use something like [{atom(), term()}] to store all the data of an "object". Then it is easy to add new properties as needed. animal:is_alive/1 will simply access the {'is_alive', boolean()} entry in the given properties list - and player:get_level/1 may use the {'pc_level', integer()} entry from the given property list.

flodis wrote:
I thought of using a generic state of all objects, and store type specific data in the process dictionary.
Is there anyone with an alternative solution for my problem?

Do not abuse the process dictionary!

Better use ETS, gen_server processes, or something similiar for storing the object states. Static ("class") properties that are equal for all "instances" of a type (such as animal or player) can be stored there too.
View user's profile Send private message Send e-mail ICQ Number

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum