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: 

Constants - To be declared in class or interface?

FabioPagoti
Active Contributor
0 Kudos

Hello OO gurus,

The place where I work is upgrading SAP version and now all developments must be done using OO concepts. I am already familiar with this concept but i have one doubt regarding its application in ABAP programs.

I transformed a existing procedural report into a object oriented report. I declared all the constants used in this program in a separated class as follows:


CLASS: cl_constants_handler DEFINITION.
  PUBLIC SECTION.
    CONSTANTS:
            c_h1(10)          TYPE c    VALUE   'Purch.Doc.',
            c_h2(09)          TYPE c    VALUE   'Doc. date',
" (...)

However i saw that constants are declared in interfaces in standards programs. Then, classes need to implement these interfaces to used them. Examples: IF_BCB_ITEM_CONSTANTS, IF_CRM_CONSTANT1, IF_UA_APPLICATION_CONSTANTS

My question is: what is the advantage to declare constants in interfaces instead of classes?

Thanks,

1 ACCEPTED SOLUTION

Former Member
0 Kudos

I know it is done in the standard, but personally I do not like the practice of using interfaces for constants. Coming from a Java background, I consider this an anti-pattern. The main idea of an interface is to expose the behavior of a class and hiding the implementation detail. Inventing an interface and throwing in constants without regard to the previous consideration makes me squirm.

If I absolutely have to have a place for global constants, I prefer an abstract class. An added advantage is that you can have table-type constants as read-only static variables that are initialized in the class constructor.

-- Sebastian

7 REPLIES 7

MarcinPciak
Active Contributor
0 Kudos

I think there is no big advantage in defining constants in an interface. All constants are address as static components so you can use any public constant of any class you want.

Here the interface just introduce new logical grouping. You can store all the constants in one place and call the interface some more meaningfully like i.e. "if_physics" than just addressing them from different classes.

Personally I prefer to use interfaces as a central point for types which I use accross several classes. This way I don't need to define them separately in each class, I just implement right interface.

So, in my opinion this is all about logical data grouping, to make the program more meaningful to other developer.

Regards

Marcin

0 Kudos

Hello Marcin!

First of all.. thanks a lot! We think in the same way ... in this example report, i also created a separated class for types. It is really helpful.

I don't know if i was clear enough, but i used a single class in this report too. I made a logical separation using a class instead of an interface, like 'lcl_physics'.

It's really good to know that i am in the right way... it would be great if SDN had a source of information about OO best pratices like that one.

0 Kudos

Hej Fabio,

Yes, I agree. If there was a such document, we could add this advise there

Cheers

Marcin

Former Member
0 Kudos

I know it is done in the standard, but personally I do not like the practice of using interfaces for constants. Coming from a Java background, I consider this an anti-pattern. The main idea of an interface is to expose the behavior of a class and hiding the implementation detail. Inventing an interface and throwing in constants without regard to the previous consideration makes me squirm.

If I absolutely have to have a place for global constants, I prefer an abstract class. An added advantage is that you can have table-type constants as read-only static variables that are initialized in the class constructor.

-- Sebastian

0 Kudos

that's a very good point of view Sebastian.

Do someone splitt the constants in different classes for increase logical separation?

0 Kudos

Halo Fabio,

In ABAP the problem with the interface is it makes the code unreadable .

I suggest that you have a seperate hierarchies for Model and GUI . All the classes should inherit from a parent Model and parent GUI class. All the GUI relted constants should be added in parent GUI class so that it appears and can be used in GUI sub classes.

Similarly all the Model related constants should be added in Model Parent class so that it gets inherited to the Model Sub classes

0 Kudos

the problem is there is no multiple inheritance in ABAP OO : a class has only one parent class

that's why we use interfaces to store constants: this way we just have to "implement" each interface with needed constants (I mean it is possible to have several interfaces in the same class, and so to get constants from several at the same time)