{"id":363,"date":"2009-12-07T21:49:24","date_gmt":"2009-12-07T20:49:24","guid":{"rendered":"http:\/\/www.gennard.net\/blog\/?p=363"},"modified":"2009-12-07T21:49:24","modified_gmt":"2009-12-07T20:49:24","slug":"factory-method-pattern-in-cobol","status":"publish","type":"post","link":"http:\/\/www.gennard.net\/blog\/2009\/12\/factory-method-pattern-in-cobol\/","title":{"rendered":"Factory Method Pattern in COBOL"},"content":{"rendered":"<p>Continuing my series on design patterns for the COBOL, the next one on my list is the &#8220;Factory method&#8221; pattern.<\/p>\n<p>The pattern is useful, as it helps you hide the real implementation\/creation mechanism of your classes.   I you are fond of uml&#8230; here is the actual uml (from wikipedia).<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/a\/a3\/FactoryMethod.svg\/500px-FactoryMethod.svg.png\" \/><\/p>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Factory_method_pattern\">Factory Method Pattern<\/a> from Wikipedia!<\/p>\n<p>So&#8230; lets see the COBOL code&#8230;<\/p>\n<p><code lang=\"cobol\" width=\"800\" lines=\"-1\" nowrap=\"0\"><br \/>\n       interface-id. \"Base\".<br \/>\n         method-id. \"DoIt\".<br \/>\n         end method \"DoIt\".<br \/>\n       end interface \"Base\".<\/p>\n<p>       class-id. \"Derived1Impl\".<br \/>\n       object. implements type \"Base\".<br \/>\n        method-id. \"DoIt\" public.<br \/>\n         display \"Derived1Impl from DoIt\".<br \/>\n        end method \"DoIt\".<br \/>\n       end object.<br \/>\n       end class \"Derived1Impl\".<\/p>\n<p>       class-id. \"Derived2Impl\".<br \/>\n       object. implements type \"Base\".<br \/>\n        method-id. \"DoIt\" public.<br \/>\n         display \"Derived2Impl from DoIt\".<br \/>\n        end method \"DoIt\".<br \/>\n       end object.<br \/>\n       end class \"Derived2Impl\".<\/p>\n<p>       class-id. \"Factory\".<br \/>\n       object.<br \/>\n         method-id. \"GetObject\".<br \/>\n         linkage section.<br \/>\n         01 obj-base type \"Base\".<br \/>\n         procedure division using by value oType as binary-long<br \/>\n               returning obj-base.<\/p>\n<p>           evaluate oType<br \/>\n              when 1<br \/>\n                 set obj-base to new type \"Derived1Impl\"()<br \/>\n              when 2<br \/>\n                 set obj-base to new type \"Derived2Impl\"()<br \/>\n              when other<br \/>\n                 set obj-base to null<br \/>\n         end method \"GetObject\".<br \/>\n       end object.<br \/>\n       end class \"Factory\".<\/p>\n<p>       class-id. \"FactoryDemo\".<\/p>\n<p>       method-id. \"Main\" static.<br \/>\n       local-storage section.<br \/>\n       01 obj-factory type \"Factory\".<br \/>\n       01 base-obj type \"Base\".<\/p>\n<p>       linkage section.<br \/>\n       01 args string occurs any.<br \/>\n       procedure division using by value args.<br \/>\n          set obj-factory to new type \"Factory\"()<\/p>\n<p>          set base-obj to obj-factory::\"GetObject\"(1)<br \/>\n          invoke base-obj::\"DoIt\"()<\/p>\n<p>          set base-obj to obj-factory::\"GetObject\"(2)<br \/>\n          invoke base-obj::\"DoIt\"()<\/p>\n<p>       end method \"Main\".<br \/>\n       end class \"FactoryDemo\".<br \/>\n  <\/code><\/p>\n<p>That was pretty straight forward&#8230; not too much pain&#8230;<\/p>\n<p>And finally the code produces&#8230;<\/p>\n<p><code lang=\"text\"><br \/>\nd:> FactoryDemo.exe<br \/>\nDerived1Impl from DoIt<br \/>\nDerived2Impl from DoIt<br \/>\nHello world<br \/>\n<\/code><\/p>\n<p>Time to sign off for today.. but if you would like me to continue the series on code patterns or have a particular pattern you need&#8230; drop us a line!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continuing my series on design patterns for the COBOL, the next one on my list is the &#8220;Factory method&#8221; pattern. The pattern is useful, as it helps you hide the real implementation\/creation mechanism of your classes. I you are fond &hellip; <a href=\"http:\/\/www.gennard.net\/blog\/2009\/12\/factory-method-pattern-in-cobol\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5],"tags":[209,77,98],"_links":{"self":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/363"}],"collection":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/comments?post=363"}],"version-history":[{"count":0,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/363\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/media?parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/categories?post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/tags?post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}