Patterns for propagating changes to nested objects

2019-05-29 07:34发布

问题:

I am implementing a game/application where the player's account/state is synced to the server. I am contemplating a general framework communicating modifications of nested objects of an entity (the entity being the users's account). Let us assume for discussions of computation/reflection that both the client and server are written in Java (in reality client is in Actionscript which can modify properties dynamically)

Take for instance Firebase. Modifications to any object of the root object (a Firebase object) are propagated with a request that probably looks something like:

Service: PersistenceService
Action: modifiedObjects
Body:
Objects [{"/full/Path/To/Object/1","newValue"},{"/full/Path/to/Object/2","newValue"}]

My request for your input is the following:

1) Please correct and/or augment the following thoughts on implementing this general framework for propagating modifications to a tree of objects.

On the sending side, it would appear that every object either:

1) Needs to store it's full path from the root entity

2) Changes to properties of all nested objects need to be done reflectively

OR

A sync needs to forced, comparing the entity's saved object tree from the last request to the current object tree for modifications.

On the server side, one can analyze the paths of the objects to cache objects that are accessed multiple times in one request so as not to access the tree by reference/search collections multiple times.

回答1:

The answer I have come up with is actually very obvious the obviously the best way to do it. The answer is to mirror a database of tables. Assign each object an id, and store every object in an ArrayList (or assign each object a unique ID based on type and store the object in its type's ArrayList which is itself stored in a HashMap).

I call my interfaces ServiceObject and ServiceContainer.

Now the only thing I have to see that works is how json and protostuff serialize dual references to objects. Are they serialized as seperate objects? If so, than I any nested ServiceObject's need to deserialized as references to the objects in the ArrayList.



回答2:

Generally observer pattern is answer to kind of requirement you have (from wiki)

The observer pattern (aka. Dependents, publish/subscribe) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

You need implementation on the client server hence example given on the wiki is not applicable you might want to check this :

http://deepintojee.wordpress.com/2011/03/18/observer-pattern-applied-at-remote-level/