Application Development and Automation 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: 
Read only

singleton concept

Former Member
0 Likes
1,246

Hi

Please anybody explain me about singleton concept in abap objects,

Thanks n Regards

vijay

1 ACCEPTED SOLUTION
Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
785

Hi,

Singleton means having one instance and making sure that only that instance is used by all the programs or modules. This is generally acheived by making the class instatitation as Private and having an instance attribute in the class of its own type and having a method something like GET_INSTANCE which creates and returns the intance if the class if the instance is already not there.

Regards,

Sesh

Message was edited by:

Seshatalpasai Madala

3 REPLIES 3
Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
786

Hi,

Singleton means having one instance and making sure that only that instance is used by all the programs or modules. This is generally acheived by making the class instatitation as Private and having an instance attribute in the class of its own type and having a method something like GET_INSTANCE which creates and returns the intance if the class if the instance is already not there.

Regards,

Sesh

Message was edited by:

Seshatalpasai Madala

Read only

uwe_schieferstein
Active Contributor
0 Likes
785

Hello Vijay

The singleton concept is a general <b>design pattern</b> of object-orientation and not specific to ABAP Objects.

If you want to look at a concrete sample in ABAP objects have a look at the OO-based persistance services:

<a href="http://help.sap.com/saphelp_nw2004s/helpdata/en/fd/022008bc9311d4b2e80050dadfb92b/content.htm">Components of the Persistence Service</a>

The <b>class agent</b> is realised as singleton.

Regards

Uwe

Read only

dustyplanet
Active Participant
0 Likes
785

Just to elaborate on what Sesh mentioned, A Singleton class is one that can only be instantiated once during the runtime of a program. Typical examples would be Classes that provide services(Persistance), or classes that provide resource pools(BADI Adapters, Shared Connections, etc.)

Since normal classes can be instantiated any number of times,

CLASS lcl_normal DEFINITION.
  PUBLIC SECTION.
    DATA: v type i.
    METHODS: disp_v.
ENDCLASS.

CLASS lcl_normal IMPLEMENTATION.
  METHOD disp_v.
    WRITE:/ v.
  ENDMETHOD.
ENDCLASS.

*__Report Code__*

DATA: r1 TYPE REF TO lcl_normal,
          r2 TYPE REF TO lcl_normal.

CREATE OBJECT r1.
r1->v = 10.

CREATE OBJECT r2. "Possible because class is not singleton
r2->v = 20.

r1->disp_v( ).
r2->disp_v( ). " Also possible

If we have to redesign the above class to make it singleton, in other words, irrespective of how many times the program uses the class, only a single shared instance will be reused, we have to prevent the class from being reinstantiated, we start by preventing calls to the constructor from outside of the class, ie: Only the class itself can create it's own instance!!

CLASS lcl_singleton DEFINITION CREATE PRIVATE.

Next step, now that calling programs cannot "CREATE OBJECT" our class, we provide an alternative to this...

CLASS lcl_singleton DEFINITION CREATE PRIVATE.
   PUBLIC SECTION.
      CLASS-DATA: singleton TYPE REF TO lcl_singleton READ-ONLY.
      DATA: v type i.
      CLASS-METHODS: class_constructor.
      METHODS: disp_v.
ENDCLASS.

CLASS lcl_singleton IMPLEMENTATION.
  METHOD class_constructor.

     CREATE OBJECT singleton. "Can be executed only once

  ENDMETHOD.

  METHOD disp_v.

    WRITE:/ v.

  ENDMETHOD.
ENDCLASS.

*__Report Code__*
DATA: r1 TYPE REF TO lcl_singleton,
          r2 TYPE REF TO lcl_singleton.

*Create object r1 not possible!
r1 = lcl_singleton=>singleton. " The only way to retrieve an instance
r2 = lcl_singleton=>singleton.
r1->v = 10.
r1->disp_v( ).
r2->disp_v( ). "Will also print 10

And that's how it goes !

Regards,

Dushyant Shetty

P.S. Did not get the time to syntax-check, please check before using code !