04-29-2015 4:15 PM
Hi Grus,
I have a very basic question but really make me headache...
Recently I am learning the design pattern in ABAP and as you know in JAVA there is a key word "Synchronized" to keep thread safe. But in ABAP, I didn't find any key words like that. So does that mean ABAP is always a single thread language? And I found that there is a way looks like "CALL FUNCTION Remotefunction STARTING NEW TASK Taskname DESTINATION dest" to make multi-thread works. As you can see it use the destination, so does that mean actually the function module is always executed in a remote system, and in every system, it is always single thread?
Could you help me on the question? Thanks a lot, grus
And here comes up to my mind another question...It's a little bit mad but I think it may works....What if I set every attribute and method as static in the singleton class...Since as you can see it is already a singleton so every attribute in it should be only one piece. So then I don't even need to implement a get_instance( ) method to return the instance. Just call "class_name=>some_method( )" directly then singleton is achieved...What do you think?
BR,
Steve
05-04-2015 6:39 AM
99.99% of the time ABAP program is equivalent to a single threaded Java VM. You can add parallelisation via CALL FUNCTION STARTING NEW TASK, but this creates a new process, and the only sharing is via the function's interface. You can share memory and have shared objects. With shared objects, safety is achieved by the lock mechanism.
Generally statics are less flexible then singletons. When I've used static instead of singletons in ABAP I've run quickly into design issues that are fixed by switching to singletons. For example, when generalising a class, I've had to switch the singleton to a factory - that's quite straightforward. Switching a class full of statics is rather more work. Nowadays, I always do the singleton from the start.
From an OO perspective, you might also consider that the consumer of the class shouldn't need to know how it has been implemented. You don't get that with an entirely static class.
04-29-2015 5:36 PM
For thread-safe answer you can check this: http://scn.sap.com/thread/1222643
Your question about "moving everything to static" is far from mad - there's a lot of discussion already made on the web about using a full static classes VS implementing the singleton pattern. This link - Difference between static class and singleton pattern? - Stack Overflow - is an example.
I believe the main reason people use singleton over static classes is that with singleton you can take advantage of everything an instance provides (passing the instance as parameter, implementing interfaces, using polymorphism, etc). I also think it's a little more clear how your program is handling memory when your're using singletons, as you must create / destroy the instance explicitly and in static methods/attributes/classes are loaded automatically at the first use and live throughout the program execution (I know you can always FREE static things... this is more of personal choice).
Finally, I'm not really an OO expert, so I hope some SCN members can contribute to this answer.
Best regards!
04-30-2015 7:53 PM
Hi Mauricio,
Thanks a lot for your answer. It's good to know that there are already some discussion on the Static class vs Singleton topics. And I have read the Thread safety part but it doesn't really helps answer my question. Hope some one could help on that one:)
BR,
Steve
04-30-2015 9:01 PM
Steve,
I've the same question, few days ago I tried to use the singleton in ABAP. In Java programming is possible to use the same reference in two sessions or programs, sharing attributes, methods and all data, but I could not do in ABAP.
In my test I created a program with one global class using the singleton pattern, so I expected that when I run my program and see the reference returned after the get_instance method it should be equal to the same program run in another session, but the ABAP will create a new reference and instantiate again.
So I deduced that the only way to share it between sessions in ABAP is using the ABAP Shared Memory Objects.
I can be wrong, but I think that ABAP use a thread by user session (Each window) and we can't change it.
Best regards.
05-04-2015 6:15 AM
Hi Fujii,
Thanks for your answer:).
I think you are right and I also got some answers from other topics that said ABAP actually is a single-thread programm language in each session. But I am a little bit curious that will this truth cause some inefficient cases since only one thread is allowed at a time...
BR,
Steve
05-04-2015 6:39 AM
99.99% of the time ABAP program is equivalent to a single threaded Java VM. You can add parallelisation via CALL FUNCTION STARTING NEW TASK, but this creates a new process, and the only sharing is via the function's interface. You can share memory and have shared objects. With shared objects, safety is achieved by the lock mechanism.
Generally statics are less flexible then singletons. When I've used static instead of singletons in ABAP I've run quickly into design issues that are fixed by switching to singletons. For example, when generalising a class, I've had to switch the singleton to a factory - that's quite straightforward. Switching a class full of statics is rather more work. Nowadays, I always do the singleton from the start.
From an OO perspective, you might also consider that the consumer of the class shouldn't need to know how it has been implemented. You don't get that with an entirely static class.
05-04-2015 11:29 AM
Hi Matthew,
Thanks a lot for your answer! It's really a clear and understandable answer for question#1. Now I get the main idea and how ABAP Programme Language works. And I will take some time to learn about shared memory. It should helps me to understand how to share something among sessions/processes.
And for question#2 I got a lot of opinions and all of them are correct from my thought. The main defect that a STATIC Class than Singleton is that it's not OO because it cannot support polymorphic and cannot be a parameter etc.
Thanks a lot, Grus. That's why I really love SCN so much! Always get issue solved!
BR,
Steve