| Home | Tutorials | Forums | Articles | Blogs | Movies | Library | Employment | Press | Buy templates |
|
|
#1 |
|
Site Contributor
|
Okay, I've been learning OOP principles bit by bit, and I'm reading a book about design patterns. I have read that you never want to use public variables, as it breaks encapsulation, and I understand all that, and really I understand why. However, suppose you're just doing a small project (or just goofing around, like me!), and you just need to create some quick classes that are only going to store some properties. On the one hand you can do this:
ActionScript Code:
ActionScript Code:
Or, consider a built in class like MovieClip. Do they really have getters and setters for every property? Thanks in advance for any responses!
__________________
My new website: theflashconnection.com |
|
|
|
|
|
#2 |
|
Obfuscated Coder
Join Date: Apr 2008
Posts: 695
|
Personally, I only break this rule in the case of custom events, which are really more just messengers than objects that are intended to stick around for any length of time. Why would I stick to it otherwise? Mainly it's because you don't always know when you'll need to track access to a variable. Yeah, you *could* go in and do it later, once it becomes apparent that you do, but it can be annoying to do that.
__________________
man.mask = mask_mc; Sigh. The AS3 version just doesn't look at nice as 'man.setMask(mask_mc);' |
|
|
|
|
|
|
|
|
#3 | |
|
Paintball Freak
|
Quote:
To be honest, I wasn't using getters and setters a year ago, I didn't even know what they exactly were... but since then, i've been using them all the time. And you are kinda right, I don't really declare my variables as public, I rather define a getter/setter if I want to modify them externaly... I feel that it makes the code more readable and organized and something I really like about is that you can give a really long name to your variable but have a simply and clear getter / setter manipulate it: Example: ActionScript Code:
I don't think that it's a must to define getters and setters for your vars but it definitely is considered a better practice then manipulating the variable directly. Some like getters, some don't, what matters is to have readable and non-ambiguous code.
__________________
FLASHFORUM.RO - You must speak Romanian in order to join. BLOG.WISEBISOFT.COM - Share and Experiment. Last edited by fx.barrett; 11-05-2008 at 01:12 AM.. |
|
|
|
|
|
|
#4 |
|
Site Contributor
|
Thanks guys, nice ideas. This has given me some more food for thought, I appreciate it.
__________________
My new website: theflashconnection.com |
|
|
|
|
|
#5 |
|
jordanrift.com
Join Date: Sep 2007
Location: Phoenix, AZ
Posts: 297
|
You don't *need* them, but as far as best OO practices go, it's definitely a good thing to use them instead of directly accessing those variables. You don't always need to, obviously, but there are times when you want to control access to those variables with customized logic:
ActionScript Code:
The syntax may not be perfect, but you get the idea. Using getters and setters is better than the alternative... ActionScript Code:
Last edited by yell0wdart; 11-04-2008 at 06:43 PM.. |
|
|
|
|
|
#6 |
|
Site Contributor
|
Okay, I solved my own issue. What I did is wrote out all the getters and setters in an Abstract Base Class. When my other classes extend the Base Class, they inherit all those getter and setter methods. Even though it seems like a lot more work than writing public variables, I really only had to do it once, then all my derived classes just inherit them. And even though each one simply sets or returns the associated private property, in my derived classes I can always override that default behavior if I want to.
I had an idea for a cool project, and I want to do it up OOP style. I wonder if anyone here (some who knows good OOP practices, preferably) would be interested in helping me develop it. If so, send me a PM and I'd be glad to tell you more about it.
__________________
My new website: theflashconnection.com |
|
|
|
|
|
#7 |
|
dondeEstanMisPantalones?
|
getters/setters should really only be used:
otherwise they are superfluous, and, if anything, add computational overhead. if you follow adobe convention, the protected or private property is identical to the getter/setter name, but prefixed with an underscore. as far as asdoc comments, the property gets marked @private, and the getter gets the commenting. http://opensource.adobe.com/wiki/dis...ng+Conventions |
|
|
|
|
|
#8 |
|
Site Contributor
|
Hey newblack,
Thanks for the response. I've been reading a lot about design patterns, and I'm attempting, via an abstract class, to program to an interface. I've got a nice little inheritance chain happening. And in the above, the phrase "associated private property" should really read "associated protected property."
__________________
My new website: theflashconnection.com |
|
|
|
|
|
#9 | ||
|
rather be programming
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
|
Quote:
Quote:
. Not to pick on newblack (he's awesome! and i do it too)...but let me just add you can't always anticipate when that is going to happen. It is easy enough to change later when need to a mutator/acessor but then you're class real isn't closed for modification and open for extension. (if it really matters for changing to a getter/setter). Yes, OOP does give overhead. Programming is often a trade of....take your pick.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman Last edited by Flash Gordon; 11-12-2008 at 07:29 AM.. |
||
|
|
|
|
|
#10 |
|
Member
Join Date: Aug 2007
Location: Ascot, UK
Posts: 67
|
100% agree with newblack -- if you are not applying any processing to the values, and they are read-write, don't waste your time with getters and setters. As mentioned, yes it will add overhead due to the scope change that occurs when you enter the relevant get or set function.
__________________
www.visualharmonics.co.uk |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|