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: 

Global singleton class - public or protected/private instance generation?

Former Member
0 Kudos

Hello colleagues,

Recently, in abapdocu I read a guideline that describes a rule: "Declare the instance constructor in the public visibility section." with an exсeption: "The technical restrictions mentioned only apply to the processing of global classes.".

Okay, clear. But what about global singleton classes? Should we also use public section for constructors of such classes? I thought that protected or private instance generation should be defined for this pattern .

Thanks,

Ilya

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor
0 Kudos

I thought that protected or private instance generation should be defined for this pattern

Absolutely correct. If the instance creation is Public, then you can never impose the "singleton" behaviour!

I think you are confusing "instantiation level" with "visibility".

When defining a class as singleton i set the instantiation level to "Private/Protected" depending on the requirement & never to Public. You can still create the "Constructor" in the PUBLIC "visibility"

As of ABAP 7.0 EhP2 you can define the CONSTRUCTOR in all visibility areas. (Ref. ABAP Keyword Documentation).

BR,

Suhas

4 REPLIES 4

SuhaSaha
Advisor
Advisor
0 Kudos

I thought that protected or private instance generation should be defined for this pattern

Absolutely correct. If the instance creation is Public, then you can never impose the "singleton" behaviour!

I think you are confusing "instantiation level" with "visibility".

When defining a class as singleton i set the instantiation level to "Private/Protected" depending on the requirement & never to Public. You can still create the "Constructor" in the PUBLIC "visibility"

As of ABAP 7.0 EhP2 you can define the CONSTRUCTOR in all visibility areas. (Ref. ABAP Keyword Documentation).

BR,

Suhas

Former Member
0 Kudos

I'm confused, that guidelines contradict each other...

0 Kudos

Ilya Klopkov wrote:

I'm confused, that guidelines contradict each other...

I am not sure how they contradict? The guidelines refer to the "visibility section" of the CONSTRUCTOR.


Always declare the instance constructor of a global class in its public visibility section and independently of the instantiation specified by the CREATE addition in the class definition.

The commonly used CL_SALV_TABLE class, uses a factory to generate the instance. The instantiation level is pvt. => no external agent can create an instance of the class

But the CONSTRUCTOR is defined in the public visibility level. I think that the programming guideline is referring to this part.

The SAP guideline answers your question. It states & i quote -


With the CREATE PRIVATE addition, objects can only be created by the class itself. The restriction of the object creation to the class itself is useful in connection with the singleton design pattern, for example, where the class itself performs the object creation.

Refer: ABAP Keyword Documentation.

BR,

Suhas

PS - The agent class(CA* classes) of persistence classes are singleton. You can refer to their definition for further info.

Former Member
0 Kudos

Suhas Saha wrote:

Always declare the instance constructor of a global class in its public visibility section and independently of the instantiation specified by the CREATE addition in the class definition.

That explains everything . Seems like I understood it in a wrong way.

Thanks a lot!