Java OO design confusion: how to handle actions modified by states modified by actions...

Posted by Arvanem on Stack Overflow See other posts from Stack Overflow or by Arvanem
Published on 2010-04-23T04:41:15Z Indexed on 2010/04/23 4:43 UTC
Read the original article Hit count: 302

Filed under:
|
|
|

Hi folks,

Given an entity, whose action is potentially modified by states (of the entity and other entities) in turn potentially modified by other actions (of the entity and other entities) , what is the best way to code or design to handle the potential existence of the modifiers?

Speaking metaphorically, I am coding a Java application representing a piano. As you know a piano has keys (which, when pressed, emit sound) and pedals (which, when pressed, modify the keys' sounds).

My base class structure is as follows:

  1. Entity (for keys and pedals)
  2. State (this holds each entity's states, e.g. name such as "soft pedal", and boolean "Pressed"),
  3. Action (this holds each entity's actions, e.g. play sound when pressed, or modify others sounds).

By composition, the Entity class has a copy of each of State and Action inside it. e.g.:

public class Entity {       
   State entityState = new State();
   Action entityAction = new Action();

Thus I have coded a "C-Sharp" key Entity. When I "press" that entity (set its "Pressed" state to true), its action plays a "C-Sharp" sound and then sets its "Pressed" state to false. At the same time, if the "C-Sharp" key entity is not "tuned", its sound deviates from "C-Sharp".

Meanwhile I have coded a "soft pedal" Entity. When that entity is "pressed", no sound plays but its action is to make softer the sound of the "C-Sharp" and other key entities.

I have also coded a "sustain pedal" Entity. When that entity is "pressed", no sound plays but its action is to enable reverberation of the sound of the "C-Sharp" and other key entities.

Both the "soft" and "sustain pedals" can be pressed at the same time with the result that keys entities become both softened and reverberating.

In short, I do not understand how to make this simultaneous series of states and actions modify each other in a sensible OO way. I am wary of coding a massive series of "if" statements or "switches".

Thanks in advance for any help or links you can offer.

© Stack Overflow or respective owner

Related posts about java

Related posts about action