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: 

Use of Local Classes

Former Member
0 Kudos

Hi OO Gurus,

I have this nagging question about the local classes in ABAP if we recommend the use of them any more.

I have come through some discussions where it has been said that they are not recommended any more.

Can you please throw some light on use of local classes. Also when the local classes should be used and when global classes? Most of the times, I have seen people using global classes only.

Thanks in Advance,

Raveesh

1 ACCEPTED SOLUTION

MarcinPciak
Active Contributor
0 Kudos

So far I had only once a chance to use local clasess in real case. It was for a BSP project we were working on. I had controller class which received any user (http) request. As this controller was dedicated to do other stuff too, I was not very keen on encapsulating all request handling within private method's of this controller. Creating separate global classes would also mean they would be exposed to the world, which I wanted to avoid (due to tight coupling to project). So I decided to delegate this task to local class object. This allowed me to separate responsibility b/w controller and local controller's class. Each had its own unique task to do.

From OO design it has also one more advantage. I could easily implement several local classes and via composition use appropriate local class object based on view I used. So I could have several views handled by different local class objects which would share some common interface (but implement it in different manner). This interface would be all my client (controller) knows about local objects handling requests. So by means of strategy pattern I could dynamically decide which handler I want to use for which view.

To conclude. I believe I can live w/o local classes, but certainly there should be some balance b/w exposing parts of the application to world and inner complexity hidden behind either private methods or local classes.

Regards

Marcin

20 REPLIES 20

Former Member
0 Kudos

Also if possible, the reasons behind not using them.

Thanks,

Raveesh

0 Kudos

Hi Raveesh,

I am no expert in OOPS, but i guess you could draw an anology by comparing the above to Sub Routines and Functions modules at a high level.

So, if you have a local class the possibility of other users using this local class is diminished. And they might have to use the program/class which encompasses this local class to get to the local class.

I have come through some discussions where it has been said that they are not recommended any more.

I guess the above depends on the context, could you provide the links for these discussions.

More info on the below link

http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm

I will let the experts throw more light and may be correct me.

Regards,

Chen

0 Kudos

Hi Raveesh,

Local classes can be used. There is no standard recommendation for not using them.

However, local classes have the following shortcomings.

1. Local Classes are not 100% object oriented. They depend on some Program for their existence.

2. Enhancement of local classes is not supported to the extent it is supported in the case of global classes. e.g. if a local class method has no IMPORTING parameters, you can't add this by enhancement. You can always add any kind of additional optional parameters for Global Classes.

3. Reusability of Local Classes is questionable.

Based on the above facts, I'd recommend fellow developers to use Global Classes whenever possible.

Regards,

Abhinava

0 Kudos

Hi Abhinava,

1. Local Classes are not 100% object oriented. They depend on some Program for their existence.

My thoughts, local classes are as object oriented as their counter parts(global class). A global class too depends on some program for its use, the use of a class(in a program or a class) doesn't decide if it is object oriented or not. A class in SAP ABAP (any other language) irrespective of local/global has to adhere to the OOP principles, and if it does it is Object Oriented.

Regards,

Chen

JerryWang
Advisor
Advisor
0 Kudos

Hi Friend,

another important usage of local class is that it can be used as generation of ABAP unit test class. As a approach of test-driven development in ABAP, you can use the build in functionality in SE80 to generate local test class of your productive class, and then write unit testing code in those generated lobal test class. You can search "ABAP Unit" as key word in abap document.

Best Regards,

Jerry

MarcinPciak
Active Contributor
0 Kudos

So far I had only once a chance to use local clasess in real case. It was for a BSP project we were working on. I had controller class which received any user (http) request. As this controller was dedicated to do other stuff too, I was not very keen on encapsulating all request handling within private method's of this controller. Creating separate global classes would also mean they would be exposed to the world, which I wanted to avoid (due to tight coupling to project). So I decided to delegate this task to local class object. This allowed me to separate responsibility b/w controller and local controller's class. Each had its own unique task to do.

From OO design it has also one more advantage. I could easily implement several local classes and via composition use appropriate local class object based on view I used. So I could have several views handled by different local class objects which would share some common interface (but implement it in different manner). This interface would be all my client (controller) knows about local objects handling requests. So by means of strategy pattern I could dynamically decide which handler I want to use for which view.

To conclude. I believe I can live w/o local classes, but certainly there should be some balance b/w exposing parts of the application to world and inner complexity hidden behind either private methods or local classes.

Regards

Marcin

0 Kudos

Hello All,

Just to let you know as of Release 702 subroutines & FMs are marked as "[obsolete modularization|http://help.sap.com/abapdocu_702/en/abenobsolete_modularization.htm]" techniques!

This should put to an end all the confusion about not using local classes. I've been using local classes instead of subroutines since i read the above SAP documentation , lol.

All i can say is there is more to OO than creating them for modularization purposes. It provides the developers with many design patterns(as Marcin has highlighted in his reply), then there are object services(Persistent Objects, GOS etc.) & the list goes on ...

BR,

Suhas

matt
Active Contributor
0 Kudos

There's two types of local classes - those that are written in SE38 as part of, e.g. a report program - and those that are part of a class.

The former I use frequently - usually a local "main" class that just handles screen flow. The latter I've used from time to time, as Marcin, where I've needed something that's "other" than the global class, but is connected to it. Another approach for this scenario is to create a friend class, with private instantiation, and no factory method. Then only the friend class can instantiate it.

0 Kudos

Hi Everyone,

Thanks for your valuable replies and time. I am really sorry that I am replying late. I was busy with some office stuff.

What I could understand with your replies is that local classes can be used in below situations.

1. To Modularize a report program, or encapsulate some functionality inside a global class as a local class under the section 'Local Types'. Also ABAP unit test class is always local.

2. If the class we are creating will be used only within our program and no where else, then it is better to create a local class and not global class.(Please correct me if I am wrong).

And the Global class should be created only if we want to use it globally. If we are using it locally in our program itself, then it better to use local class.(Please correct me if I am wrong).

Also provide some guidance for the use of global classes as well. When to create global class and local class?

Thanks once again for your valuable time and guidance.

Raveesh

0 Kudos

Hi,

Also ABAP unit test class is always local

Not 100% true, it may inherit a global AUnit test class (since 7.0).

And the Global class should be created only if we want to use it globally.

I like to define a local class in its own include program, to be able to reuse it very quickly in my own test programs when starting projects at new clients (as creating a global class takes a little more time than creating everything at one place; of course I could use SAPLink/equivalent tools to create it but it's not so fast).

Note that a global class can be generated automatically from a local class through SE24 menu (though not 100% can be extracted if I remember well).

Global classes are required if you have to refer to them from some ABAP Dictionary objects, and they are also often used with recent automatically-generated code (shared objects, web services, web dynpros...)

Sandra

0 Kudos

Note that a global class can be generated automatically from a local class through SE24 menu (though not 100% can be extracted if I remember well).

As a proof for Sandra's words please refer [An easy way to create global class hierarchies|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/13402] [original link is broken];. As a matter of fact, for me this is faster way of creating global classes as I have all local my source classes in one spot, which is quite handy from design perspective.

Regards

Marcin

0 Kudos

Hi Everyone,

Thanks a lot for your valuable replies and excellect insights.

As the conclusion, I will try to summarize what I learnt from you guys.

1. Local Classes can be used for modularization purposes, or for encapsulating some independent functioanlity within a class as local class.

2. Also local classes can be used if we know that the class which we are creating will be used by us only and there is no resue expected for it. In real application scenarios, it is not practical to to think that we will use the class only in one program and no where else. For example, if we create a sales order class for some SD applcation, then it will ceratainly be used in multiple places being such an important class. So it's better to create a global class for sales order even if we are using it just once. May be after 2-3 years, we want to add a new functionality, and we can use the same old class.

3. Also local classes are useful for demo or test purposes, since we might need classes just once. Also as Sandra pointed that for moving the classes from one system to other, it is better to use local classes, since we just need to copy the program containing the local class in the other system.

I will close the thread after one day. If I have understood something wrong, then please add your comments.

Thanks & Warm Regards,

Raveesh

0 Kudos

I have one comment.

This is very nice approach you took. First you ask the question, then waiting for replies, you sum up what you have learned and understood. Finally you don't forget about appreciation. For me this is model thread:) Keep going this way.

Thanks and regards

Marcin

0 Kudos

Hi,

You forgot a few remarks above: from 7.02, as Suhas said, SAP recommends to use them (or global classes) instead of forms/function modules, and they are better suited for use of control framework (I don't see why we should create a global class, but this is explained in your point 2). A few things can't be achieved using local classes as shared objects...

raveesh saurabh wrote

> 1. Local Classes can be used for modularization purposes, or for encapsulating some independent functioanlity within a class as local class.

This point is not needed: it is not specific to local classes, it applies to global classes too. I think point 2 explains it.

Sandra

PS: I agree with Marcin

matt
Active Contributor
0 Kudos

My practice is to use local classes only in small, report-like applications. And occasionally in global classes, when I need something small as an object. For everything else, especially when it's modelling an object that could be reused, then I use global classes.

For large applications, say using the MVC pattern, I'll use global classes simply because they're easier to handle than local ones.

The trick is, as far as possible, to make your classes possible to reuse. As soon as you use local classes, you lose that opportunity. In the years I've been developing OO, I've reused classes maybe four years after I first wrote them, which is always cool.

Edited by: Matt on Jul 1, 2011 10:10 PM

Former Member
0 Kudos

Hi Marcin, Sandra

SDN is a place where best people of the trade helps you selflessly even though they don't know you. I really am thankful for it.

@ Sandra: Regarding your point that even global classes can be used for modularization: Technically it is possible to do so, but is it logical to create one more repository object just for the sake of modularizing your program or class? I have a feeling that it is better to modularize using local classes rather than global classes since we are creating a repository object just to arrange our code, and that's why I had added this point to use local classes for modularization.

Please let me know if I am missing something, or my understanding is flawed.

Thanks once again for your help.

Warm Regards,

Raveesh

Former Member
0 Kudos

Hi All,

I am closing the thread. Only point left is whether it is wise to use the global classes for modularizing your program.I think it is not wise to use global classes just for the sake of modularization of the program. I hope that some expert throws some light into it.

Thanks & Warm Regards,

Raveesh

matt
Active Contributor
0 Kudos

Hi All,

>

> I am closing the thread. Only point left is whether it is wise to use the global classes for modularizing your program.I think it is not wise to use global classes just for the sake of modularization of the program. I hope that some expert throws some light into it.

>

> Thanks & Warm Regards,

> Raveesh

I agree. Global classes when you're modelling an object, and it could have a meaningful existence outside your application. For example, I've written a class for handling fileuploads and downloads... because I discovered I was writing the same code again and again.

0 Kudos

A bit late response to this thread. Hope this one will add some value to the discussion.

Many times I used the local class definition within a report program to handle the EVENTS raised from the global class. Eg. In a report to display an ALV grid using SALV class, a local class method used to handle the DOUBLE_CLICK event of CL_SALV_EVENTS_TABLE.

class lcl_handle_events definition.
  public section.
    methods:
      on_double_click for event double_click of cl_salv_events_table
        importing row column.
endclass.

Regards, Vinod

0 Kudos

I'm also bit late in replying but will give my 2 cents.

I would suggest not to create Business Local Classes in the global class or in the program if possible. While designing the local class, we may not realize that it may be used in future. More over if you have local class declared in the global class, they are not available in the inherited classes of the global class. So, ultimately, you would think why did you create that local class, which you need in the inherited class.

Local classes in the program are fine if they are event handlers for some ALVs as they wont be "alive" outside the application. But if you have a global class which act as a wrapper to generate the ALV, you should create a private event handler method in the same class.

Along with other uses, Local classes are also used in enhancing the global classes.


CLASS LCL_ZTEST_NP_123 DEFINITION.
PUBLIC SECTION.
CLASS-DATA OBJ TYPE REF TO LCL_ZTEST_NP_123.
DATA CORE_OBJECT TYPE REF TO ZCL_TEST_NP .
 INTERFACES IPR_ZTEST_NP_123.
  METHODS:
   CONSTRUCTOR IMPORTING CORE_OBJECT
     TYPE REF TO ZCL_TEST_NP OPTIONAL.
ENDCLASS.
CLASS LCL_ZTEST_NP_123 IMPLEMENTATION.
METHOD CONSTRUCTOR.
  ME->CORE_OBJECT = CORE_OBJECT.
ENDMETHOD.

METHOD IPR_ZTEST_NP_123~M_1.
*"------------------------------------------------------------------------*
*" Declaration of PRE-method, do not insert any comments here please!
*"
*"methods M_1 .
*"------------------------------------------------------------------------*

ENDMETHOD.
ENDCLASS.

Regards,

Naimesh Patel