Page 1 of 1
advantage of using getters and setters in OOP??
Posted: Tue Jun 05, 2007 5:58 am
by PHPycho
Hello forums !!
I would like to know
1> the advantage of using getters and setters in class ?
2> is directly accessing the properties of class harmful ?
3> good techniques for designing the robust class ?
Thanks in advance to all of you!!
Posted: Tue Jun 05, 2007 7:24 am
by feyd
- Getters (accessors) and setters (mutators) give you control over the information placed into the properties. There are many times where very specific information needs to be in properties. If access wasn't controlled someone could place an unexpected value and potentially break the class' code.
- Read #1
- Methods should, with little exception, do one thing. Classes should, with little exception, do one thing too, but at a more macro level than methods. Each method should generally be a verb that the noun (class) does.
Consider a car. It's a kind of vehicle, which is made up of many components. Some of these components are wheels, windows, doors, a steering wheel, seats, engines, "fuel" sources, etc. In turn, those components are made of components. A car can carry people and many arbitrary objects.
Posted: Tue Jun 05, 2007 8:40 am
by superdezign
However, in ActionScript (Flash), we avoid traditional get and set functions. The horrible thing about ActionScript 2.0 is that running a method consumes a lot more memory than just performing the operation. But every other language that supports OOP, use getters and setters. ActionScipt is like, the only exception. (Although they have a built in set/get setup that feels like you're not using them, but you are. It's weird, really.)
Just thought I'd throw that in. ^_^