{"id":5135,"date":"2024-09-21T09:19:18","date_gmt":"2024-09-21T09:19:18","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5135"},"modified":"2024-11-07T10:52:11","modified_gmt":"2024-11-07T10:52:11","slug":"factory-pattern-2","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/factory-pattern-2\/","title":{"rendered":"Factory Method Pattern"},"content":{"rendered":"<blockquote><p>\nDefine an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/09\/N3mC1.png\" alt=\"\" \/><\/p>\n<ol>\n<li>In Factory Pattern we have Product(Abstract), ConcreteProduct and Creator(Abstract), ConcreteCreator<\/li>\n<li>ConcreteCreator would create ConcreteProduct by implementing abstract factory method of Creator which has Product return type<\/li>\n<li>Incase if there is any new Product to be added it fully supports Open Closed Principle(Open For Extension, Closed for Changes).<\/li>\n<li>Open for Extension &#8211; Adding new ConcreteProduct and ConcreateCreator class, Closed for Changes &#8211; No changes in anyother code unlike Simple factory or static factory method which requires change in Switchcase, enum (or) if case<\/li>\n<li>Closed for Changes &#8211; No changes in anyother code unlike Simple factory or static factory method which requires change in Switchcase, enum (or) if case<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/09\/RealFactoryPattern.png\" alt=\"\" \/><\/p>\n<p><strong>JobProfile.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nabstract class JobProfile {\r\n    public abstract String mandatorySkills();\r\n\r\n    public Integer defaultWorkHrs(){\r\n        return 8;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>JavaProfile.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class JavaProfile extends JobProfile{\r\n    @Override\r\n    public String mandatorySkills() {\r\n        return &quot;Java, Springboot, Microservices&quot;;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SQLProfile.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SQLProfile extends JobProfile{\r\n    @Override\r\n    public String mandatorySkills() {\r\n        return &quot;Cosmos, MySQL, MSSQL&quot;;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>JobProfileCreator.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nabstract class JobProfileCreator {\r\n    public JobProfile getJobProfile(){\r\n        JobProfile objJobProfile = createJobProfileFactory();\r\n        return objJobProfile;\r\n    }\r\n\r\n    public abstract JobProfile createJobProfileFactory();\r\n}\r\n<\/pre>\n<p><strong>JavaProfileCreator.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class JavaProfileCreator extends JobProfileCreator {\r\n    @Override\r\n    public JobProfile createJobProfileFactory() {\r\n       return new JavaProfile();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>SQLProfileCreator.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SQLProfileCreator extends JobProfileCreator {\r\n    @Override\r\n    public JobProfile createJobProfileFactory() {\r\n        return new SQLProfile();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Consultancy.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Consultancy {\r\n    public static void main(String&#x5B;] args) {\r\n        getProfileDetails(new JavaProfileCreator());\r\n    }\r\n\r\n    public static void getProfileDetails(JobProfileCreator jobProfileCreator){\r\n        JobProfile objJobProfile = jobProfileCreator.getJobProfile();\r\n        System.out.println(objJobProfile.mandatorySkills() + &quot; with &quot;+ objJobProfile.defaultWorkHrs() + &quot;hrs of Work&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nJava, Springboot, Microservices with 8hrs of Work\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/09\/JobProfileCreator2.png\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. In Factory Pattern we have Product(Abstract), ConcreteProduct and Creator(Abstract), ConcreteCreator ConcreteCreator would create ConcreteProduct by implementing abstract factory method of Creator which has Product return type Incase if there is any&hellip; <a href=\"https:\/\/codethataint.com\/blog\/factory-pattern-2\/\">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-5135","post","type-post","status-publish","format-standard","hentry","category-factory-pattern"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5135","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=5135"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5135\/revisions"}],"predecessor-version":[{"id":5302,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5135\/revisions\/5302"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}