Home ABAP Tutorials ABAP Objects Singleton Classes
Singleton Classes PDF Print E-mail
User Rating: / 8
PoorBest 
ABAP Tutorials - Object Oriented ABAP
Written by admin   
Thursday, 24 July 2008 16:35

Often, there is a requirement that you would want to create only one instance of the class. To put it in other words, you do not want any one to create instance of the class if already created. This is a common requirement in many application designs. So how do we achieve this ?

This can be achieved using the singleton pattern. In this design pattern, we will declare the constructor as a 'Private' one. Private constructor !! Is that possible?

Yes, it is.

Go to the properties tab of class builder (SE24) and change the instantiation type to private. Then you can create a private constructor. The advantage of having a private constructor is that now no one can create an object of your class.. not even you Wink

So how do we create the instance of this class ? We create a static method GET_INSTANCE that will be used to create the instance. A static method because this method can be called directly with the class reference (without an object). Inside this method, we can create the object (because the constructor is a private method, we can call it within class). 

Now, how to make sure that only one instance is created? Simple.

Store the own reference as an attribute of the class. In the method GET_INSTANCE check if this attribute is populated. If it populated then return the same instance. If not, create a new one, store it in the own attribute and return the instance.

An example implementation of GET_INSTANCE is shown here. Lets say the name of the attribute is GREF_OBJ.

IF gref_obj IS INITIAL.

CREATE OBJECT gref_obj TYPE (class name).

ENDIF.

RETURN gref_obj.

An example how to create the instance of the class is shown here:

lref_obj = (class_name)=>get_instance_of().

Comments
Add New Search
archana  - need answer-doubt   |address59.93.29.142 |2009-01-24 12:46:16
can you please help me in finding the answer of this question?it would be a
great help for me...

SE24
If you have a "Singleton Pattern", you
must ensure that only one object can be
created from a cl_singleton class.
What mechanisms must you avail of here?

-The singleton class must have a
class method implemented in which the
CREATE OBJECT call is programmed for
this one object.




-The singleton class must have the addition CREATE
PRIVATE in the definition
part.

-In the singleton class, there must be an
even defined that is triggered when the



first and only object is
created
and also prevents further objects of this class from being created.


-The singleton class must have an instance method implemented in which the

CREATE OBJECT call is programmed for this one object.

-The CREATE OBJECT
call for this one object can take place in the class
constructor of the
single...
mastram  - Singleton Class   |Publisher |2009-01-24 16:17:27
Hello Archana,
The simplest and most commonly used mechanism is:

a) Make the
constructor private. To do this go to the properties tab of your class in SE24
and set instantiation type as Private.

b) Declare a class attribute called
GREF_OBJ TYPE REF TO CL_SINGLETON_CLASS

b) Define a public static method named
GET_INSTANCE( ) - this will have a returning parameter rref_object of TYPE your
CL_SINGLETON_CLASS

In this method, check if GREF_OBJ is initial, then create an
object of the class and store it in GREF_OBJ.
Then return the RREF_OBJ =
GREF_OBJ.

Points to note:
a) Since constructor is private, no one from outside
the class can instantiate this class.

b) By providing a public static method
GET_INSTANCE you provide the outside world to get the instance of this class

c)
Since before returning the object from GET_INSTANCE you always check if the
object is initial, you ensure that only one object is created

I hope it was
Anonymous   |address59.96.42.12 |2010-04-23 07:31:43
*------------------------------------------------- -------*
* CLASS
lcl_singleton DEFINITION
*--------------------------------------
------------------*
CLASS lcl_singleton DEFINITION CREATE PRIVATE.
PUBLIC
SECTION.
"--------------
CLASS-METHODS: class_constructor.

CLASS-METHODS: get_singleton RETURNING value(re_single)
TYPE REF TO
lcl_singleton.
PRIVATE SECTION.
"----------------
CLASS-DATA:
r_single TYPE REF TO lcl_singleton.
ENDCLASS. "lcl_singleton
DEFINITION






*-------------------------------- --------------------------*
*
CLASS lcl_singleton IMPLEMENTATION
*----------------------------------
------------------------*
CLASS lcl_singleton IMPLEMENTATION.
METHOD
class_constructor.
CREATE OBJECT r_single.
ENDMETHOD.
"class_constructor




METHOD get_singleton.
re_single =
r_single.
WRITE: 'object created'.
ENDMETHOD. "get_singleton
ENDCLASS.
prakahsha mb  - single ton class   |address59.96.42.12 |2010-04-23 07:47:03
*------------------------------------------------- -------*
* CLASS
lcl_singleton DEFINITION
*--------------------------------------
------------------*
CLASS lcl_singleton DEFINITION CREATE PRIVATE.


public
SECTION.
"--------------
CLASS-METHODS: class_constructor.

CLASS-METHODS: get_singleton RETURNING value(re_single)
TYPE REF TO
lcl_singleton.
methods: m1.
PRIVATE SECTION.
"----------------

CLASS-DATA: r_single TYPE REF TO lcl_singleton.
methods:
constructor.

ENDCLASS. "lcl_singleton
DEFINITION






*-------------------------------- --------------------------*
*
CLASS lcl_singleton IMPLEMENTATION
*----------------------------------
------------------------*
CLASS lcl_singleton IMPLEMENTATION.
METHOD
class_constructor.
CREATE OBJECT r_single.
ENDMETHOD.
"class_constructor

METHOD constructor.
WRITE 'i am a constructor'.

ENDMETHOD. ...
prakahsha mb  - re: single ton class   |address59.96.42.12 |2010-04-23 07:50:03
METHOD get_singleton.
re_single = r_single.

ENDMETHOD.
"get_singleton




METHOD m1.
NEW-LINE.
write 'Called me through
object'.
ENDMETHOD. "m1


ENDCLASS. "lcl_singleton
IMPLEMENTATION





*** Main Program;
*** creating the singleton via
class-constructor
DATA: r_single TYPE REF TO
lcl_singleton.



START-OF-SELECTION.
*########### #############
r_single =
lcl_singleton=>get_singleton( ).

call METHOD r_single->m1.
*** repeat this
statement and watch the execution in the Debugger !
prakahsha mb   |address59.96.42.12 |2010-04-23 07:51:10
copy the code and execute u wil get some idea
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Thursday, 21 August 2008 14:44 )
 

Advertisement

 

Google Search

Advertisement