2011 Feb 05 5:27 PM
I'm trying to pick up ABAP on my own by reading through various resources I've collected and writing my own programs. Unfortunately I don't have an experienced ABAP guru to ask questions of. I'm hoping someone can help me with the following:
Based on my other programming experience I know that it is very common for object attributes to be defined as private and then accessor/mutator (getter/setter) methods to be defined to provide access to the attribute.
In my study I noted that ABAP allows attributes to be made public but declared to be read-only. This would seem to permit a structure where accessor/getter methods are not needed (the attribute can be read externally) and one would only have to define a mutator/setter method.
My question: what is typically done in 'real-world' programs? Is there any reason why using read-only would be more or less preferred?
Thanks for any help that can be provided.
2011 Feb 06 10:11 PM
Hello Tony,
a public attribute gives up encapsulation for ease of use. Read only attributes gives access control back, but a subclass still cannot redefine the getter (e.g. for validation, tracking... purposes).
Remember [Tell don't ask|http://www.pragprog.com/articles/tell-dont-ask]? Even getters breaks encapsulation, so IMHO real-world usage is a trade-off and I will not consider typical usage but only propose two worthwhile contexts for public read only attributes:
- the class interface is not [published|http://martinfowler.com/ieeeSoftware/published.pdf] (PDF) or
- immutable [value objects|http://devlicio.us/blogs/casey/archive/2009/02/13/ddd-entities-and-value-objects.aspx], e.g. attribute is setup once and never changed.
regards,
J.N.N.
I'm trying to pick up ABAP on my own by reading through various resources I've collected and writing my own programs. Unfortunately I don't have an experienced ABAP guru to ask questions of. I'm hoping someone can help me with the following:
Based on my other programming experience I know that it is very common for object attributes to be defined as private and then accessor/mutator (getter/setter) methods to be defined to provide access to the attribute.
In my study I noted that ABAP allows attributes to be made public but declared to be read-only. This would seem to permit a structure where accessor/getter methods are not needed (the attribute can be read externally) and one would only have to define a mutator/setter method.
My question: what is typically done in 'real-world' programs? Is there any reason why using read-only would be more or less preferred?
Thanks for any help that can be provided.
2011 Feb 06 4:18 PM
Hello Tony
Below I have listed a few examples from standard ABAP classes. I cannot say whether you find more public read-only attributes than GETTER methods - you will find examples for both of them.
I could imagine that before it was possible to use the functional method call in ABAP that public read-only attributes were an alternative to the "verbose" method cal (see below)l.
" RE-FX: Contract
CL_RECN_CONTRACT-IF_RECN_CONTRACT~MS_DETAIL " public read-only attribute
" MM: Material
CL_IBASE_R3_MATERIAL->GET_MARA " GETTER method
"FI/CO: Company Code
CL_REEXC_COMPANY_CODE=>GET_DETAIL " GETTER method
" HR: Employee
CL_PT_EMPLOYEE->IF_PT_EMPLOYEE~PERNR " public read-only attribute
" Assumption: CL_IBASE_R3_MATERIAL would have
" a public read-only attribute MS_DETAIL (of TYPE mara)
" Normal method call (verbose):
CALL METHOD go_material->GET_MARA
RECEIVING
R_MARA = ls_mara.
" Functional method call:
ls_mara = go_material->get_mara( ).
" Accessing public attribute
ls_mara = go_material->ms_detail.
Regards
Uwe
2011 Feb 06 10:11 PM
Hello Tony,
a public attribute gives up encapsulation for ease of use. Read only attributes gives access control back, but a subclass still cannot redefine the getter (e.g. for validation, tracking... purposes).
Remember [Tell don't ask|http://www.pragprog.com/articles/tell-dont-ask]? Even getters breaks encapsulation, so IMHO real-world usage is a trade-off and I will not consider typical usage but only propose two worthwhile contexts for public read only attributes:
- the class interface is not [published|http://martinfowler.com/ieeeSoftware/published.pdf] (PDF) or
- immutable [value objects|http://devlicio.us/blogs/casey/archive/2009/02/13/ddd-entities-and-value-objects.aspx], e.g. attribute is setup once and never changed.
regards,
J.N.N.
2011 Feb 08 10:51 AM
I default to setters and getters. I break that when it's clearly overkill. I don't think I've ever used read-only public attributes - either I give full access or none.
2011 Feb 22 1:48 PM
I just think of public read-only attributes as private attributes with getters written for me. If you get in the habit of using these instead of private attributes with public getters, you simply write less code!
There is no drawback I know of except it may styalistically clash with the other way.
2011 Feb 23 2:31 PM
My 2 cents:
- It is cumbersome to use the GETTER methods till release 702 EhP2, as we can't use the chaining statement. We need to use the helper variables to temporarily store the values and pass it to next method.
- If you care for performance, it is better to use READ-ONLY attribute compare to GETTER methods.
You can read more at [READ-ONLY attribute vs GETTER methods|http://help-abap.blogspot.com/2011/02/read-only-attribute-vs-getter-methods.html]
Regards,
Naimesh Patel
2011 Feb 23 2:34 PM
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |