XML : To copy or not to copy

I have deleted my previous question since it was too complicated. I am creating a versatile data-driven system behavior. Since I cannot create objects in realtime whenever I need them I parse and store them all at the start of my program. These behaviors actually perform the actions for it's owner.

I could copy the behaviors and add the owner to them. But I think this is unnecessary and the behaviors can function as templates. So each owner with the same behavior is referring to the same object.

A behavior can activate when the owner experiences a event. That is why the behavior has a OwnerListener and when the behavior is compatible with the event it will run it's actions.

  public class Behavior implements OwnerListener {      List<Action> actions = new ArrayList<>();        HashMap<String, Object> additionalData = new HashMap<>();        public Behavior(XmlElement behaviorNode)      {          //Parse XML      }        @Override      public void seeSomeone(Object someone) {          //Since I need to run the actual greet action that was created by XML I need to store the person to greet.              additionalData.put("PERSON_SEEN", someone);            for (Action action : actions)          {              //if action is compatible with seeing someone action.perform();                //if that action actually needs the person it can take it from this behavior template additionalData.get("PERSON_SEEN");          }      }        @Override      public void getTalkedTo(Object talker) {          additionalData.put("BEING_TALKED_TO", talker);            for (Action action : actions)          {              //if action is compatible with being talked to action.perform();          }      }  }    

Now since I have a lot of these behaviors and some of them can be huge (many dozens of actions). I really do not feel like having a copy for each owner.

The bottleneck is that additional data field since the actions need access to specific data at the time of performing. They also need access to the owner but that could also be supplied by that data field in each of the listener methods.

This question still got way longer then I wanted. I hope it's more clear now. Do I really need to copy each behavior for each owner or are there other ways? My XML data is pretty big, a single behavior can have a lot of data and many dozens of actions. There will be "endless" amount of behaviors and each owner can potentially own each one.

No comments:

Post a Comment