Tuesday, June 2, 2009

generating getters and setters

I'm down with OOP and using getters and setting instead of exposing class member variables, but making getters and setters is a pain. Flex and FTD have tools to help automate the process, but they don't automate it enough. I wanted to be able to type in a list of vars like this...

private var _newWidth : Number;
private var _newHeight : Number;
private var _scaleList : Array;
private var _moveList : Array;
private var _oldWidth : Number;
private var _oldHeight : Number;
private var _widthPercentChange : Number;
private var _heightPercentChange : Number;

... and, via a single click, get this...

public function get newWidth() : Number
{
return _newWidth;
}

public function set newWidth( value : Number ) : void
{
_newWidth = value;
}

public function get newHeight() : Number
{
return _newHeight;
}

public function set newHeight( value : Number ) : void
{
_newHeight = value;
}

public function get scaleList() : Array
{
return _scaleList;
}

public function set scaleList( value : Array ) : void
{
_scaleList = value;
}

public function get moveList() : Array
{
return _moveList;
}

public function set moveList( value : Array ) : void
{
_moveList = value;
}

public function get oldWidth() : Number
{
return _oldWidth;
}

public function set oldWidth( value : Number ) : void
{
_oldWidth = value;
}

public function get oldHeight() : Number
{
return _oldHeight;
}

public function set oldHeight( value : Number ) : void
{
_oldHeight = value;
}

public function get widthPercentChange() : Number
{
return _widthPercentChange;
}

public function set widthPercentChange( value : Number ) : void
{
_widthPercentChange = value;
}

public function get heightPercentChange() : Number
{
return _heightPercentChange;
}

public function set heightPercentChange( value : Number ) : void
{
_heightPercentChange = value;
}

And now I can, because I built a little AIR app to do it for me. I can copy in the private vars, press the GENERATE button, and -- BINGO! -- out pops my getters and setters. Then I can click the COPY button, which copies the getters and setters to the clipboard. Finally, I can paste them into Flex or FDT.



Here's a link to the app.

Notes: it is template-based, so if you don't like the code it generates, it's easy to make it do what you want. Click HELP for more info.

Also, please note that though this is a useful tool, it can be abused. For instance, some properties should be read only. They shouldn't have a setter. So use with caution and good OOP practices!

No comments:

Post a Comment