PDA

View Full Version : return a protected var who's props are also protected


lordofduct
05-04-2008, 11:39 PM
I want to be able to have a property of an Class that is protected, but can be pulled with a get function. BUT this get function protects the props of this object.

for instance:


public class CustomClass
{
protected var _bounds:Rectangle;

public function get bounds():Rectangle { return _bounds; }

public function CustomClass(x:Number, y:Number, w:Number, h:Number):void
{
_bounds = new Rectangle(x,y,w,h);
}
}


but if I instantiate it and say:

myCustomClass.bounds.width = 500;

the bounds will be updated. How can I make it where the props of this protected object stay protected?

I know I could pass a clone of the property which would keep it protected, but takes up more memory. OR I could set functions in the CustomClass that only return these sub props, and doesn't set them. Kinda like this:


public class CustomClass
{
protected var _bounds:Rectangle;

public function get boundsWidth():Number { return _bounds.width; }
public function get boundsHeight():Number { return _bounds.height; }

public function CustomClass(x:Number, y:Number, w:Number, h:Number):void
{
_bounds = new Rectangle(0,0,200,200);
}
}


but that's a bit of an annoyance, and it doesn't feel as natural to have to say:

var h:Number = myCustomClass.boundsHeight;

as it is to say:

var h:Number = myCustomClass.bounds.height;

That in this latter choice makes it REALLY annoying to use methods supplied by these protected objects. Like hitTests or the sort.


any suggestions?

lordofduct
05-05-2008, 12:08 AM
Another thing I was thinking of is extending the classes I want protected and create protected versions of them... like this, a protected rectangle:


package com.lordofduct.protectedObjects
{
import flash.geom.Rectangle;

public class ProtectedRectangle
{
protected var _rect:Rectangle;

public function ProtectedRectangle(x:Number = 0, y:Number = 0, width:Number = 0, height:Number = 0)
{
_rect = new Rectangle(x,y,width,height);
}

public function get width():Number { return _rect.width; }
public function get height():number { return _rect.height; }
public function get x():Number { return _rect.x; }
public function get y():Number { return _rect.y; }

//... etc

public function contains(x:Number, y:Number):Boolean
{
return _rect.contains(x,y);
}

//... etc
}
}


But this limits my use as well... because the class I instantiate this rect in has to consider it completely protected as well... not cool. In that case, I could probably make it internal to the framework.

To give you an idea of what I want to do with this is this. I have a class that describes a grid of tiles that represents a rectangular map. There is a Rectangle in it that represents the visual boundaries of the map, width, height, position. This map is generated via an XML file and constructs the entire map from it, and sets its own boundary rectangle. I want people to be able to read this rectangle and get information from it and use its methods, like contains and whats the width. But I don't want them able to alter the rectangle.

Here's the class in progress:
www.lordofduct.com/storage/AStarMap.as

Flash Gordon
05-05-2008, 12:29 AM
Well it sounds like you already know the answers. What are you willing to trade: memory, speed, strict encapsulation....etc.

Something to consider, because your constructor

public function CustomClass(x:Number, y:Number, w:Number, h:Number):void
takes numbers not associated with an object and you are returning an object of these numbers (rectangle), you are now exposing the internals of your class. The classes that uses CustomClass also has to know about Rectangle in addition to Number.

For speed, it takes longer to access properties in a outside class than it does to access properties directly in the class. So if speed is an issue, maybe you really do just want to pass the reference. But like you said, you are losing control over the object.

In the end, it's your call if you are the archetic. You need to ask yourself, why does another object need these properties, how frequently (speed), and how tight of a lid do you want on it.

Cheers