{"id":8042,"date":"2012-09-24T10:22:15","date_gmt":"2012-09-24T04:52:15","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8042"},"modified":"2012-09-24T10:22:15","modified_gmt":"2012-09-24T04:52:15","slug":"groovy-delegation-pattern-using-invokemethod-and-propertymissing-methods","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-delegation-pattern-using-invokemethod-and-propertymissing-methods\/","title":{"rendered":"Groovy Delegation Pattern Using invokeMethod and propertyMissing methods"},"content":{"rendered":"<p>Following DRY is practised  religously in my current project. Recently, I identified a case where code repetition had long been ignored. We used to pass either a domain object or a command object in the view model. Due to this, many domain methods had to be duplicated in the command object. This was probablematic because one would easily forget to create\/modify a function in the command object if it was created\/modified in the domain object. The code was smelling bad. The classes were something like this:<br \/>\n[java]<br \/>\nClass College{<br \/>\n\/\/attributes<\/p>\n<p>\/\/somethods<br \/>\n}<\/p>\n<p>Class CollegeCO{<br \/>\nCollege college<\/p>\n<p> \/\/other attributes and constraints<\/p>\n<p> \/\/ repetitive College methods<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p> Groovy intercepts all methods and property access via invokeMethod and get\/SetProperty property hooks. So all I needed to do was to override invokeMethod() and propertyMissing() functions in the CommandObject like this:<\/p>\n<p>[java]<br \/>\ndef invokeMethod(String name, args) {<br \/>\n        college.invokeMethod(name, args)<br \/>\n    }<\/p>\n<p>def propertyMissing(String name) {<br \/>\n        college.&quot;$name&quot;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>So any missing method or property call on command object was automatically delegated to the college object. This is a simple way of utilising the Delegation Patttern using Groovy.<\/p>\n<p>Hope this helps.<\/p>\n<p>Imran Mir<br \/>\nImran[at]intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following DRY is practised religously in my current project. Recently, I identified a case where code repetition had long been ignored. We used to pass either a domain object or a command object in the view model. Due to this, many domain methods had to be duplicated in the command object. This was probablematic because [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7],"tags":[1017,1015,1016],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8042"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8042"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8042\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}