0

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.

4

2 回答 2

0

Use immutable Strategy objects, so you can share them.

于 2016-03-23T19:56:01.437 回答
0

How many billions of object instances do you need to maintain?

As you already stated you to be in need for storing owner related data, you may not expect to get less instances.

n the other hand, the actual code already is kept shared by the class instance and is not "copied" for each instance. So, the object instances only use their data space.

If the XML configuration you are using to setup the individual instances and thos are "constant" among all owners. You might use a split implementation. Instantiate an object that is "known" by the owners and initialized with the related XML, but then forward that XML to a factory class that maintains XML code specific instances of a class that is then used as a local instance and is shared among all instances of "same" behaviour that gets the data passed in for processing. Of course those behavioural instances need to be implented as immutable objects to allow proper sharing.

于 2016-03-23T19:57:39.507 回答