07-04-2014 2:35 PM
Dear moderators,
this is a generic question which is independent of http://scn.sap.com/thread/3582195, so PLEASE keep this open.
Hello everybody,
I am not sure what is the most appropriate/robust way to change attributes of a class by the class' own private methods - just change them or use a parameter? Simple example:
CLASS sample_class DEFINITION.
PUBLIC SECTION.
METHODS main.
PRIVATE SECTION
DATA attribute TYPE c.
METHODS:
meth1,
meth2 CHANGING ch_attribute TYPE c.
...
ENDCLASS.
CLASS sample_class IMPLEMENTATION.
METHOD main.
me->meth1.
me->meth2( attribute ).
METHOD meth1.
me->attribute = 'X'.
ENDMETHOD.
METHOD meth2.
ch_attribute = 'X'.
ENDMETHOD.
ENDCLASS.
Would you prefer meth1 or meth2 or does one of the solutions even raise problems that I don't see? Of course, if a method changes lots of attributes it is impractical to use parameters, but besides that?
created by Tomas Buryanek in ABAP Development
Difference would be third method:
set_attribute IMPORTING i_value TYPE c.
...
METHOD set_attribute.
me->attribute = i_value.
ENDMETHOD.
...
lo=>set_attribute( 'A' ).
which is example of classic "setter" method.
For this topic check theory and usage of encapsulation OOP principle. It will help understand
07-04-2014 2:38 PM
So Tomas, do I get you right that you would set up a set_attribute method for each single attribute of a class (if it needs to be changed by methods of the class)?
You are right, I have to get a bit more familiar with the concepts, but this is a part of that 😉
07-04-2014 3:02 PM
I only put setters and getters on public attributes, if I need r/w or write access to them. For private, protected and read-only attributes, I use the attribute directly.
07-04-2014 3:12 PM
No, I realized your question is little bit different so I deleted my (little bit confusing) answer in old topic.
I would say that all 3 methods are ok, but each is for different usage...
07-14-2014 8:24 AM
Hm, if you are using get/set methods for an attribute anyway, why not make this attribute private? Your suggestion seems a little inconsistent to me, as a public attribute with associated get/set methods could still be changed directly, which is against the intention of get/set methods...
Just to recall, my question is about changing (not reading) attributes of a class by the class' own methods (not from outside).
07-14-2014 8:25 AM
Could you please explain your thoughts about the different usage in a bit more detail? 🙂
07-14-2014 9:57 AM
I can try describe methods by how i see them (method naming can help a lot)
meth1 => do_something( )
its like for example your method main( )...
meth2 => do_something_with( attribute )
if you have some logic which could be called multiple times with different attributes...
meth3 => set_attribute( value )
classic "setter", class with them can be very robust (which can be contraproductive), for example you can encapsulate validation of value inside...
07-14-2014 10:08 AM
Sorry, that was badly expressed. I should say
I use the attributes directly if they're read-only public, private or protected. Otherwise, I set them as private or protected, and use getters and setters. I do not use non-read-only public attributes.