<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to implement Strategy pattern in ABAP Objects? in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-implement-strategy-pattern-in-abap-objects/m-p/7873495#M1593993</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a problem where I need to implement different algorithms, depending on the type of input. Example: I have to calculate a Present Value, sometimes with payments in advance, sometimes payment in arrear. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;From documentation and to enhance my ABAP Objects skills, I would like to implement the strategy pattern. It sounds the right solution for the problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hence I need some help in implementing this pattern in OO. I have some basic OO skills, but still learning.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Has somebody already implemented this pattern in ABAP OO and can give me some input. Or is there any documentation how to implement it?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks and regards,&lt;/P&gt;&lt;P&gt;Tapio&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 23 May 2011 07:59:31 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2011-05-23T07:59:31Z</dc:date>
    <item>
      <title>How to implement Strategy pattern in ABAP Objects?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-implement-strategy-pattern-in-abap-objects/m-p/7873495#M1593993</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a problem where I need to implement different algorithms, depending on the type of input. Example: I have to calculate a Present Value, sometimes with payments in advance, sometimes payment in arrear. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;From documentation and to enhance my ABAP Objects skills, I would like to implement the strategy pattern. It sounds the right solution for the problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hence I need some help in implementing this pattern in OO. I have some basic OO skills, but still learning.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Has somebody already implemented this pattern in ABAP OO and can give me some input. Or is there any documentation how to implement it?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks and regards,&lt;/P&gt;&lt;P&gt;Tapio&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 23 May 2011 07:59:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-implement-strategy-pattern-in-abap-objects/m-p/7873495#M1593993</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-05-23T07:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement Strategy pattern in ABAP Objects?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-implement-strategy-pattern-in-abap-objects/m-p/7873496#M1593994</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am not a high skilled OO ABAP guy but still i can give you some hints. You can design an interface which holds your method for calculation. Create classes for different algorithms and implement the interface in it.Create a reference to the interface and based on the type of input create the object and call the method ( generaly known as polymorphism ).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Guys I am just going through the desing pattern documents suggested by Marcin &lt;SPAN __jive_emoticon_name="wink"&gt;&lt;/SPAN&gt; and if there is any mistake then correct me.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 23 May 2011 08:52:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-implement-strategy-pattern-in-abap-objects/m-p/7873496#M1593994</guid>
      <dc:creator>kesavadas_thekkillath</dc:creator>
      <dc:date>2011-05-23T08:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement Strategy pattern in ABAP Objects?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-implement-strategy-pattern-in-abap-objects/m-p/7873497#M1593995</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Keshav has already outlined required logic, so let me fulfill his answer with a snippet&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;An Interface&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
INTERFACE lif_payment.
  METHODS pay CHANGING c_val TYPE p.
ENDINTERFACE.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Payment implementations&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
CLASS lcl_payment_1 DEFINITION.
  PUBLIC SECTION.
  INTERFACES lif_payment.
  ALIASES pay for lif_payment~pay.
ENDCLASS.                  


CLASS lcl_payment_2 DEFINITION.
  PUBLIC SECTION.
  INTERFACES lif_payment.
  ALIASES pay for lif_payment~pay.
ENDCLASS.                    


CLASS lcl_payment_1 IMPLEMENTATION.
  METHOD pay.
    "do something with c_val i.e.
    c_val = c_val - 10.
  ENDMETHOD.                    
ENDCLASS.                   


CLASS lcl_payment_2 IMPLEMENTATION.
  METHOD pay.
    "do something else with c_val i.e.
    c_val = c_val + 10.
  ENDMETHOD.   
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Main class&lt;/STRONG&gt; which uses &lt;EM&gt;strategy pattern&lt;/EM&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
CLASS lcl_main DEFINITION.
  PUBLIC SECTION.
    "during main object creation you pass which payment you want to use for this object
    METHODS constructor IMPORTING ir_payment TYPE REF TO lif_payment.
    "later on you can change this dynamicaly
    METHODS set_payment IMPORTING ir_payment TYPE REF TO lif_payment.
    METHODS show_payment_val.
    METHODS pay.

  PRIVATE SECTION.
    DATA payment_value TYPE p.
    "reference to your interface whcih you will be working with
    "polimorphically
    DATA mr_payment TYPE REF TO lif_payment.
ENDCLASS.                   

CLASS lcl_main IMPLEMENTATION.
  METHOD constructor.
    IF ir_payment IS BOUND.
      me-&amp;gt;mr_payment = ir_payment.
    ENDIF.
  ENDMETHOD.                   
  METHOD set_payment.
    IF ir_payment IS BOUND.
      me-&amp;gt;mr_payment = ir_payment.
    ENDIF.
  ENDMETHOD.                   
  METHOD show_payment_val.
    WRITE /: 'Payment value is now ', me-&amp;gt;payment_value.
  ENDMETHOD.                   
  "hide fact that you are using composition to access pay method
  METHOD pay.
    mr_payment-&amp;gt;pay( CHANGING c_val = payment_value ).
  ENDMETHOD.                   ENDCLASS.                   &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Client application&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
PARAMETERS pa_pay TYPE c. "1 - first payment, 2 - second

DATA gr_main TYPE REF TO lcl_main.
DATA gr_payment TYPE REF TO lif_payment.

START-OF-SELECTION.
  "client application (which uses stategy pattern)

  CASE pa_pay.
    WHEN 1.
      "create first type of payment
      CREATE OBJECT gr_payment TYPE lcl_payment_1.
    WHEN 2.
      "create second type of payment
      CREATE OBJECT gr_payment TYPE lcl_payment_2.
  ENDCASE.
  "pass payment type to main object
  CREATE OBJECT gr_main
    EXPORTING
      ir_payment = gr_payment.

  gr_main-&amp;gt;show_payment_val( ).
  "now client doesn't know which object it is working with
  gr_main-&amp;gt;pay( ).

  gr_main-&amp;gt;show_payment_val( ).

  "you can also use set_payment method to set payment type dynamically
  "client would see no change
  if pa_pay = 1.
    "now create different payment to set it dynamically
    CREATE OBJECT gr_payment TYPE lcl_payment_2.

    gr_main-&amp;gt;set_payment( gr_payment ).
    gr_main-&amp;gt;pay( ).
    gr_main-&amp;gt;show_payment_val( ).
  endif.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Regads&lt;/P&gt;&lt;P&gt;Marcin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 23 May 2011 12:22:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-implement-strategy-pattern-in-abap-objects/m-p/7873497#M1593995</guid>
      <dc:creator>MarcinPciak</dc:creator>
      <dc:date>2011-05-23T12:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement Strategy pattern in ABAP Objects?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-implement-strategy-pattern-in-abap-objects/m-p/7873498#M1593996</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Marcin,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;this was really helpful! Thanks a lot.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Tapio&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 23 May 2011 12:29:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-to-implement-strategy-pattern-in-abap-objects/m-p/7873498#M1593996</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-05-23T12:29:53Z</dc:date>
    </item>
  </channel>
</rss>

