Recently, we have seen how we can achieve OO Design Pattern Decorator in ABAP using the inheritance tree. Achieving decorator seems to be little bit complex, but it gets easier as we get used with it.
Problem
As you can notice, in this example that we had to use the helper variable LO_PRE to retain the object reference of the previous instantiated object.
Decorator using Helper
DATA: lo_decorator TYPE REF TO output,
lo_pre TYPE REF TO output. ” Helper Variable
* Setup objects
* standarad object
CREATE OBJECT lo_decorator TYPE alvoutput.
lo_pre = lo_decorator.
* testing Decorator
IF iv_pdf IS NOT INITIAL.
CREATE OBJECT lo_decorator TYPE op_pdf
EXPORTING
io_decorator = lo_pre.
lo_pre = lo_decorator. ” <<
ENDIF.
IF iv_email IS NOT INITIAL.
CREATE OBJECT lo_decorator TYPE op_email
EXPORTING
io_decorator = lo_pre.
lo_pre = lo_decorator. " <<
ENDIF.
[/abap_code]
If we don't use the helper variable and do the code like this - it seems fine - but it goes to endless loop.
[abap_code slider="X" title="Without using Helper variable"]
CREATE OBJECT:
lo_decorator TYPE alvoutput
lo_decorator TYPE op_pdf EXPORTING io_decorator = lo_decorator,
lo_decorator TYPE op_emain EXPORTING io_decorator = lo_decorator.
[/abap_code]
Why this happens
The endless loop is the result of the usage of LO_DECORATOR. Because when system executes this statement it carries the new object created in the IO_DECORATOR parameter. So, the object has the reference of its OWN object instead of the SUPER object. This happens because as soon as system executes the statement CREATE OBJECT it creates the object before calling the constructor. SO, when it reaches to constructor it has its OWN object reference instead of the SUPER object reference.
This is what SAP Help point out to:
The CREATE OBJECT statement creates an instance of a class or object and assigns the object reference to the reference variable oref. Directly after the object has been created, the instance constructor of the class is executed.
Original forum discussion can be found on SDN at Design Pattern, The decorator
Further Reading
Explore all other Design patterns. More are comming soon.
- ABAP Object Design Patterns – Singleton
- ABAP Objects Design Patterns – Model View Controller (MVC) Part 1
- ABAP Objects Design Patterns – Model View Controller (MVC) Part 2
- ABAP Objects Design Patterns – Model View Controller (MVC) Part 3
- ABAP Objects Design Patterns – Decorator
- ABAP Objects Design Patterns – Factory Method
- ABAP Objects Design Patterns – Observer
- Case Study: Observer Design Pattern Usage
- ABAP Objects Design Patterns – Abstract Factory
- OO Design Pattern Decorator – Why do we need to use helper variable?
- ABAP Objects Design Patterns – Facade
- ABAP Objects Design Patterns – Adapter
- ABAP Objects Design Patterns – Composite
- ABAP Objects Design Patterns – Iterator
- Iterator Design Pattern to access Linked List in ABAP Objects
- ABAP Objects Design Patterns – Proxy
- ABAP Objects Design Patterns – Prototype
- ABAP Objects Design Patterns – Singleton Factory
- ABAP Object Oriented Approach for Reports – Initial Design
- ABAP Object Oriented Approach for Reports – Redesign
- ABAP Objects Design Patterns – Builder
- ABAP Objects Design Patterns Singleton Usage
- ABAP MVC – Model View Controller Discussion
- Object Oriented Approach for Reports with multiple Datasource
- OO ABAP – Selection Object with Direct Access
- OO ABAP – Selection Criteria Object using RS_REFRESH_FROM_SELECT OPTIONS
- ABAP Factory Method Using SWITCH