‎2014 Sep 30 10:34 AM
Dear Experts,
Can you pls. give some clues on this issue.
How can I create an abstract class with its own factory method which returns a refrence to himself.
example given
data: go_descr type ref to cl_abap_structdescr,
gs_comp type abap_compdescr.
go_descr ?= cl_abap_typedescr=>describe_by_data( gs ). "returns refrence to CL_ABAP_TYPEDESCR
Kind Regards
Hakan
‎2014 Sep 30 2:00 PM
The factory method can return an instance of the abstract factory class but because the class is abstract, the instance has to be the type of a sub class. When calling the factory method it calls the static constructor method. In the static constructor method, you can instantiate the sub class you require. (Use a parameter in the factory method to make it more dynamic). The factory method then returns the instance created by the static constructor method.
class abstract_factory definition abstract. "Abstract factory class
public section.
class-methods: class_constructor,
factory returning value(ro_abstract_factory) type ref to abstract_factory.
class-data go_abstract_factory type ref to abstract_factory. "static reference to itself
methods my_abstract_method abstract.
endclass.
class car definition inheriting from abstract_factory. "factory sub class car
public section.
methods: my_abstract_method redefinition.
endclass.
class scooter definition inheriting from abstract_factory. "factory sub class scooter
public section.
methods: my_abstract_method redefinition.
endclass.
class car implementation.
method my_abstract_method.
write: / 'This is the Car''s Absract Method'.
endmethod.
endclass.
class scooter implementation.
method my_abstract_method.
write: / 'This is the Scooter''s Abstract Method'.
endmethod.
endclass.
class abstract_factory implementation.
method factory.
ro_abstract_factory = go_abstract_factory. "return the static instance
endmethod.
method class_constructor.
go_abstract_factory = new car( ). "use scooter if required
endmethod.
endclass.
start-of-selection.
data(go_vehicle) = abstract_factory=>factory( ).
go_vehicle->my_abstract_method( ). "can be type car or type scooter
return.
‎2014 Sep 30 11:13 AM
You already posted the solution to your problem. Or did not?
When factories are used, usually the constructor is private (protected). Inside your factory method you use the CREATE OBJECT to create the exporting / returning object sent as parameter from the factory.
‎2014 Sep 30 2:00 PM
The factory method can return an instance of the abstract factory class but because the class is abstract, the instance has to be the type of a sub class. When calling the factory method it calls the static constructor method. In the static constructor method, you can instantiate the sub class you require. (Use a parameter in the factory method to make it more dynamic). The factory method then returns the instance created by the static constructor method.
class abstract_factory definition abstract. "Abstract factory class
public section.
class-methods: class_constructor,
factory returning value(ro_abstract_factory) type ref to abstract_factory.
class-data go_abstract_factory type ref to abstract_factory. "static reference to itself
methods my_abstract_method abstract.
endclass.
class car definition inheriting from abstract_factory. "factory sub class car
public section.
methods: my_abstract_method redefinition.
endclass.
class scooter definition inheriting from abstract_factory. "factory sub class scooter
public section.
methods: my_abstract_method redefinition.
endclass.
class car implementation.
method my_abstract_method.
write: / 'This is the Car''s Absract Method'.
endmethod.
endclass.
class scooter implementation.
method my_abstract_method.
write: / 'This is the Scooter''s Abstract Method'.
endmethod.
endclass.
class abstract_factory implementation.
method factory.
ro_abstract_factory = go_abstract_factory. "return the static instance
endmethod.
method class_constructor.
go_abstract_factory = new car( ). "use scooter if required
endmethod.
endclass.
start-of-selection.
data(go_vehicle) = abstract_factory=>factory( ).
go_vehicle->my_abstract_method( ). "can be type car or type scooter
return.
‎2014 Sep 30 3:18 PM
Hi Eric,
thank you very much for kind help.
I had found something similar here. It is useful as well.
ABAP Objects Design Patterns - Factory Method | ABAP Help Blog
Regards
Hakan