开发者

How would you update 100+ variables if something is changed in a different class?

开发者 https://www.devze.com 2023-01-03 11:06 出处:网络
I have a class Grid which produces a graph paper like grid on in the drawing area. I then have 5 other classes for different shapes to draw with; Line, Polygon, Ellipse, Curve, Arc

I have a class Grid which produces a graph paper like grid on in the drawing area. I then have 5 other classes for different shapes to draw with; Line, Polygon, Ellipse, Curve, Arc

Now, these 5 classes use an instance of Grid because Grid has a resolution and a scale. Inside Grid I have:

public function set resolution(x:Number):void {
    _gap = (modBy10(x) /开发者_高级运维 10);
    _scale = (modBy10(x) / (this.resolution * _scale));

    draw();
}

public function get resolution():Number {
    return (_gap * 10);
}

public function set scale(x:Number):void {
    _scale = (this.resolution / x);
}

public function get scale():Number {
    return _scale;
}

/**/

public function scaleLength(x:Number):Number {
    return (x * this.scale);
}

public function scaleLengthDown(x:Number):Number {
    return (x / this.scale);
}

public function scaleArea(x:Number):Number {
    return (x / Math.pow(this.scale, 2));
}

I'm just lost for a solution on how to update every instance of my 5 drawing classes when Grid is changed.

For instance, Polygon is made up of multiple instances of Line, Line(length, angle) where "length" is in either in, ft, cm, or m. If the user wishes to change the scale from say 10ft per 100px resolution to 20ft per 80px.. Is there an easier way than re-drawing every Line inside Polygon?


I wouldn't have all the other objects depend on the Grid for scale.

Rather I would have a container called something like ScaledDrawingSurface that contains all of the shape objects and the Grid object.

The container would then have a scale propety that could be changed.

You then have two options for dealing with a change in the scale property:

  1. Have an event that is dispatched by the ScaledDrawingSurface when the scale changes. All of the shapes and the grid would listen for this event and update themselves accordingly. If you do this, you will want to make a custom ScaleChangedEvent that includes a property for the new scale.

  2. Have the ScaledDrawingSurface iterate through all of its children setting their scales appropriately. If you do this, you will want to have an IScalable interface with a SetScale method that the shapes and the grid implement.

In either case, you can make composite objects (e.g. polygon) responsible for updating their child objects, using either event dispatching or iterative processing.

One nice bonus of this approach is it would be very easy to have multiple overlayed grids (like graph paper) that update to the scale of the surface - you would just need a property on the Grid class to set the spacing of each grid relative to the global scale (and probably colour and thickness properties)


a way to handle this is the composite pattern and dependency inversion.

alternatively, you could stick the all drawn objects into one container and just scale the container accordingly.

greetz
back2dos

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号