Properties Shared Amongst Objects



In an application I'm writing, I have a group of objects that are instances
of the same class. These objects share the same property values. For
example, if I have the following class:

class Component
{
private:
float value;

public:
// Stuff...

float GetValue() const
{
return value;
}

void SetValue(float value)
{
this->value = value;
}
};

And I then have several objects of type Component:

Component c1;
Component c2;
Component c3;

The components are part of a single group, so the "Value" property has the
same value for all of the objects in the group. When SetValue is called on
one object, it needs to be called for all of the objects in the group with
the same value passed to each.

The Composite design pattern is applicable here in that I can add all of the
objects to a composite component and treat the group of objects as one.

So far, so good. However, there is a type of property in my application that
needs to be "smoothed" over time. In other words, instead of going from a
value of, say, 100 to 1000, the value needs to be interpolated between 100
to 1000, say over a few milliseconds. This is easy enough to realize by
running property changes through a lowpass filter. The cost of doing so is
non-trivial. If each object smooths the property changes itself, the
computation costs add up; since the property values are the same for a group
of objects, the interpolation is duplicated in each object.

My approach was to factor out properties into classes of their own.
Instances of the property classes are passed to component objects. In the
case of the "smoothed" properties, the smoothing takes place in one property
object and the results shared amongst a group of components.

class SmoothProperty
{
public:
void Render(int count)
{
// Smooth parameter changes...
}

const float *GetOutput() const
{
// Return the results of smoothing property changes.
return buffer;
}

float GetValue() const
{
return value;
}

void SetValue(float value)
{
this->value = value;

// Do stuff to calibrate property smoothing...
}
};

SmoothParameter param;
Component c1(param);
Component c2(param);

param.SetValue(1000);
param.Render(1024);

// etc...

I extended this approach to all properties for my components, not just
properties that need smoothing. So I have a set of general purpose property
classes that can be customized to represent properties in my components. In
many cases this saves some computational costs for the same reason that the
smooth property does; calculations made when a property changes value are
done in one place and are shared amongst many components.

In some cases components need to know when a property changes value. I've
set up a simple implementation of Observer so that components can hook into
properties in order to receive notification of value changes.

I'm looking for comments or insights into the above approach. Also, are
there any sources that describe an approach similar to the above?


.