Are there any benefits to getters and setters?

January 20th, 2010

Well once upon a time, my answer would of definately been a resounding no....I, along with many other people from what I have read and heard never really saw the point to them. Why go through all the hassle of typing out all that extra code when you can just declare your variables as public so that they can be accessed outside the class?!
Well now I can fully see the usefulness of this little coding gem and how it fits nicely into proper OOP principles, I have done for a while and have been fully using them to boost the reusability of my frameworks, but only today really thought about how great they really are.

Public variables are great if you simply want your variables to be easy to access, but using a setter for something like this makes  things a lot easier in the long run. Lets have a look at a real world example. Lets say you have created a custom class that creates a sprite with a text field. Now to change the text in this class from outside you would access the public String variable that difines the text, then you would have to access the actual text field of the class, which of course would have to be public for you to access it which of course opens the textfield up to be able to be edited from out the class....not good as you really want to have things like this closed, unless of course it really suits you to have the textfield as public, but any way it goes against the OOP principle of encapsulation. Also everytime you want to change the text in the textfield from outside the class you eventually have to enter more code as each time you access the string varaibale and also the textfield's text.  So if we use a setter for this like so:

Actionscript:
  1. public function set text(txt:String):void
  2. {
  3.     myTextField.text = txt;
  4. }

Now one thing you will notice here is that through accessing this setter you automatically update the textfield so that the text changes, so that all you have to type when targeting the instance from outside the class is:

Actionscript:
  1. myCustomTextField.text = "blah blah blah";

One line to change the text, and also no need to access any string variables or even set them. Its is the same with getters also making assets and their properties and also private variables easy to access.

Actionscript:
  1. public function get text():String
  2. {
  3.     return myTextField.text;
  4. }

would simply be accessed with:

Actionscript:
  1. trace(myCustomTextField.text);

So you can see that getters and setters really are very useful and something that I seem to use pretty much in every class I seem to create these days, they not only keep your code cleaner and easier to read but make your classes seem to make more sense and function a lot better.

Happy coding

Categories: Actionscript 3.0 | Tags: , | 6 Comments

TweetParser updated to include usernames and hashtags

December 21st, 2009

Well the title says it all, the tweet parser I created and mentioned in an earlier post has been updated to include @usernames and also #hashtags. The parser will format these tags into html formatted links ready to be included in your htmlText for your AS3 text fields.

Updated source files here

Categories: Actionscript 3.0 | Tags: , , | No Comments

Messing about with the AS3DMod library

December 10th, 2009

I don't know why but I have had AS3DMod in my collection of libraries for some time now, but have never gotten around to having a play with it....what fun I have been missing out on!.
It is a nice lightweight library that allows you to modify the vertex geometry of primitives in many of the most popular AS3 3D engines. It is very simple to use and applying a modifier is as simple as creating a modifier stack - targeting which ever 3D library you want which AS3Dmod supports, creating an instance of the modifier wanted and adding it to the stack. You can animate the variables of the modifer with your favourite tweening engine and come up with some nice effects.


The Flash plugin is required to view this object.




In the example here, I have used the four modifiers I think work best with a plane primitive and letting the tweens interact with each other if they have tweened in. I have also used Keith Peters minimalComps library for the radio buttons due to their simplicity and very low footprint, which are great for testing apps. You will also need to have the tweenLite library for this example and of course for the 3D library I have used Papervision.

If you want to have a look you can get the source here.

If I get a bit more time later I will have more of a play and see if I can produce something more interesting!

Categories: Actionscript 3.0, Papervision | Tags: , , | 4 Comments