‎2010 Aug 20 6:44 AM
Guys,
i am not sure if i'm perceiving the singleton concept correctly.Please help me in understanding.
If i'm not wrong, singleton concept is to make a class have only one object(instance).
and the way you define a class is "CLASS lcl_a DEFINITION CREATE PRIVATE FINAL."
i've done the same but i'm able to create more than one instance.
CLASS lcl_a DEFINITION DEFERRED.
DATA: o_a TYPE REF TO lcl_a,
o_b TYPE REF TO lcl_a.
----
CLASS lcl_a DEFINITION
----
*
----
CLASS lcl_a DEFINITION CREATE PRIVATE FINAL.
PUBLIC SECTION.
CLASS-METHODS: m_sta.
METHODs: m_ins.
ENDCLASS. "lcl_a DEFINITION
----
CLASS lcl_a IMPLEMENTATION
----
*
----
CLASS lcl_a IMPLEMENTATION.
METHOD m_sta.
CREATE OBJECT o_a.
CREATE OBJECT o_b.
ENDMETHOD. "class_constuctor
METHOD m_ins.
WRITE: / 'i am the instance method.'.
ENDMETHOD.
ENDCLASS. "lcl_a IMPLEMENTATION
*CLASS lcl_b DEFINITION.
*ENDCLASS.
*
*CLASS lcl_b IMPLEMENTATION.
*ENDCLASS.
START-OF-SELECTION.
CALL METHOD lcl_a=>m_sta.
o_a->m_ins( ).
o_b->m_ins( ).
how does singleton concept make sense?
‎2010 Aug 20 6:54 AM
‎2010 Aug 20 7:18 AM
lavanya,
thanks for the quick reply, but, this was not i was looking for.
in your METHOD get_apps_instance. if you have not specified that IF CONDITION you can create another instance right?so, how does the singleton concept make sense?
IF lo_apps IS INITIAL.
CREATE OBJECT lo_apps.
ro_apps = lo_apps.
ENDIF.
or is it that singleton concept is, you CAN create multiple objects.but you have to restrict it to only one?
tHANKS,
zID.
‎2010 Aug 20 7:47 AM
hi,
As developer/owner of the CLASS you can create as many instances as you want( inside the methods of the CLASS), but others who are using your CLASS cannot create multiple instances, they have to call static methods of your CLASS to get object instance. So it is up to you to restrict the no.of instances of your CLASS.
‎2010 Aug 20 8:02 AM
‎2010 Aug 20 8:19 AM
Phani,
Thanks for the reply.
But, i still can't digest the justification of this pattern.
it's just that instead of using CREATE OBJECT, one has to just call the static method to create the instance.
the definition of SINGLEton classes should rather have been like... "you can create as many instances as possible,but, only inside the class" or "you can restrict the user from creating the objects using CREATE OBJECT"
i should say the definition is completely unjustified provided what you are saying is correct.
anyway
Thanks,
Zid.
‎2010 Aug 20 8:35 AM
‎2010 Aug 20 9:21 AM
hi Zid,
we use singleton concept if only one instance of a CLASS has to created and all actions has to be performed only on that instance. if user calls a static method multiple times to get the instance , you will first check if object is created or not, if its not created then you will create a new one and save it in class variable and return the same instance reference very time the method is called. So for outside users of the class its a singleton bcoz the same instance is returned from method. But inside the class there is no restriction that only one instance can be created inside methods of the same class. Good practice is we create one instance and use it in all methods.
-Phani
‎2010 Aug 20 7:32 PM
from the [SingletonPattern|http://www.c2.com/cgi/wiki?SingletonPattern] page:
A Singleton is the combination of two essential properties:
1. Ensure a class only has one instance
2. Provide a global point of access to it.
ABAP does not have built-in support for singletons, so you must enforce the one instance only rule with a private constructor and provide a global access point with a class method like get_instance( ).
hope this helps,
J.N.N
‎2010 Aug 23 10:47 AM
I think this can be solved by using Static Constructor instead of using Static Method. which executed only once in life time of a class.
‎2010 Aug 23 3:34 PM
Change your method:
METHOD m_sta.
IF o_a IS NOT BOUND.
CREATE OBJECT o_a.
ENDIF.
ENDMETHOD. "class_constuctorThen, no matter what you do, you'll only have one instance of your class.
The way I normally do it however is to define a static public method (factory) that returns a reference of TYPE REF TO myclass, and have a static attribute that is TYPE REF TO myclass.
METHOD factory. " Note this is static public method
IF myclass=>my_object IS NOT BOUND.
CREATE OBJECT myclass=>my_object.
ENDIF.
r_obj = myclass=>my_object.
ENDMETHOD.I use it like this:
DATA lr_obj TYPE REF TO myclass.
lr_obj = myclass=>factory( ).
lr_obj->do_stuff( ). " Do_stuff being an instance method of myclass matt
‎2010 Aug 23 9:43 PM
I think this can be solved by using Static Constructor instead of using Static Method. which executed only once in life time of a class.
Yep, I would support vidyasagar's suggestion. I'm generally using singleton patterns with help of the CLASS_CONSTRUCTOR, too. This ensures that only one instance of an object exists.
Regards,
Alej
‎2010 Aug 24 8:27 AM
The reason to use a factory method, rather than the class constructor, is that instantiation as soon as the class is touched in anyway, isn't always desirable. For example, if there are other static methods that don't use an instance, and instantiation has a significant overhead.
matt
‎2010 Aug 24 8:41 AM
Hi Matt,
Got your point on CLASS_CONSTRUCTOR v/s FACTORY method.
Just FACTORY method is used in classes with private instantiation. I don't think using this method makes any sense with public instantiation. Your comments ?
BR,
Suhas
‎2010 Aug 24 11:04 AM
If you have a factory method in a class with public instantiation, then that simply allows two different ways of creating instances. I can't think of a reason why you'd want to do this; it does seem to go rather against OOP principles.
matt
‎2010 Aug 24 10:40 PM
Hello Matt,
I think the decision between CLASS_CONSTRUCTOR and a factory pattern should depend on the usage of the class.
If I have a tool class (e.g. for handling an ALV) I would favor the class constructor, but if I have a object representing e.g. a business object (like orders, notifications, etc.) I would use a factory pattern to allow only one instance per business object.
I would also recommend to avoid the use of static methods, because they can't be redefined.
Regards,
Alej
‎2010 Aug 25 6:50 AM
>
> I would also recommend to avoid the use of static methods, because they can't be redefined.
>
> Regards,
> Alej
I'd put it a bit stronger than that. When I've found that I've needed to redefine a static method, it's meant I've got the design wrong.
‎2010 Aug 25 10:15 AM
guys...
sorry for the late reply...
so...what i can infer from this thread is that...
It's not that you can not create multiple instances using singleton concepts...it's just that you should restrict the user from creating multiple...may it be using static constructor...or using the factory method ..or a static method (with an IF condition)....
or is it, as someone said, that SAP has not adopted the singleton concept properly? i do not know how it works in java though...
Thanks guys,
Zid.
‎2010 Aug 25 11:52 AM
I've just encountered another scenario where you wouldn't want to use a CLASS-CONSTRUCTOR: When the code you want to execute needs to raise an exception to be trapped by the caller.
>
> guys...
>
> sorry for the late reply...
>
> so...what i can infer from this thread is that...
> It's not that you can not create multiple instances using singleton concepts...it's just that you should restrict the user from creating multiple...may it be using static constructor...or using the factory method ..or a static method (with an IF condition)....
>
> or is it, as someone said, that SAP has not adopted the singleton concept properly? i do not know how it works in java though...
>
> Thanks guys,
> Zid.
The singleton concept is a [design pattern|http://en.wikipedia.org/wiki/Design_pattern_(computer_science)]. If you need to ensure that only ever one instance exists, then you implement your class using the singleton pattern. It's not something that's built into a language. In Java, you have to implement it yourself. There's no concept of defining a class as a singleton within the code. So, it's nothing to do with SAP "not adopting it properly."
[From the wikipedia article:|http://en.wikipedia.org/wiki/Singleton_pattern]
The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made protected (not private, because reuse and unit test could need to access the constructor). Note the distinction between a simple static instance of a class and a singleton: although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed.
The article shows that the laziest implementation is that using a factory method.
‎2010 Aug 27 12:05 AM
It's not something that's built into a language.
Patterns might be [supported by the language|http://www.c2.com/cgi/wiki?AreDesignPatternsMissingLanguageFeatures], e.g. JavaScript and [Scala|http://en.wikipedia.org/wiki/Singleton_pattern#Scala] have native support for singletons.
A word of caution: [SingletonsAreGood|http://www.c2.com/cgi/wiki?SingletonsAreGood] and [SingletonsAreEvil|http://www.c2.com/cgi/wiki?SingletonsAreEvil] !
J.N.N.
‎2010 Aug 27 8:58 AM
In what way does Javascript support singletons that ABAP Objects doesn't?
Edit: ok, it is built in.
The point is that design patterns are intended to be ways of programming, not programming constructs themselves. If I say "I implemented this as a singleton", then any informed programmer knows what I mean. Regardless of their language preference.
The idea that ABAP Objects is somehow deficient because it isn't built in, seems a little odd to me. Some languages build some things in and not others. You might as well say Java is deficient because it doesn't natively support reading from databases.
Edited by: Matt on Aug 27, 2010 10:00 AM
‎2010 Aug 27 2:28 PM
One more to add where CLASS CONSTRUCTOR is not good for the Singleton : If you are using ABAP Test class (as part of TDD), CLASS CONSTRUCTOR is not a good place to achieve the Singleton Object. You should use the Factory method to instantiate the Singleton Object.
Regards,
Naimesh Patel