{"id":591,"date":"2010-05-28T00:50:09","date_gmt":"2010-05-27T23:50:09","guid":{"rendered":"http:\/\/www.gennard.net\/blog\/?p=591"},"modified":"2010-05-28T00:50:09","modified_gmt":"2010-05-27T23:50:09","slug":"method-chaining","status":"publish","type":"post","link":"http:\/\/www.gennard.net\/blog\/2010\/05\/method-chaining\/","title":{"rendered":"Method Chaining"},"content":{"rendered":"<p>Creating objects with a complex constructor can be a bit of a pain in any language.     One technique I have used is method chaining.  It is not applicable to every type of class but it can be useful.<\/p>\n<p>Method chain can help simply the use class and allows more complex object initialisation without having to worry about the order of the parameters and be done inline.<\/p>\n<p>Consider the use of an &#8220;Account&#8221; class that takes Name, Address, Telephone and Country.  All of which are strings, the constructor with four strings would not be a great constructor.  <\/p>\n<p>So you could have a simple constructor and four properties\/methods.. however so set up the object would mean you have spread the setup over multiple lines.<\/p>\n<p>Using method chain you can overcome this and even space in the &#8220;value&#8221; area of the object in your storage area.<\/p>\n<p><code lang=\"cobol\" width=\"800\" lines=\"-1\" nowrap=\"0\"><br \/>\nset x to new type Account::Name(\"xx\")::Address(\"yy\") ::Telephone(\"yy\")<br \/>\n     ::Country(\"zz\")::World(\"Earth\")<br \/>\n<\/code><\/p>\n<p>The trick of the pattern is to provide methods that always return this\/self, so we can change the invokes together&#8230;    For example:<\/p>\n<p>This technique could even be used to build up a series of items required, for example, the preparation and execution of a sql statement comes to mind&#8230; in a similar fashion to Linq.<\/p>\n<p>So&#8230; lets look at an example:<\/p>\n<p><code lang=\"cobol\" width=\"800\" lines=\"-1\" nowrap=\"0\"><br \/>\n$set ilusing\"System.Collections.Generic\"<br \/>\nprogram-id. Program1 as \"MethodChaining1.Program1\".<br \/>\ndata division.<br \/>\nworking-storage section.<br \/>\n01 accounts type List[type Account] value new type List[type Account].<br \/>\n01 jAccount type Account value<br \/>\n   new type Account::Name(\"Mr Johnson\")::Address(\"Somewhere, some place\")<br \/>\n    ::Telephone(\"+44 1234 4321\").<br \/>\n01 sAccount type Account value<br \/>\n   new type Account::Name(\"Mr Smith\")::Address(\"Nowhere place\")<br \/>\n     ::Telephone(\"+44 1234 4321\")::Country(\"Wales\").<br \/>\n01 lAccount type Account.<br \/>\nprocedure division.<br \/>\n invoke accounts::Add(jAccount)<br \/>\n invoke accounts::Add(sAccount)<\/p>\n<p> perform varying lAccount through accounts<br \/>\n  display lAccount<br \/>\n end-perform<\/p>\n<p> goback.<br \/>\nend program.<\/p>\n<p><\/code><\/p>\n<p>WIth the class being:<\/p>\n<p><code lang=\"cobol\" width=\"800\" lines=\"-1\" nowrap=\"0\"><br \/>\nclass-id Account.<\/p>\n<p>working-storage section.<br \/>\n01 wName       string property as \"Name\".<br \/>\n01 wAddres     string property as \"Address\".<br \/>\n01 wCountry    string property as \"Country\".<br \/>\n01 wTelephone  string property as \"Telephone\".<br \/>\n01 wEmail      string property as \"Email\".<\/p>\n<p>method-id New.<br \/>\nlocal-storage section.<br \/>\nprocedure division.<br \/>\n  set wCountry to type System.Globalization.RegionInfo::CurrentRegion::DisplayName<br \/>\nend method.<\/p>\n<p>method-id Name public.<br \/>\nprocedure division using uName as string<br \/>\n returning ret as type Account.<br \/>\n  set self::Name to uName<br \/>\n  set ret to self<br \/>\nend method.<\/p>\n<p>method-id Address public.<br \/>\nprocedure division using uAddress as string<br \/>\n returning ret as type Account.<br \/>\n  set self::Address to uAddress<br \/>\n  set ret to self<br \/>\nend method.<\/p>\n<p>method-id Telephone public.<br \/>\nprocedure division using uTelephone as string<br \/>\n returning ret as type Account.<br \/>\n  set self::Address to uTelephone<br \/>\n  set ret to self<br \/>\nend method.<\/p>\n<p>method-id Email public.<br \/>\nprocedure division using uEmail as string<br \/>\n returning ret as type Account.<br \/>\n  set self::Address to uEmail<br \/>\n  set ret to self<br \/>\nend method.<\/p>\n<p>method-id Country public.<br \/>\nprocedure division using uCountry as string<br \/>\n returning ret as type Account.<br \/>\n  set self::Country to uCountry<br \/>\n  set ret to self<br \/>\nend method.<\/p>\n<p>method-id ToString public override.<br \/>\nprocedure division returning ret as string.<br \/>\nset ret to String::Format(\"Name:{0}, Address:{1}, Telephone:{2}, Email:{3}, Country:{4}\",<br \/>\n     self::Name, self::Address, self::Telephone,<br \/>\n     self::Email, self::Country)<br \/>\nend method.<br \/>\nend class.<br \/>\n<\/code><\/p>\n<p>Which when run with Visual COBOL gives: <\/p>\n<p><code lang=\"text\"  width=\"800\" lines=\"-1\" nowrap=\"0\"><br \/>\nName:Mr Johnson, Address:+44 1234 4321, Telephone:, Email:, Country:United Kingdom<br \/>\nName:Mr Smith, Address:+44 1234 4321, Telephone:, Email:, Country:Wales<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating objects with a complex constructor can be a bit of a pain in any language. One technique I have used is method chaining. It is not applicable to every type of class but it can be useful. Method chain &hellip; <a href=\"http:\/\/www.gennard.net\/blog\/2010\/05\/method-chaining\/\">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":[208,209,77,130,227,188,194],"_links":{"self":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/591"}],"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=591"}],"version-history":[{"count":0,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/591\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/media?parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/categories?post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/tags?post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}