mercredi 16 décembre 2015

How to implement a flyweight pattern with overridable properties?

I want to implement a system of components that are shared between multiple nodes of a tree. I imagined the Flyweight pattern is really good for this use. The component is instantiated only once and this instance is given to each node that needs it. This way, if a property of the component changes, it is immediately shared to the nodes.

Now, I want to change just one single parameter of a component on a node without affecting the other nodes. But I don't want to copy the instance of the component in order to do that.

  • If the property is changed at a component-level, the new value will be shared to the nodes, unless this property was explicitly changed on the node.
  • If the property is changed at a node-level, it overrides the one from the component.

What I want to create is a mechanism similar to the scene tree in Unity were we can override some properties of a component at node-level (for example, when we use prefabs) without affecting original resources unless we explicitly want to.

Now, I don't know how to make sort of a flyweight pattern with some kind of overridable properties.

Can you help me?

Thanks

Aucun commentaire:

Enregistrer un commentaire