How can I update multiple object references at once?

  • Thread starter Thread starter Jocke
  • Start date Start date
J

Jocke

Hello

I'm working on an application where some common objects are consumed
thorough the application. The issue is that such a common object - say some
kind of graphics object - shall be replaced by another object now and then,
and then all references should be updated at once. Say I reference the same
graphics object ten different places in the application. How can I create a
new graphics object and then make all of the ten references point to this
new object?

Well, I know I could do this by calling a GetNewestGraphicsObject() method
instead of having references directly to the commonly used obejcts. Then
this intermediate method returns the newest object. But is there a way to
achieve this without using methods?
 
I'm working on an application where some common objects are consumed
thorough the application. The issue is that such a common object - say some
kind of graphics object - shall be replaced by another object now and then,
and then all references should be updated at once. Say I reference the same
graphics object ten different places in the application. How can I create a
new graphics object and then make all of the ten references point to this
new object?

Well, I know I could do this by calling a GetNewestGraphicsObject() method
instead of having references directly to the commonly used obejcts. Then
this intermediate method returns the newest object. But is there a way to
achieve this without using methods?

Take a look at the Observer pattern. The Subject object can change state and
any other objects that are Observers of that Subject will be notified when
the change occurs. The idea of a multicast delegate as used in .NET events
fulfils this pattern perfectly.

Joanna
 
Jocke said:
I'm working on an application where some common objects are consumed
thorough the application. The issue is that such a common object - say some
kind of graphics object - shall be replaced by another object now and then,
and then all references should be updated at once. Say I reference the same
graphics object ten different places in the application. How can I create a
new graphics object and then make all of the ten references point to this
new object?

Well, I know I could do this by calling a GetNewestGraphicsObject() method
instead of having references directly to the commonly used obejcts. Then
this intermediate method returns the newest object. But is there a way to
achieve this without using methods?

Could you actually have an object with the same interface, which just
proxies to whatever the most current real object is? That would be my
first thought as to the best way round it.
 
Jocke said:
Hello

I'm working on an application where some common objects are consumed
thorough the application. The issue is that such a common object - say some
kind of graphics object - shall be replaced by another object now and then,
and then all references should be updated at once. Say I reference the same
graphics object ten different places in the application. How can I create a
new graphics object and then make all of the ten references point to this
new object?

Well, I know I could do this by calling a GetNewestGraphicsObject() method
instead of having references directly to the commonly used obejcts. Then
this intermediate method returns the newest object. But is there a way to
achieve this without using methods?

Does a "property" count as a "method"?

public class CommonGraphicsObject
{
private static GraphicsObject _currentObject = null;

public static GraphicsObject CurrentObject
{
get { return _currentObject; }
set { _currentObject = value; }
}
}

Then you just reference CommonGraphicsObject.CurrentObject everywhere.

Similar to Joanna's suggestion, you could fire an event in the setter if all
references needed to know when a new object was set.

OTOH, this is very, very similar to calling GetNewestGraphicsObject() -
which you wanted to avoid.
 
Back
Top