{"id":6406,"date":"2012-08-25T00:18:31","date_gmt":"2012-08-24T18:48:31","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6406"},"modified":"2016-12-19T15:30:58","modified_gmt":"2016-12-19T10:00:58","slug":"what-is-delegate","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/what-is-delegate\/","title":{"rendered":"What is Delegate ??"},"content":{"rendered":"<p>Suppose a scenario, where User and Account classes are linked as below:<br \/>\n[groovy]<br \/>\nclass Account {<br \/>\n    String email<br \/>\n    String password<br \/>\n    \/\/ &#8230; other fields<br \/>\n}<\/p>\n<p>class User {<br \/>\n    Account account<br \/>\n    String name<br \/>\n    String address<br \/>\n    \/\/ &#8230; other fields<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p style=\"padding:10px\">\n<p>At the time of creating a new User, and accessing the user members, we write something of this sort:<br \/>\n[groovy]<br \/>\n\/\/ For initalizing the user class first we have to initializing Account class.<br \/>\nAccount account = new Account(email:&#8221;amit.kumar@gmail.com&#8221;, password:&#8221;intelligrape&#8221;)<br \/>\n\/\/ Now have to pass that account instance for initializing User class<br \/>\nUser user = new User(account:account, name:&#8221;Amit&#8221;, address:&#8221;Intelligrape City&#8221;)<\/p>\n<p>\/\/ For Accessing User&#8217;s Account information we have to use &#8220;user.account&#8221; as prefix<br \/>\nprintln user.account.email<br \/>\nprintln user.account.password<br \/>\n[\/groovy]<\/p>\n<p style=\"padding:10px\">\n<p>It can be clearly seen that we have to write &#8220;user.account&#8221; as a prefix for accessing user&#8217;s account information and have to initialize account info separately. If we use @Delegate annotation before account declaration as below:<br \/>\n[groovy]<br \/>\n @Delegate Account account = new Account()<br \/>\n[\/groovy]<\/p>\n<p style=\"padding:10px\">\n<p>Time is saved and groovy will manage account for user automatically. We do not need to care for initializing account information separately.<br \/>\n[groovy]<br \/>\n\/\/ Initializing account information with User class.<br \/>\nUser user = new User(email:&#8221;amit.kumar@gmail.com&#8221;, password:&#8221;intelligrape&#8221;, name:&#8221;Amit&#8221;, address:&#8221;Intelligrape City&#8221;)<br \/>\n\/\/ Accessing User Account info with &#8220;user&#8221; prefix only instead of &#8220;user.account&#8221; prefix<br \/>\nprintln user.email<br \/>\nprintln user.password<br \/>\n[\/groovy]<\/p>\n<p style=\"padding:10px\">\n<p><strong>Note:<\/strong> When you use <strong>@Delegate<\/strong> annotation, all the methods of delegated class(Account) are implement in current class(User), so we will be able to access those members with prefix (&#8220;user&#8221;) only.<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose a scenario, where User and Account classes are linked as below: [groovy] class Account { String email String password \/\/ &#8230; other fields } class User { Account account String name String address \/\/ &#8230; other fields } [\/groovy] At the time of creating a new User, and accessing the user members, we write [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[885,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6406"}],"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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6406"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6406\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}