Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

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

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

Tags:
replied

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.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question