A
Adam Clauss
Let's say I have a class that represents a rectangle:
class Rectangle
{
private int width;
private int height;
private int area;
public int Width
{
get { return width; }
set { width = value; }
}
public int Height
{
get { return height; }
set { height= value; }
}
public int Area
{
get { return area; }
}
}
Obviously, area does not represent anything based on that. How to best
keep area in sync is the purpose of this question. One way would be to
simply get rid of the variable area, and make the property calculate it:
public int Area
{
get { return Width * Height }
}
But, for the sake of argument, lets pretend Width * Height was actually
a more expensive calculation, and we would prefer to cache the value
when it changes, rather than recalculate it every time Area gets called.
One option then, would be for the Width and Height properties to update
area, something like:
public int Width
{
get { return width; }
set
{
width = value;
UpdateArea(); // some method that performs the area calculation -
it's implementation is not relevant
}
}
Another option, assuming you were following a pattern similar to typical
..NET UI components (maybe other components too), would be to add events
for each of the properties changing:
public EventHandler WidthChanged;
public int Width
{
get { return width; }
set
{
if (width != value)
{
width = value;
WidthChanged(); // ignore unsafe event invocation, just making
the point
}
}
If there is such an event defined, is there a "good" way to update
Area? Should width/height directly know that they affect area and
therefore call UpdateArea(). Or, should the Rectangle class register an
event handler to it's own event (WidthChanged/HeightChanged) and invoke
UpdateArea() from there.
Any thoughts on the "best pattern"? Is there even a general rule, or is
it more just a case-by-case basis? Or should a class almost NEVER have
a reason to subscribe to its own event?
-Adam
class Rectangle
{
private int width;
private int height;
private int area;
public int Width
{
get { return width; }
set { width = value; }
}
public int Height
{
get { return height; }
set { height= value; }
}
public int Area
{
get { return area; }
}
}
Obviously, area does not represent anything based on that. How to best
keep area in sync is the purpose of this question. One way would be to
simply get rid of the variable area, and make the property calculate it:
public int Area
{
get { return Width * Height }
}
But, for the sake of argument, lets pretend Width * Height was actually
a more expensive calculation, and we would prefer to cache the value
when it changes, rather than recalculate it every time Area gets called.
One option then, would be for the Width and Height properties to update
area, something like:
public int Width
{
get { return width; }
set
{
width = value;
UpdateArea(); // some method that performs the area calculation -
it's implementation is not relevant
}
}
Another option, assuming you were following a pattern similar to typical
..NET UI components (maybe other components too), would be to add events
for each of the properties changing:
public EventHandler WidthChanged;
public int Width
{
get { return width; }
set
{
if (width != value)
{
width = value;
WidthChanged(); // ignore unsafe event invocation, just making
the point
}
}
If there is such an event defined, is there a "good" way to update
Area? Should width/height directly know that they affect area and
therefore call UpdateArea(). Or, should the Rectangle class register an
event handler to it's own event (WidthChanged/HeightChanged) and invoke
UpdateArea() from there.
Any thoughts on the "best pattern"? Is there even a general rule, or is
it more just a case-by-case basis? Or should a class almost NEVER have
a reason to subscribe to its own event?
-Adam