‎2016 Aug 29 11:52 AM
Hello,
I am working on creation of a factory pattern. The "factory" method inside the class will instantiate the sub classes based on the importing parameter. The subclasses redefine an abstract method in the super class and the same is being called in my program. It is fine till this stage. The problem that I am facing is that am not able to implement a separate method for the subclass. When I try to call the method ( Exclusive for the sub class not inherited from the super class) , I get the error message - "The method is unknown, protected or private" . There is no syntax error while I try to access a message inherited from the super class. Can you please share some thoughts?
Thank you.
Regards,
Prem
‎2016 Aug 29 9:00 PM
Post your code snippet so that others can have a better understanding of your problem.
If i understand correctly you're trying to call the method of the subclass using the reference variable whose static type is that of the superclass
cl_factory=>get_instance(...)->sub_class_method(...)
‎2016 Aug 29 9:00 PM
Post your code snippet so that others can have a better understanding of your problem.
If i understand correctly you're trying to call the method of the subclass using the reference variable whose static type is that of the superclass
cl_factory=>get_instance(...)->sub_class_method(...)
‎2016 Aug 30 3:49 AM
Hello Suhas,
Thanks for your reply. Please find the code snippet below.
go_val = ycl_factory=>factory( exporting iv_type = 'SO').
if go_val is bound.
go_val->calculate( exporting iv_customers = ip_sales
receiving ep_customers = gt_sales ) .
go_val->calculate_sub ().
endif.
endif.
The above code is fine till the calculate method which is inherited from the super class . The calculate_sub is a method that is available only in the child class which is instantiated by go_val.
and that is giving a syntax error.
‎2016 Aug 30 7:09 AM
Hi Prem,
you first need to cast your go_val to the type of the subclass. Something like this:
data: sub_class type ref to cl_sub_class.
sub_class ?= ycl_factory=>factory( ...).
sub_class->calculate_sub( ).
However, this code looks like you are having a design issue. Usually it should not be necessary to call a specific method of the subclass. Instead you should stick with the interface provided by your factory.
Christian
‎2016 Sep 02 11:01 AM
Thanks Christian.
Can you give some example working with the interface ?
‎2016 Sep 05 10:20 AM
‎2016 Sep 05 11:08 AM
Hi Prem,
it's hard to give an example as you question is quite abstract. However, I'd try to implement you framework in such a way that you don't need to know the specific subclass to invoke the required method. This can easily be achieved using an interface:
data: my_example type ref to zif_example_interface.
my_example = zcl_factory=>get_implementation( ...).
my_example->execute_if_method( ).
You define a interface (zif_example_interface) that has the necessary methods (execute_if_method). Your subclasses implement this interface and the specific functionality in the concrete interface implementation.
The factory now return a reference to the interface (the concrete implementation depends on the parameters). You now simply invoke the interface methods and don't need care about the concrete implementation returned by the factory.
Hope this helps.
Christian