{"id":1790,"date":"2019-04-13T08:34:22","date_gmt":"2019-04-13T08:34:22","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=1790"},"modified":"2024-12-16T22:32:38","modified_gmt":"2024-12-16T19:32:38","slug":"visitor-pattern","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/hi\/2019\/04\/13\/visitor-pattern\/","title":{"rendered":"Visitor pattern"},"content":{"rendered":"<p><em>In this note I will describe a design pattern called &#8220;Visitor&#8221; or &#8220;Visitor&#8221;<br \/><\/em>This pattern belongs to the group of <strong>Behavioral patterns<\/strong>.<\/p>\n<h3>Let&#8217;s think of a problem<\/h3>\n<p>This pattern is mainly used to work around the single dispatch limitation in early-binding languages.<\/p>\n<p><a href=\"https:\/\/www.flickr.com\/photos\/nfg\/16506635470\/in\/photostream\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1798\" src=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2019\/04\/kinbaku.jpg\" alt=\"\" width=\"420\" height=\"640\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.flickr.com\/photos\/nfg\/16506635470\/in\/photostream\/\" target=\"_blank\" rel=\"noopener noreferrer\">Alice X<\/a> by <a href=\"https:\/\/nfgphoto.com\" target=\"_blank\" rel=\"noopener noreferrer\">NFGPhoto<\/a> (<a href=\"https:\/\/creativecommons.org\/licenses\/by\/2.0\/\" target=\"_blank\" rel=\"noopener noreferrer\">CC-2.0<\/a>)<br \/>Let&#8217;s create an abstract class\/protocol Band, make a subclass MurpleDeep, create a class Visitor with two methods &#8211; one for outputting to the console any descendant of Band, the second for outputting any MurpleDeep, the main thing is that the names (signatures) of the methods are the same, and the arguments differ only in the class. Through the intermediate method printout with the argument Band, create an instance of Visitor and call the visit method for MurpleDeep.<br \/>Next is the code in Kotlin:<br \/><!-- iframe plugin v.5.1 wordpress.org\/plugins\/iframe\/ --><br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/pl.kotl.in\/qsxky0Z8C\" width=\"360\" height=\"400\" scrolling=\"yes\" class=\"iframe-class\" frameborder=\"0\"><\/iframe>\n<\/p>\n<p>The output will be &#8220;<strong><span class=\"standard-output darcula\">This is Band class<\/span><\/strong>&#8220;<\/p>\n<h3>How is that possible?!<\/h3>\n<p>Why this happens is described in clever words in many articles, including in Russian, but I suggest you imagine how the compiler sees the code, perhaps everything will become clear right away:<br \/><!-- iframe plugin v.5.1 wordpress.org\/plugins\/iframe\/ --><br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/pl.kotl.in\/oBsgszLCO\" width=\"360\" height=\"500\" scrolling=\"yes\" class=\"iframe-class\" frameborder=\"0\"><\/iframe>\n<\/p>\n<h3>Solving the problem<\/h3>\n<p>There are many solutions to this problem, below we will consider a solution using the Visitor pattern.<br \/>In the abstract class\/protocol we add the accept method with the Visitor argument, inside the method we call visitor.visit(this), after that we add an override\/implementation of the accept method to the MurpleDeep class, decisively and calmly violating DRY, we write visitor.visit(this) again.<br \/>Final code:<br \/><!-- iframe plugin v.5.1 wordpress.org\/plugins\/iframe\/ --><br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/pl.kotl.in\/0fvYomzJd\" width=\"360\" height=\"500\" scrolling=\"yes\" class=\"iframe-class\" frameborder=\"0\"><\/iframe>\n<\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/refactoring.guru\/ru\/design-patterns\/visitor-double-dispatch\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/refactoring.guru\/ru\/ design-patterns\/visitor-double-dispatch<\/a><\/p>\n<h3>Source code<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/patterns\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gitlab.com\/demensdeum\/patterns<\/a><\/p >\n","protected":false},"excerpt":{"rendered":"<p>In this note I will describe a design pattern called &#8220;Visitor&#8221; or &#8220;Visitor&#8221;This pattern belongs to the group of Behavioral patterns. Let&#8217;s think of a problem This pattern is mainly used to work around the single dispatch limitation in early-binding languages. Alice X by NFGPhoto (CC-2.0)Let&#8217;s create an abstract class\/protocol Band, make a subclass MurpleDeep,<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/hi\/2019\/04\/13\/visitor-pattern\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Visitor pattern&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[95,98],"class_list":["post-1790","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-patterns","tag-visitor","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"hi","enabled_languages":["en","ru","zh","de","fr","ja","pt","hi"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false},"hi":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/1790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/comments?post=1790"}],"version-history":[{"count":38,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/1790\/revisions"}],"predecessor-version":[{"id":3960,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/1790\/revisions\/3960"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/media?parent=1790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/categories?post=1790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/tags?post=1790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}