Prototype design pattern allows the flexibility to create itself – cloning. Lets check out, how to achieve this in ABAP objects.
What is Prototype
You have instantiated an object, called some operations. Your object is now in a state from which you wish to copy itself. In other words, you want to clone the object. This wont be achieved if you just assign the object reference to another variable declared with the similar type as these are object references not the actual data. So, What you need to do is instantiate a new object and copy all the attributes.
To achieve the prototype, you need to wrap the logic of instantiating the object and copying the attributes in a method. Client who needs to have a clone, will call the prototype’s clone method. Clients don’t need to worry about whenever any change happens to the attributes. The clone method would be responsible to set the required attributes to the cloned object.
Here is the UML for the demo:
Components involved in the UML:
- LCL_REPORT_DATA: This class declares a method which can be called by clients to clone a concrete object. This is called Prototype.
- LCL_DETAIL_REPORT_DATA: This class is inherited from prototype class LCL_REPORT_DATA. This class implements the method CLONE in order to clone itself. It is responsible for setting up all the attributes of cloned object.
- LCL_MAIN – This class acts as a client and asks the prototype to clone itself.
Check out the implementation of the Prototype design pattern in ABAP:
Prototype (Clone) Design Pattern implementation in ABAP
PROGRAM znp_dp_prototype. * CLASS lcl_report_data DEFINITION ABSTRACT. PUBLIC SECTION. METHODS: clone ABSTRACT RETURNING value(ro_object) TYPE REF TO lcl_report_data. METHODS: select_data ABSTRACT. ENDCLASS. "lcl_report_data DEFINITION * CLASS lcl_detail_report_data DEFINITION INHERITING FROM lcl_report_data. PUBLIC SECTION. METHODS: clone REDEFINITION. METHODS: select_data REDEFINITION. DATA: t_data TYPE STANDARD TABLE OF t100. ENDCLASS. "lcl_detail_report_data DEFINITION * CLASS lcl_detail_report_data IMPLEMENTATION. METHOD select_data. SELECT * FROM t100 INTO TABLE t_data UP TO 20 ROWS WHERE sprsl = sy-langu. ENDMETHOD. "select_Data METHOD clone. * instantiate a new object * Declaring a temp variable helps to set all the attributes * by its name and allows to call the methods of the subclass * itself as RO_OBJECT is defined wrt to Super class DATA: lo_object TYPE REF TO lcl_detail_report_data. CREATE OBJECT lo_object. * list down all the attributes which needs to be copied over lo_object->t_data = me->t_data. * Set it to return object ro_object = lo_object. ENDMETHOD. "clone ENDCLASS. "lcl_detail_report_data IMPLEMENTATION * CLASS lcl_main DEFINITION. PUBLIC SECTION. CLASS-METHODS: run. ENDCLASS. "lcl_main DEFINITION * CLASS lcl_main IMPLEMENTATION. METHOD run. DATA: lo_report TYPE REF TO lcl_report_data. CREATE OBJECT lo_report TYPE lcl_detail_report_data. lo_report->select_data( ). DATA: lo_rep_2 TYPE REF TO lcl_report_data. lo_rep_2 = lo_report->clone( ). ENDMETHOD. "run ENDCLASS. "lcl_main IMPLEMENTATION * START-OF-SELECTION. lcl_main=>run( ).
You may have noticed that, I have declared a local variable named LO_OBJECT with reference to more specific type of class – LCL_DETAIL_REPORT_DATA. This allows us the flexibility to access methods and attributes specific to the class itself.
Design Time Consideration
You need to consider these points while achieving Prototype:
- Create an Abstract Super Class with an abstract method CLONE( )
- Implement the method CLONE( ) in the inherited concrete class
- Instantiate the object
- Set all the attributes using own attributes
You need to also consider the “depth” of the cloning. As object would have another objects as attributes, you need to pay extra attention while directly referring them or cloning them as well.
In upcoming Post, I’ll try to cover the Case Study on real-time prototype usage.
Check out all Design Patterns
You may also want to explore all other Design Patterns in OO ABAP.
- 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
Do you think this would solve any of your existing problems?