{"id":1122,"date":"2014-10-14T20:02:41","date_gmt":"2014-10-14T19:02:41","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1122"},"modified":"2014-10-14T20:08:42","modified_gmt":"2014-10-14T19:08:42","slug":"design-patterns-in-abap-factory-method","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1122","title":{"rendered":"Design Patterns in ABAP &#8211; Factory Method"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\"size-full wp-image-358 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" alt=\"SAP\" width=\"44\" height=\"50\" \/>Factory Method design pattern provides unique interface to object instantiation. The decision on which object is to be instantiated is hidden from the outside world in the code. Using this approach different objects can be instantiated dynamically.<!--more--><\/p>\n<p>Requirements for implementing Factory method:<\/p>\n<ul>\n<li>Base ABSTRACT class with implemented Factory Method having at least one importing parameter to decide which sub-class to be instantiate<\/li>\n<li>Sub-classes inheriting the base abstract class<\/li>\n<\/ul>\n<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_02.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1127\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=1127\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_02.png\" data-orig-size=\"456,335\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Factory Method class diagram\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_02.png\" class=\"size-medium wp-image-1127 aligncenter\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_02-300x220.png\" alt=\"Factory Method class diagram\" width=\"300\" height=\"220\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_02-300x220.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_02-408x300.png 408w, https:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_02.png 456w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Definition of the base abstract class with factory method GET_INSTANCE<\/h2>\n<pre lang=\"abap\">CLASS lcl_output DEFINITION ABSTRACT.\r\n  PUBLIC SECTION.\r\n    CLASS-METHODS:\r\n    get_instance\r\n      IMPORTING iv_type TYPE char4\r\n      RETURNING value(ro_result) TYPE REF TO lcl_output.\r\n    METHODS:\r\n      output ABSTRACT.\r\nENDCLASS.<\/pre>\n<h2>Subclass LCL_<span style=\"color: #ff0000;\">PDF<\/span>_OUTPUT inheriting the base abstract class<\/h2>\n<pre>CLASS lcl_pdf_output DEFINITION INHERITING FROM lcl_output.\r\n  PUBLIC SECTION.\r\n    METHODS: output REDEFINITION.\r\nENDCLASS.\r\n\r\nCLASS lcl_pdf_output IMPLEMENTATION.\r\n  METHOD output.\r\n    WRITE: \/ 'Printing PDF document'.\r\n  ENDMETHOD.\r\nENDCLASS.<\/pre>\n<h2>Subclass LCL_<span style=\"color: #ff0000;\">PNG<\/span>_OUTPUT inheriting the base abstract class<\/h2>\n<pre>CLASS lcl_png_output DEFINITION INHERITING FROM lcl_output.\r\n  PUBLIC SECTION.\r\n    METHODS: output REDEFINITION.\r\nENDCLASS.\r\n\r\nCLASS lcl_png_output IMPLEMENTATION.\r\n  METHOD output.\r\n    WRITE: \/ 'Generating PNG Image'.\r\n  ENDMETHOD.\r\nENDCLASS.<\/pre>\n<h2>Implementation of the base abstract class and its factory method<\/h2>\n<pre>CLASS lcl_output IMPLEMENTATION.\r\n  METHOD get_instance.\r\n    CASE iv_type.\r\n      WHEN 'PDF'.\r\n        CREATE OBJECT ro_result TYPE lcl_pdf_output.\r\n      WHEN 'PNG'.\r\n        CREATE OBJECT ro_result TYPE lcl_png_output.\r\n      WHEN OTHERS.\r\n***     ERROR\r\n    ENDCASE.\r\n  ENDMETHOD.\r\nENDCLASS.<\/pre>\n<h2>Testing report<\/h2>\n<pre>START-OF-SELECTION.\r\n  DATA:\r\n    lo_output TYPE REF TO lcl_output.\r\n  \r\n  lo_output = lcl_output=&gt;get_instance( 'PDF' ).\r\n  lo_output-&gt;output( ).\r\n\r\n  lo_output = lcl_output=&gt;get_instance( 'PNG' ).\r\n  lo_output-&gt;output( ).<\/pre>\n<h2>Output<\/h2>\n<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_01.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1119\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=1119\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_01.png\" data-orig-size=\"313,164\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Factory Method &amp;#8211; Output\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_01.png\" class=\"size-medium wp-image-1119 aligncenter\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_01-300x157.png\" alt=\"Factory Method - Output\" width=\"300\" height=\"157\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_01-300x157.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/DESIGN_PATTERNS_FACTORY_METHOD_01.png 313w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Factory Method design pattern provides unique interface to object instantiation. The decision on which object is to be instantiated is hidden from the outside world in the code. Using this approach different objects can be instantiated dynamically.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Design Patterns in ABAP - Factory Method","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,15,6,4,9],"tags":[446,335,336,221],"class_list":["post-1122","post","type-post","status-publish","format-standard","hentry","category-abap","category-algorithms","category-creational-patterns","category-design-patterns","category-development","tag-abap","tag-design-patterns-2","tag-factory-method","tag-oo-abap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-i6","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1122"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1122\/revisions"}],"predecessor-version":[{"id":1128,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1122\/revisions\/1128"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}