ABAP Objects Design Patterns – Prototype

By | February 7, 2012 | ABAP Objects, OO Design Patterns | 9,941 | 2

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.

UML

Here is the UML for the demo:

Prototype Design Pattern UML

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.

Code Lines

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.

Do you think this would solve any of your existing problems?

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

2 Comments

  • fewtron

    excellent source.really never seen such type of explanatory tutorial,please keep it up,also can you please send me pdf version of sap press official books.thanks once again for nice tutorial.

  • Hello Fewtron,

    I’m glad that you like the content.

    In my opinion, it would be unprofessional to ask for copyright materials.

    Regards,
    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.