{"id":2303,"date":"2017-05-14T08:27:03","date_gmt":"2017-05-14T08:27:03","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2303"},"modified":"2017-05-14T08:36:24","modified_gmt":"2017-05-14T08:36:24","slug":"visitor-pattern","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/visitor-pattern\/","title":{"rendered":"Visitor Pattern Example"},"content":{"rendered":"<p>Lets take the below scenario where there are Different segment of customers whose discount rates is going to vary based on the segment type and the Store they are visiting<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2017\/05\/VisitorPattern.png\" alt=\"\" height=\"578\" width=\"464\"\/><\/p>\n<p>Now the Customer may hold <em>Platinum,Premium or Club Card<\/em> which avails them some discount on the total bill amount.<br \/>\nAgain this discount vary based on the store from which the purchase is made <em>Regular, Franchise, Licensed Store<\/em><\/p>\n<p><strong>Visitor.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.visitor;\r\n\r\npublic interface Visitor {\r\n\tpublic double getDiscount(FranchiseStore objFranchiseStore);\r\n\tpublic double getDiscount(RegularStore objRegularStore);\r\n\tpublic double getDiscount(LicensedStore objLicensedStore);\r\n}\r\n<\/pre>\n<p><strong>Visitable.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.visitor;\r\n\r\npublic interface Visitable {\r\n\tpublic double acceptDiscount(Visitor objVisitior);\t\r\n}\r\n<\/pre>\n<p><strong>FranchiseStore.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.visitor;\r\n\r\npublic class FranchiseStore implements Visitable{\r\n\tprivate int totalBillAmt;\r\n\r\n\tFranchiseStore(int item) {\r\n\t\ttotalBillAmt = item;\r\n\t}\r\n\r\n\tpublic int getTotalBillAmt() {\r\n\t\treturn totalBillAmt;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic double acceptDiscount(Visitor objVisitior) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0;\r\n\t}\t\t\r\n}\r\n<\/pre>\n<p><strong>RegularStore.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.visitor;\r\n\r\npublic class RegularStore implements Visitable {\r\n\r\n\tprivate int totalBillAmt;\r\n\r\n\tRegularStore(int item) {\r\n\t\ttotalBillAmt = item;\r\n\t}\r\n\r\n\tpublic double acceptDiscount(Visitor visitor) {\r\n\t\treturn visitor.getDiscount(this);\r\n\t}\r\n\r\n\tpublic double getTotalBillAmt() {\r\n\t\treturn totalBillAmt;\r\n\t}\t\t\r\n}\r\n<\/pre>\n<p><strong>LicensedStore.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.visitor;\r\n\r\npublic class LicensedStore implements Visitable {\r\n\t\r\n\tprivate int totalBillAmt;\r\n\r\n\tLicensedStore(int item) {\r\n\t\ttotalBillAmt = item;\r\n\t}\r\n\r\n\tpublic double acceptDiscount(Visitor visitor) {\r\n\t\treturn visitor.getDiscount(this);\r\n\t}\r\n\r\n\tpublic double getTotalBillAmt() {\r\n\t\treturn totalBillAmt;\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p><strong>ClubCardVisitor.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.visitor;\r\n\r\npublic class ClubCardVisitor implements Visitor \r\n{\t\r\n\t@Override\r\n\tpublic double getDiscount(FranchiseStore objFranchiseStore) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0.02;\t\r\n\t}\r\n\r\n\t@Override\r\n\tpublic double getDiscount(RegularStore objRegularStore) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0.03;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic double getDiscount(LicensedStore objLicensedStore) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0.00;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>PlatinumCardVisitor.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.visitor;\r\n\r\npublic class PlatinumCardVisitor implements Visitor \r\n{\r\n\t@Override\r\n\tpublic double getDiscount(FranchiseStore objFranchiseStore) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0.05;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic double getDiscount(RegularStore objRegularStore) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0.07;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic double getDiscount(LicensedStore objLicensedStore) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0.03;\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p><strong>PremiumCardVisitor.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.visitor;\r\n\r\npublic class PremiumCardVisitor implements Visitor {\r\n\t@Override\r\n\tpublic double getDiscount(FranchiseStore objFranchiseStore) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0.03;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic double getDiscount(RegularStore objRegularStore) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0.05;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic double getDiscount(LicensedStore objLicensedStore) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\treturn 0.02;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>GetVisitorDiscount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.visitor;\r\n\r\npublic class GetVisitorDiscount \r\n{\r\n\tpublic static void main(String&#x5B;] args) \r\n\t{\t\t\r\n\t\tint totalBillAmount = 500;\r\n\t\t\r\n\t\tSystem.out.println(&quot;Discount for Club Card Visitor @ Franchise Store&quot;);\r\n\t\t\r\n\t\tClubCardVisitor objClubCardVisitor = new ClubCardVisitor();\r\n\t\tFranchiseStore objFranchiseStore = new FranchiseStore(totalBillAmount);\r\n\t\t\r\n\t\t\r\n\t\tdouble discountPct = objClubCardVisitor.getDiscount(objFranchiseStore);\r\n\t\tdouble discountAmt = objClubCardVisitor.getDiscount(objFranchiseStore)*totalBillAmount;\r\n\t\tdouble AmtAfterDis = totalBillAmount - discountAmt; \r\n\t\t\r\n\t\t\r\n\t\tSystem.out.println(&quot;---------------------------------------------------&quot;);\r\n\t\tSystem.out.println(&quot;Total Amount of Purchase &quot; + totalBillAmount);\r\n\t\tSystem.out.println(&quot;Discount Percentage &quot; + discountPct*100 + &quot;%&quot;);\r\n\t\tSystem.out.println(&quot;Discount Amount &quot; + discountAmt);\r\n\t\tSystem.out.println(&quot;Total Amount after Discount &quot; + AmtAfterDis);\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nDiscount for Club Card Visitor @ Franchise Store\r\n---------------------------------------------------\r\nTotal Amount of Purchase 500\r\nDiscount Percentage 2.0%\r\nDiscount Amount 10.0\r\nTotal Amount after Discount 490.0\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Lets take the below scenario where there are Different segment of customers whose discount rates is going to vary based on the segment type and the Store they are visiting Now the Customer may hold Platinum,Premium or Club Card which avails them some discount on the total bill amount. Again this discount vary based on&hellip; <a href=\"https:\/\/codethataint.com\/blog\/visitor-pattern\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[231],"tags":[],"class_list":["post-2303","post","type-post","status-publish","format-standard","hentry","category-visitor-pattern"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2303"}],"version-history":[{"count":10,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2303\/revisions"}],"predecessor-version":[{"id":2314,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2303\/revisions\/2314"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}