---
title: Working with the ABAP Prototype Design Pattern
description: When it comes to cloning items in an ABAP system, you might use an ABAP prototype design pattern to accomplish the copying. Learn how to do it here.
---

[Learn SAP from the Experts | The SAP PRESS Blog](https://blog.sap-press.com)

# [Working with the ABAP Prototype Design Pattern](https://blog.sap-press.com/working-with-the-abap-prototype-design-pattern)

 Written by [SAP PRESS](https://blog.sap-press.com/author/sap-press) | Feb 3, 2020 4:00:00 PM

When it comes to cloning items in an [ABAP](https://learning.sap-press.com/abap) system, you might use a prototype design pattern to accomplish the copying.

 

In this example, we will work on an imaginary purchase requisition application where the user has to enter the vendor code (LIFNR), material number (MATNR), unit price (PREIS and WAERS), and quantity (MENGE and MEINS) into the GUI. We will want it to be possible to enter multiple line items.

 

## Brand-New Instances

Below demonstrates what the model class of such an application would roughly look like.

 

CLASS zcl\_purchase DEFINITION

PUBLIC FINAL CREATE PUBLIC.

 

PUBLIC SECTION.

“ Some type definitions

 

METHODS add\_line IMPORTING !is\_line TYPE t\_line.

METHODS load\_from\_db IMPORTING !iv\_docno TYPE zdocno.

METHODS save\_to\_db.

 

PRIVATE SECTION.

“ Some hidden variables

“ Some boring methods

PROTECTED SECTION.

ENDCLASS.

 

CLASS zcl\_purchase IMPLEMENTATION.

 

METHOD add\_line.

APPEND VALUE #(

obj\_material = NEW zcl\_material( is\_line-matnr )

lifnr = is\_line-lifnr

matnr = is\_line-matnr

price = VALUE #(

preis = is\_line-preis

waers = is\_line-waers )

quan = VALUE #(

menge = is\_line-menge

meins = is\_line-meins )

) TO gt\_line.

ENDMETHOD.

 

METHOD load\_from\_db.

“ Some boring code containing SELECT, etc.

ENDMETHOD.

 

METHOD save\_to\_db.

“ Some boring code containing MODIFY, etc.

ENDMETHOD.

 

ENDCLASS.

 

So far, so good. As you see, GT\_LINE is a nested internal table with five fields: one object (**OBJ\_MATERIAL**); two variables (**LIFNR**, **MATNR**); and two work areas (**PRICE**, **QUAN**). At this point, we will assume that creating a new **CL\_MATERIAL** has a high runtime cost, which means that **CL\_MATERIAL~CONSTRUCTOR** contains costly code snippets that take quite some time to execute.

 

Below demonstrates what the **CL\_MATERIAL** class looks like.

 

CLASS zcl\_material DEFINITION

PUBLIC FINAL CREATE PUBLIC.

 

PUBLIC SECTION.

“ Some type definitions

 

METHODS constructor IMPORTING !iv\_matnr TYPE matnr.

“ Some further methods

 

PRIVATE SECTION.

“ Some hidden methods

PROTECTED SECTION.

ENDCLASS.

 

CLASS zcl\_material IMPLEMENTATION.

 

METHOD constructor.

“ Some very slow but unavoidable code

ENDMETHOD.

 

“ Some more methods

ENDCLASS.

 

If the user enters a new line item containing a brand new material, we must call **ADD\_LINE** again. Thus, because we will be creating a new instance of **CL\_MATERIAL** we will have to endure high runtime cost. On the other hand, what if the user enters a material number that he/she had entered before? Would we still create a brand new instance of **CL\_MATERIAL**? With our current code, we would. So we must ask ourselves, “Is there a better way?”

 

Luckily there is! What we will do is clone an existing material object instead of creating a new one—the core logic of the prototype design pattern.

 

## Item Clone

Let’s look at the Unified Modeling Language (UML) diagram below to see what this should look like.

 

 

The UML diagram has only one extra method: **CLONE**. This method will do exactly what it says: clone the object. That’s the easy part; the relatively tricky part will transpire in **CL\_PURCHASE**. Let’s start simple and see what **CL\_MATERIAL** looks like below.

 

CLASS zcl\_material DEFINITION

PUBLIC FINAL CREATE PUBLIC.

 

PUBLIC SECTION.

“ Some type definitions

 

METHODS constructor IMPORTING !iv\_matnr TYPE matnr.

METHODS clone RETURNING VALUE(ro\_material) TYPE REF TO zcl\_material.

“ Some further methods

 

PRIVATE SECTION.

“ Some hidden methods

PROTECTED SECTION.

ENDCLASS.

 

CLASS zcl\_material IMPLEMENTATION.

 

METHOD constructor.

“ Some very slow but unavoidable code

ENDMETHOD.

 

METHOD clone.

SYSTEM-CALL OBJMGR CLONE me TO ro\_material.

ENDMETHOD.

 

“ Some more methods

ENDCLASS.

 

Normally, within the **CLONE** method, we would create the object **RO\_MATERIAL** (avoiding the slow code in the constructor using flag variables and conditions) and copy the entire state of **ME** to **RO\_MATERIAL**. However, we used a cheat code. What you see is a system call to clone ME, which is not meant for external use. No one gets hurt by little hacks like this, as long as the unit tests are done well.

 

Now, back to **CL\_PURCHASE**. Instead of creating a new **CL\_MATERIAL** object every time **ADD\_LINE** is called, we will clone an existing material object whenever possible. The code to do so can be seen below.

 

METHOD add\_line.

DATA lo\_material TYPE REF TO zcl\_material.

 

ASSIGN gt\_line\[ matnr = is\_line-matnr \]

TO FIELD-SYMBOL(<ls\_line>).

IF sy-subrc EQ 0.

lo\_material = <ls\_line>-obj\_material->clone( ).

ELSE.

lo\_material = NEW zcl\_material( is\_line-matnr ).

ENDIF.

 

APPEND VALUE #(

obj\_material = lo\_material

lifnr = is\_line-lifnr

matnr = is\_line-matnr

price = VALUE #(

preis = is\_line-preis

waers = is\_line-waers )

quan = VALUE #(

menge = is\_line-menge

meins = is\_line-meins )

) TO gt\_line.

ENDMETHOD.

 

## Conclusion

Brilliant, isn’t it? Instead of re-calling the constructor every time the same material is added, we simply cloned an existing object and avoided the initialization runtime cost. This trick to using prototype design patterns in your ABAP programs should save you quite a bit of time.

 

## Learn ABAP with Rheinwerk Courses!

Ready to level up your ABAP skills? Explore the full range of our ABAP-focused online courses, from foundational programming to advanced RAP, CDS, AMDP, unit testing, and more. Each one is instructor-led (live and on-demand), includes recordings and slide decks, and is designed to give you practical take-away skills you can apply right away in your SAP landscape. Click on the banner below to get started!

 

<https://www.sap-press.com/online-courses/topics/on-demand-courses/#programming>

 

*Editor’s note:* This post has been adapted from a section of the book *[Design Patterns in ABAP Objects](https://www.sap-press.com/design-patterns-in-abap-objects_4277/?utm_source=sappressblog&utm_medium=referral&utm_campaign=Blogs&utm_term=chapter7&utm_content=1464)* by Kerem Koseoglu.

[View full post](https://blog.sap-press.com/working-with-the-abap-prototype-design-pattern)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "SAP PRESS"
  },
  "dateModified" : "2026-03-12T13:54:06.955Z",
  "datePublished" : "2020-02-03T16:00:00Z",
  "headline" : "Working with the ABAP Prototype Design Pattern",
  "image" : {
    "@type" : "ImageObject",
    "height" : 473,
    "url" : "https://cdn2.hubspot.net/hubfs/5707200/Working%20with%20the%20ABAP%20Prototype%20Design%20Pattern.png",
    "width" : 1440
  },
  "mainEntityOfPage" : "https://blog.sap-press.com/working-with-the-abap-prototype-design-pattern",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60.0,
      "url" : "https://cdn2.hubspot.net/hubfs/5707200/rheinwerk-sappress-logo.png",
      "width" : 312.23303
    },
    "name" : "Blog"
  }
}
```