{"id":5120,"date":"2024-09-14T15:40:29","date_gmt":"2024-09-14T15:40:29","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5120"},"modified":"2024-09-21T08:44:31","modified_gmt":"2024-09-21T08:44:31","slug":"factory-pattern-with-hashmap","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/factory-pattern-with-hashmap\/","title":{"rendered":"Simple Factory Pattern with HashMap"},"content":{"rendered":"<blockquote><p>Factory allows the consumer to create new objects without having to know the details of how they&#8217;re created, or what their dependencies are &#8211; they only have to give the information they actually want.<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/09\/FactoryPatternUML3.png\" alt=\"\" \/><\/p>\n<p><strong>Account.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nabstract class Account {\r\n   abstract Integer calculateInterest();\r\n}\r\n<\/pre>\n<p><strong>CreditAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class CreditAccount extends Account{\r\n    @Override\r\n    Integer calculateInterest() {\r\n        return 11;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SalaryAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SalaryAccount extends Account{\r\n    @Override\r\n    Integer calculateInterest() {\r\n        return 5;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SavingsAccount.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SavingsAccount extends Account{\r\n   @Override\r\n    Integer calculateInterest() {\r\n        return 7;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>AccountFactory.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class AccountFactory {\r\n    static Map&lt;String, Account&gt;  hmAccountMap =  new HashMap&lt;&gt;();\r\n\r\n    static {\r\n        hmAccountMap.put(&quot;SavingsAcc&quot;, new SavingsAccount());\r\n        hmAccountMap.put(&quot;CreditAcc&quot;, new CreditAccount());\r\n        hmAccountMap.put(&quot;SalaryAcc&quot;, new SalaryAccount());\r\n    }\r\n\r\n    public static Account getAccount(String accountType){\r\n        return hmAccountMap.get(accountType);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>CalcInterest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class CalcInterest{\r\n    public static void main(String&#x5B;] args) {\r\n        Account objAccountFactory = AccountFactory.getAccount(&quot;SavingsAcc&quot;);\r\n        System.out.println(&quot;Interest rate is - &quot; + Optional.of(objAccountFactory.calculateInterest()));\r\n    }\r\n}\r\n<\/pre>\n<p>Using Streams for AccountFactory Class<br \/>\n<strong>AccountFactory.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static Optional&lt;Account&gt; getAccount(String accountType) {\r\n        return hmAccountMap.entrySet().stream()\r\n                                      .filter(accParam -&gt; accParam.getKey().equals(accountType))\r\n                                      .findFirst()\r\n                                      .map(Map.Entry::getValue);\r\n}\r\n<\/pre>\n<p><strong>CalcInterest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class CalcInterest{\r\n    public static void main(String&#x5B;] args) {\r\n        Account objAccountFactory = AccountFactory.getAccount(&quot;SavingsAcc&quot;);\r\n        System.out.println(&quot;Interest rate is - &quot; + Optional.of(objAccountFactory.calculateInterest()));\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nInterest rate is - 7\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Factory allows the consumer to create new objects without having to know the details of how they&#8217;re created, or what their dependencies are &#8211; they only have to give the information they actually want. Account.java abstract class Account { abstract Integer calculateInterest(); } CreditAccount.java public class CreditAccount extends Account{ @Override Integer calculateInterest() { return 11;&hellip; <a href=\"https:\/\/codethataint.com\/blog\/factory-pattern-with-hashmap\/\">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":[228],"tags":[],"class_list":["post-5120","post","type-post","status-publish","format-standard","hentry","category-factory-pattern"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5120","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=5120"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5120\/revisions"}],"predecessor-version":[{"id":5136,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5120\/revisions\/5136"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}