{"id":2895,"date":"2018-10-07T08:34:35","date_gmt":"2018-10-07T08:34:35","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2895"},"modified":"2024-02-18T07:47:46","modified_gmt":"2024-02-18T07:47:46","slug":"abstract-factory-pattern","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/abstract-factory-pattern\/","title":{"rendered":"Abstract Factory Pattern"},"content":{"rendered":"<p><strong>Abstract Factory Pattern<\/strong><br \/>\nThe Abstract Factory Pattern consists of an AbstractFactory, ConcreteFactory, AbstractProduct, ConcreteProduct and Client. We code against AbstractFactory and AbstractProduct and let the implementation to others to define ConcreteFactory and ConcreteProduct<\/p>\n<p><strong>Why Abstract Factory Introduced?<\/strong><br \/>\nAnother layer of abstraction over factory pattern. Abstract Factory patterns work around a super-factory which creates other factories. Factory pattern deals with creating objects of a single type(AppleFactory manufactures Iphones), while the Abstract Factory pattern deals with creating objects of related types(MobileFactory manufactures Mobiles). Abstract Factory pattern is more robust and consistent and uses composition to delegate responsibility of object creation. <\/p>\n<p><strong>When to use Abstract Factory?<\/strong><br \/>\nWhen higher level of Abstraction is required.Provides an interface for creating families of related objects<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/02\/AbstractFactoryPattern.png\" alt=\"\" \/><\/p>\n<p><strong>MobileManufactureFactory.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface MobileManufactureFactory {\r\n    public Mobiles getManufacturerDetails();\r\n}\r\n<\/pre>\n<p><strong>Mobiles.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface Mobiles {\r\n    public void getMobileModels();\r\n}\r\n<\/pre>\n<p><strong>AppleFactory.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class AppleFactory implements MobileManufactureFactory {\r\n    @Override\r\n    public Mobiles getManufacturerDetails() {\r\n        return new Iphone();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>GoogleFactory.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class GoogleFactory implements MobileManufactureFactory {\r\n    @Override\r\n    public Mobiles getManufacturerDetails() {\r\n        return new Android();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>MicrosoftFactory.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MicrosoftFactory implements MobileManufactureFactory {\r\n    @Override\r\n    public Mobiles getManufacturerDetails() {\r\n        return new Windows();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>IphoneMobiles.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class IphoneMobiles implements Mobiles {\r\n    @Override\r\n    public void getMobileModels() {\r\n        System.out.println(&quot;Iphone 13,14 and 15&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>AndroidMobiles.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class AndroidMobiles implements Mobiles {\r\n    @Override\r\n    public void getMobileModels() {\r\n        System.out.println(&quot;Oneplus, Realme, Samsung&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>WindowsMobile.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class WindowsMobile implements Mobiles {\r\n    @Override\r\n    public void getMobileModels() {\r\n        System.out.println(&quot;Samsung Focus, Nokia Lumia, Pocket PC&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>MobileFactory.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MobileFactory {\r\n    private MobileFactory(){\r\n    }\r\n\r\n    public static MobileManufactureFactory getMobilesBasedOnManufacturer(MobileCompany manufacturerName){\r\n        if(manufacturerName.equals(MobileCompany.APPLE)){\r\n            return new AppleFactory();\r\n        }else if(manufacturerName.equals(MobileCompany.MICROSOFT)){\r\n            return new MicrosoftFactory();\r\n        }else if(manufacturerName.equals(MobileCompany.GOOGLE)){\r\n            return new GoogleFactory();\r\n        }\r\n        return null;\r\n    }\r\n}\r\n\r\n<\/pre>\n<p><strong>MobileCompany.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic enum MobileCompany {\r\n    APPLE, MICROSOFT, GOOGLE\r\n}\r\n<\/pre>\n<p><strong>Client.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Client {\r\n    public static void main(String&#x5B;] args) {\r\n        MobileManufactureFactory objMobileManufa = MobileFactory.getMobilesBasedOnManufacturer(MobileCompany.APPLE);\r\n        Mobiles objMobile = objMobileManufa.getManufacturerDetails();\r\n        objMobile.getMobileModels();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n Iphone 13,14 and 15\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Abstract Factory Pattern The Abstract Factory Pattern consists of an AbstractFactory, ConcreteFactory, AbstractProduct, ConcreteProduct and Client. We code against AbstractFactory and AbstractProduct and let the implementation to others to define ConcreteFactory and ConcreteProduct Why Abstract Factory Introduced? Another layer of abstraction over factory pattern. Abstract Factory patterns work around a super-factory which creates other factories.&hellip; <a href=\"https:\/\/codethataint.com\/blog\/abstract-factory-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":[353,354,64],"tags":[],"class_list":["post-2895","post","type-post","status-publish","format-standard","hentry","category-creational-patterns","category-abstract-factory-pattern","category-design-patterns"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2895","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=2895"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2895\/revisions"}],"predecessor-version":[{"id":4905,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2895\/revisions\/4905"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}