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: 

what is singleton class and abt friends parameters purpose in class

Former Member
0 Kudos

Hi gurus,

Can any one of you pls expalain me what is singleton class and abt friends parameters purpose in class?

3 REPLIES 3

former_member199581
Active Participant
0 Kudos

The singleton principle is applied to those classes with Private Creation.

This means that only the class (and its subclasses) are able to create instances of the class itself.

It's intended to use with those classes which allow only one instance.

For example:


CLASS C1 DEFINITION FINAL CREATE PRIVATE.  "Private Creation

 CLASS-DATA: myself TYPE REF TO c1 READ-ONLY.

 CLASS-METHODS: class_constructor.

ENDCLASS.

CLASS C1 IMPLEMENTATION.

 METHOD class_constructor.

  CREATE OBJECT myself.

 ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

 DATA: classObject TYPE REF TO c1. 

"-> Syntax Error: you're not able to create instances of C1 outside the class
 CREATE OBJECT classObject.
"-> Singleton: accessing attribute myself triggers class_constructor, that creates
"-> the instance of C1 internally.
 classObject = c1=>myself.

"->You cannot create multiple instances of C1, accordingly to the Singleton
"->principle

The Friend Tab allows you to declare friends of your class. Declaring a friend allows the class to access friend's protected methods/attributes without the need to inherit them.

Refer also to the R/3 Help on classes, it is explained well.

Hope this helps,

Roby.

0 Kudos

Some classes don't allow you to use CREATE to instantiate - instead they supply a static (factory) method, which returns an object reference to you. Most classes that do this are not singleton classes. But singleton classes are a subset of this type of class.

With a singleton class, the factory method will always return the same object reference.

This is typically used, for example, to return an instance to an object that references a database. There's only one database, so there's only on instance needed.

Roberto's code above is a perfect example of a singleton class.

matt

0 Kudos

Hi,

Have a look to this link => http://en.wikipedia.org/wiki/Singleton_pattern

Regards

Thomas