Application Development and Automation 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: 
Read only

factory method which returns a refrence to the same class object

Former Member
0 Likes
1,809

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

1 ACCEPTED SOLUTION
Read only

Former Member
1,058

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.

3 REPLIES 3
Read only

Former Member
0 Likes
1,058

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.

Read only

Former Member
1,059

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.

Read only

Former Member
0 Likes
1,058

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