Get/Set Methods In OOD

Started by
26 comments, last by Dae 15 years, 3 months ago
What's the purpose of get/set methods when I can just declare the member public? Doesn't this do virtually the same thing?
Advertisement
First, I'm going to assume that you mean plain get/set that don't add behavior; further, that both of them exist for a particular datapoint.

In that case, it's done as a matter of future-proofing. Should you ever need to change the details of a class (say, adding in some temporary debugging logic) you don't need to go through every single bit of code that uses the data and change it to use the new get/set methods rather than the object itself. It's part of the motivation behind C#'s property design. That sort of thing isn't necessary there. In Java or C++ the interface changes when you change a public member into a get/set pair.
It's useful when you might want to perform secondary actions, like recalculating or caching a calculated value. Sometimes you don't know ahead of time when you'll want to add some new functionality that will require adding secondary effects in a get/set method.

It's also useful when other people are going to use your objects. They can look at *just* the object's methods instead of wondering which variables they should be tinkering with.

Finally, it's useful in designing objects since it helps you "program to an interface" as opposed to "programming to an implementation".
And of course it stops other objects from messing up your variables in unpredictable ways
I trust exceptions about as far as I can throw them.
Quote:Original post by Kenny77
What's the purpose of get/set methods when I can just declare the member public? Doesn't this do virtually the same thing?



Its a convention. The generalization allows you to later add validation code for the set values if needed and can simplify the get/set if the real data element(s) is buried more complexly in the class data structure.

More need in future will be facilitating a multiprocess data access wrapper the detaisl of which can be hidden.

Of course making it a method you can do the whole class override mechanism.

--------------------------------------------[size="1"]Ratings are Opinion, not Fact
Also, having a get and no set is a way to create "read only" public access while using any sort of backing implementation.
If you want to look it up, you'll find it under abstraction and information hiding.
010001000110000101100101
Quote:Original post by Kenny77
What's the purpose of get/set methods when I can just declare the member public?

You shouldn't do either. get/set methods are not what OO is about. The class should be operating on the encapsulated data instead.
Quote:Original post by Telastyn
It's part of the motivation behind C#'s property design. That sort of thing isn't necessary there. In Java or C++ the interface changes when you change a public member into a get/set pair.


I've recently added a small c++ dump to wikipedia, showing that properties are simply unnecessary in c++ as they can be "emulated" by means of other features:

#include <iostream> template <typename T> class strictly_typed_property {        T value;    public:        T & operator = (const T &i) {            ::std::cout << i << ::std::endl;            return value = i;        }        // This template class member function template serves the purpose to make        // typing more strict. Assignment to this is only possible with exact identical        // types.        template <typename T2> T2 & operator = (const T2 &i) {            ::std::cout << "T2: " << i << ::std::endl;            T2 &guard = value;            throw guard; // Never reached.        }        operator T const & () const {            return value;        }}; struct Foo {    // Properties using unnamed classes.    class {            int value;        public:            int & operator = (const int &i) { return value = i; }            operator int () const { return value; }    } alpha;     class {            float value;        public:            float & operator = (const float &f) { return value = f; }            operator float () const { return value; }    } bravo;}; struct Bar {    // Using the property<>-template.    strictly_typed_property <bool> alpha;    strictly_typed_property <unsigned int> bravo;}; int main () {    Foo foo;    foo.alpha = 5;    foo.bravo = 5.132f;     Bar bar;    bar.alpha = true;    bar.bravo = true; // This line will yield a compile time error                      // due to the guard template member function.    ::std::cout << foo.alpha << ", "                << foo.bravo << ", "                << bar.alpha << ", "                << bar.bravo                << ::std::endl;}


Basically it means that you can add temporary debugging features without get/set-pairs in c++, too. While the syntax is maybe a bit clumsy to c++ newcomers, it really adds another safety barrier, as the physical memory region can be hidden from the containing class. As far as I know, the latter is not possible in c# (but then I am not as fluent in c# as in c++). As far as I know, the latter is only possible with an additional named class:

class SomeClass {    private class SomeIntProperty {        private int someValue;        public SomeValue {            set { someValue = value; }            get { value = someValue; }        }    }    SomeIntProperty someInt = new SomeIntProperty ();    public SomeInt {        set { someInt = value; }        get { value = someInt; }    }}

Quote:
What's the purpose of get/set methods when I can just declare the member public? Doesn't this do virtually the same thing?


The purpose is to add an extra layer, where you can operate on the data prior to it being get/set.

You can use that for debugging purposes, or if you need to add something in later that you didn't think about originally (ie. a hack or a new feature) you can do so easily.

I've worked on afew big projects recently with work and have had to do both of the above. I can say 95% of the time it just means extra code, for no benefit, and just wastes time. But that 5% when i can just change something in a get/set more than makes up for the extra time spent writing them in the first place, and saved so many headaches. For a smaller project it may not be so useful.

Quote:
You shouldn't do either. get/set methods are not what OO is about. The class should be operating on the encapsulated data instead.


Can you explain this abit more?


This topic is closed to new replies.

Advertisement