Ensure a class only has one instance and provide a global point of access to it.
class some_class_name definition ... create private.
a. sole object is accessed directly through public static attribute holding its reference.
b. reference to sole object is provided as exporting parameter of static instance accessor method.
c. reference to sole object is provided as returning parameter of static instance accessor method.
1a - Caller accesses sole instance directly through public static attribute of class.
2b - Caller must provide its own instance reference variable to accept reference to sole instance.
2c - Caller may dispense with providing its own instance reference variable to accept reference to sole instance when using "chained method call" to access it.
3b - Same as 2b with lazy initialization.
3c - Same as 2c with lazy initialization.
report.
class registration_number_manager definition
abstract
final.
public section.
types : registration_number_type
type n length 05.
class-methods: get_next_registration_number
exporting
next_registration_number
type registration_number_manager=>registration_number_type.
private section.
class-data : next_registration_number
type registration_number_manager=>registration_number_type
value 1000.
endclass.
class registration_number_manager implementation.
method get_next_registration_number.
add 01 to registration_number_manager=>next_registration_number.
next_registration_number = registration_number_manager=>next_registration_number.
endmethod.
endclass.
class process_driver definition
abstract
final.
public section.
types : iteration_type type n length 02.
class-methods: drive_process
importing
iterations
type process_driver=>iteration_type.
endclass.
class process_driver implementation.
method drive_process.
data : registration_number
type registration_number_manager=>registration_number_type.
do iterations times.
call method registration_number_manager=>get_next_registration_number
importing
next_registration_number
= registration_number.
write: / 'Iteration :'
, sy-index
, / 'Registration number:'
, registration_number
.
enddo.
endmethod.
endclass.
parameters : iterate type process_driver=>iteration_type.
start-of-selection.
call method process_driver=>drive_process
exporting
iterations = iterate.
Caller accesses sole instance directly through public static attribute of class.
class-data : singleton type ref to registration_number_manager read-only.
class-methods: class_constructor.
method class_constructor.
create object registration_number_manager=>singleton.
endmethod.
class registration_number_manager definition
final
create private.
public section.
types : registration_number_type
type n length 05.
class-data : singleton type ref to registration_number_manager read-only.
class-methods: class_constructor.
methods : get_next_registration_number
exporting
next_registration_number
type registration_number_manager=>registration_number_type.
private section.
data : next_registration_number
type registration_number_manager=>registration_number_type
value 1000.
endclass.
class registration_number_manager implementation.
method class_constructor.
create object registration_number_manager=>singleton.
endmethod.
method get_next_registration_number.
add 01 to me->next_registration_number.
next_registration_number = me->next_registration_number.
endmethod.
endclass.
call method registration_number_manager=>get_next_registration_number
call method registration_number_manager=>singleton->get_next_registration_number
class-data : singleton type ref to process_driver read-only.
class-methods: class_constructor.
method class_constructor.
create object process_driver=>singleton.
endmethod.
class process_driver definition
final
create private.
public section.
types : iteration_type type n length 02.
class-data : singleton type ref to process_driver read-only.
class-methods: class_constructor.
methods : drive_process
importing
iterations
type process_driver=>iteration_type.
endclass.
class process_driver implementation.
method class_constructor.
create object process_driver=>singleton.
endmethod.
method drive_process.
data : registration_number
type registration_number_manager=>registration_number_type.
do iterations times.
call method registration_number_manager=>singleton->get_next_registration_number
importing
next_registration_number
= registration_number.
write: / 'Iteration :'
, sy-index
, / 'Registration number:'
, registration_number
.
enddo.
endmethod.
endclass.
call method process_driver=>drive_process
call method process_driver=>singleton->drive_process
call method process_driver=>singleton->drive_process
Yes, via the “create private” defined on the class definition statement.
Yes, the static attribute is defined with public visibility and is assigned the “read-only” qualifier so that external entities can access it but cannot change its value.
Yes, the static constructor method creates the singleton instance, placing its reference into the public static attribute implicitly available to external entities.
Caller must provide its own instance reference variable to accept reference to sole instance.
class-methods: class_constructor.
class-methods: get_instance
exporting
instance
type ref to registration_number_manager.
class-data : singleton type ref to registration_number_manager.
method class_constructor.
create object registration_number_manager=>singleton.
endmethod.
method get_instance.
instance = registration_number_manager=>singleton.
endmethod.
class registration_number_manager definition
final
create private.
public section.
types : registration_number_type
type n length 05.
class-methods: class_constructor.
class-methods: get_instance
exporting
instance
type ref to registration_number_manager.
methods : get_next_registration_number
exporting
next_registration_number
type registration_number_manager=>registration_number_type.
private section.
class-data : singleton type ref to registration_number_manager.
data : next_registration_number
type registration_number_manager=>registration_number_type
value 1000.
endclass.
class registration_number_manager implementation.
method class_constructor.
create object registration_number_manager=>singleton.
endmethod.
method get_next_registration_number.
add 01 to me->next_registration_number.
next_registration_number = me->next_registration_number.
endmethod.
method get_instance.
instance = registration_number_manager=>singleton.
endmethod.
endclass.
data : registration_number_manager
type ref to registration_number_manager.
call method registration_number_manager=>get_next_registration_number
importing
next_registration_number
= registration_number.
call method registration_number_manager=>get_instance
importing
instance = registration_number_manager.
call method registration_number_manager->get_next_registration_number
importing
next_registration_number
= registration_number.
Yes, via the “create private” defined on the class definition statement.
Yes, the static attribute is defined with private visibility making it inaccessible to external entities.
Yes, the static constructor method creates the singleton instance, placing its reference into the private static attribute unavailable to external entities, and public static accessor method “get_instance” makes this reference available to external entities via exporting parameter.
Caller may dispense with providing its own instance reference variable to accept reference to sole instance when using "chained method call" to access it.
exporting
instance
with
returning
value(instance)
class registration_number_manager definition
final
create private.
public section.
types : registration_number_type
type n length 05.
class-methods: class_constructor.
class-methods: get_instance
returning
value(instance)
type ref to registration_number_manager.
methods : get_next_registration_number
exporting
next_registration_number
type registration_number_manager=>registration_number_type.
private section.
class-data : singleton type ref to registration_number_manager.
data : next_registration_number
type registration_number_manager=>registration_number_type
value 1000.
endclass.
class registration_number_manager implementation.
method class_constructor.
create object registration_number_manager=>singleton.
endmethod.
method get_next_registration_number.
add 01 to me->next_registration_number.
next_registration_number = me->next_registration_number.
endmethod.
method get_instance.
instance = registration_number_manager=>singleton.
endmethod.
endclass.
call method registration_number_manager=>get_instance
importing
instance = registration_number_manager.
with the word "receiving", as in:
call method registration_number_manager=>get_instance
receiving
instance = registration_number_manager.
call method registration_number_manager=>get_instance
receiving
instance = registration_number_manager.
call method registration_number_manager->get_next_registration_number
importing
next_registration_number
= registration_number.
with the single statement
call method registration_number_manager=>get_instance( )->get_next_registration_number(
importing
next_registration_number
= registration_number
).
Yes, via the “create private” defined on the class definition statement.
Yes, the static attribute is defined with private visibility making it inaccessible to external entities.
Yes, the static constructor method creates the singleton instance, placing its reference into the private static attribute unavailable to external entities, and public static accessor method “get_instance” makes this reference available to external entities via returning parameter.
Caller must provide its own instance reference variable to accept reference to sole instance.
if registration_number_manager=>singleton is not bound.
create object registration_number_manager=>singleton.
endif.
class registration_number_manager definition
final
create private.
public section.
types : registration_number_type
type n length 05.
class-methods: get_instance
exporting
instance
type ref to registration_number_manager.
methods : get_next_registration_number
exporting
next_registration_number
type registration_number_manager=>registration_number_type.
private section.
class-data : singleton type ref to registration_number_manager.
data : next_registration_number
type registration_number_manager=>registration_number_type
value 1000.
endclass.
class registration_number_manager implementation.
method get_next_registration_number.
add 01 to me->next_registration_number.
next_registration_number = me->next_registration_number.
endmethod.
method get_instance.
if registration_number_manager=>singleton is not bound.
create object registration_number_manager=>singleton.
endif.
instance = registration_number_manager=>singleton.
endmethod.
endclass.
Yes, via the “create private” defined on the class definition statement.
Yes, the static attribute is defined with private visibility making it inaccessible to external entities.
Yes, the public static accessor method “get_instance” makes this reference available to external entities via exporting parameter, using lazy initialization to create an instance if the reference is not yet bound.
Caller may dispense with providing its own instance reference variable to accept reference to sole instance when using "chained method call" to access it.
if registration_number_manager=>singleton is not bound.
create object registration_number_manager=>singleton.
endif.
class registration_number_manager definition
final
create private.
public section.
types : registration_number_type
type n length 05.
class-methods: get_instance
returning
value(instance)
type ref to registration_number_manager.
methods : get_next_registration_number
exporting
next_registration_number
type registration_number_manager=>registration_number_type.
private section.
class-data : singleton type ref to registration_number_manager.
data : next_registration_number
type registration_number_manager=>registration_number_type
value 1000.
endclass.
class registration_number_manager implementation.
method get_next_registration_number.
add 01 to me->next_registration_number.
next_registration_number = me->next_registration_number.
endmethod.
method get_instance.
if registration_number_manager=>singleton is not bound.
create object registration_number_manager=>singleton.
endif.
instance = registration_number_manager=>singleton.
endmethod.
endclass.
Yes, via the “create private” defined on the class definition statement.
Yes, the static attribute is defined with private visibility making it inaccessible to external entities.
Yes, the public static accessor method “get_instance” makes this reference available to external entities via returning parameter, using lazy initialization to create an instance if the reference is not yet bound.
1a - Caller accesses sole instance directly through public static attribute of class.
2b - Caller must provide its own instance reference variable to accept reference to sole instance.
2c - Caller may dispense with providing its own instance reference variable to accept reference to sole instance when using "chained method call" to access it.
3b - Same as 2b with lazy initialization.
3c - Same as 2c with lazy initialization.
call method some_class=>get_instance( )->some_instance_method( ).
Ensure a class only has one instance and provide a global point of access to it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
4 | |
3 | |
3 | |
2 | |
2 | |
2 | |
1 | |
1 |