Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Regarding OOABAP

0 Kudos

What is Factory Design Pattern ? What is the real time example of factory design pattern in terms of SAP?

1 ACCEPTED SOLUTION

mateuszadamus
Active Contributor

Hi varun1995

Here's about the pattern: https://en.wikipedia.org/wiki/Factory_method_pattern

Here's an example for ABAP: https://blogs.sap.com/2012/02/20/factory-pattern-in-abap-oo/

The example is complex, with configuration based on DB table, but it can also be as simple as

METHOD create_instance.
  IF iv_value = 'A'.
    CREATE OBJECT ro_instance TYPE lcl_a.
  ELSE.
    CREATE OBJECT ro_instance TYPE lcl_default.
  ENDIF.
ENDMETHOD.

Kind regards,

Mateusz

3 REPLIES 3

mateuszadamus
Active Contributor

Hi varun1995

Here's about the pattern: https://en.wikipedia.org/wiki/Factory_method_pattern

Here's an example for ABAP: https://blogs.sap.com/2012/02/20/factory-pattern-in-abap-oo/

The example is complex, with configuration based on DB table, but it can also be as simple as

METHOD create_instance.
  IF iv_value = 'A'.
    CREATE OBJECT ro_instance TYPE lcl_a.
  ELSE.
    CREATE OBJECT ro_instance TYPE lcl_default.
  ENDIF.
ENDMETHOD.

Kind regards,

Mateusz

former_member1716
Active Contributor

Hello varun1995,

The factory method is most used for creating objects without any leverage to its instantiation technique. Interfaces and encapsulation concepts of OOABAP plays a vital role here.

You can follow the blog below which discusses the method in detail.

https://blogs.sap.com/2012/02/20/factory-pattern-in-abap-oo/#:~:text=The%20factory%2Dpattern%20is%20....

Regards!