{"id":78430,"date":"2025-08-28T17:11:42","date_gmt":"2025-08-28T17:11:42","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78430"},"modified":"2025-08-28T17:11:43","modified_gmt":"2025-08-28T17:11:43","slug":"factory-pattern-vs-%f0%9f%8f%ad-abstract-factory-pattern-in-java","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78430","title":{"rendered":"Factory Pattern vs \ud83c\udfed Abstract Factory Pattern in Java"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"661\" src=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-28.png?resize=750%2C661\" alt=\"\" class=\"wp-image-78431\" srcset=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-28.png?w=807 807w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-28.png?resize=300%2C264 300w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-28.png?resize=768%2C677 768w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-28.png?resize=750%2C661 750w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Abstract Factory Pattern Example in Java<\/strong><\/p>\n\n\n\n<p>From: AI Tools\/OpenAI\/Chatgpt<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>&#8220;<\/p>\n\n\n\n<p>Perfect \ud83d\udc4d Let me give you a <strong>clean, copyright-free Abstract Factory Pattern Java example<\/strong> with explanation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfed Abstract Factory Pattern Example in Java<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Problem:<\/h3>\n\n\n\n<p>We want to create <strong>UI components<\/strong> (<code>Button<\/code>, <code>Checkbox<\/code>) for <strong>different operating systems<\/strong> (Windows and Mac). The client should be able to request them without worrying about which OS family is used.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Define Product Interfaces<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Product A\ninterface Button {\n    void paint();\n}\n\n\/\/ Product B\ninterface Checkbox {\n    void check();\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create Concrete Products<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Windows Variants\nclass WindowsButton implements Button {\n    public void paint() {\n        System.out.println(\"Rendering a Windows style Button\");\n    }\n}\n\nclass WindowsCheckbox implements Checkbox {\n    public void check() {\n        System.out.println(\"Checking a Windows style Checkbox\");\n    }\n}\n\n\/\/ Mac Variants\nclass MacButton implements Button {\n    public void paint() {\n        System.out.println(\"Rendering a Mac style Button\");\n    }\n}\n\nclass MacCheckbox implements Checkbox {\n    public void check() {\n        System.out.println(\"Checking a Mac style Checkbox\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Define Abstract Factory<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface GUIFactory {\n    Button createButton();\n    Checkbox createCheckbox();\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Concrete Factories<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class WindowsFactory implements GUIFactory {\n    public Button createButton() {\n        return new WindowsButton();\n    }\n    public Checkbox createCheckbox() {\n        return new WindowsCheckbox();\n    }\n}\n\nclass MacFactory implements GUIFactory {\n    public Button createButton() {\n        return new MacButton();\n    }\n    public Checkbox createCheckbox() {\n        return new MacCheckbox();\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Client Code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class AbstractFactoryDemo {\n    public static void main(String&#91;] args) {\n        \/\/ Choose factory based on OS\n        GUIFactory factory;\n\n        String os = \"Windows\"; \/\/ could be detected dynamically\n\n        if (os.equals(\"Windows\")) {\n            factory = new WindowsFactory();\n        } else {\n            factory = new MacFactory();\n        }\n\n        \/\/ Use factory to create related objects\n        Button button = factory.createButton();\n        Checkbox checkbox = factory.createCheckbox();\n\n        \/\/ Work with products\n        button.paint();\n        checkbox.check();\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Output (if OS = Windows)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Rendering a Windows style Button\nChecking a Windows style Checkbox\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Output (if OS = Mac)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Rendering a Mac style Button\nChecking a Mac style Checkbox\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd11 Key Points:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Abstract Factory (<code>GUIFactory<\/code>)<\/strong> ensures we can switch between <strong>families of products<\/strong> easily.<\/li>\n\n\n\n<li>The <strong>client never directly instantiates<\/strong> <code>WindowsButton<\/code> or <code>MacButton<\/code>; it just asks the factory.<\/li>\n\n\n\n<li>This makes the code flexible, scalable, and easier to maintain.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udc49 Would you like me to also prepare a <strong>simpler \u201creal-life\u201d Abstract Factory example<\/strong> (like \ud83c\udf55 fast-food meal combos) so you can post something more fun and less technical on your blog?<\/p>\n\n\n\n<p>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abstract Factory Pattern Example in Java From: AI Tools\/OpenAI\/Chatgpt &#8220; Perfect \ud83d\udc4d Let me give you a clean, copyright-free Abstract Factory Pattern Java example with explanation. \ud83c\udfed Abstract Factory Pattern Example in Java Problem: We want to create UI components (Button, Checkbox) for different operating systems (Windows and Mac). The client should be able to &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=78430\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1962],"tags":[],"class_list":["post-78430","post","type-post","status-publish","format-standard","hentry","category-java-design-patterns","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":78099,"url":"http:\/\/bangla.sitestree.com\/?p=78099","url_meta":{"origin":78430,"position":0},"title":"Java Creational Design Patterns","author":"Sayed","date":"May 4, 2025","format":false,"excerpt":"5 types of creational design patterns: Factory Design Patterns: Purpose: Create Objects, Keep Object Creation Centralized Abstract Factory Design Patterns Singleton Design Pattern: Limit instantiation\u00a0 of a clas to only one instance Prototype Design Patterns: Object creation based on Prototype Object Instance; Simpler Object Creation than Factory. Builder Design Patterns:\u2026","rel":"","context":"In &quot;Root&quot;","block_context":{"text":"Root","link":"http:\/\/bangla.sitestree.com\/?cat=1"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/05\/image-3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/05\/image-3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/05\/image-3.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/05\/image-3.png?resize=700%2C400 2x"},"classes":[]},{"id":78427,"url":"http:\/\/bangla.sitestree.com\/?p=78427","url_meta":{"origin":78430,"position":1},"title":"Builder Pattern vs \u2699\ufe0f Factory Pattern in Java","author":"Sayed","date":"August 28, 2025","format":false,"excerpt":"From AI Tools\/Open AI\/ChatGPT \" Perfect \ud83d\udc4d Here\u2019s a copyright-free comparison table of the Builder Pattern vs Factory Pattern in Java, styled for easy posting on Facebook\/Blog: \ud83c\udfd7\ufe0f Builder Pattern vs \u2699\ufe0f Factory Pattern in Java AspectBuilder PatternFactory PatternTypeCreational design patternCreational design patternPurposeTo construct complex objects step by step with\u2026","rel":"","context":"In &quot;Java Design Patterns and OOP&quot;","block_context":{"text":"Java Design Patterns and OOP","link":"http:\/\/bangla.sitestree.com\/?cat=1962"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-27.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-27.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-27.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-27.png?resize=700%2C400 2x"},"classes":[]},{"id":78425,"url":"http:\/\/bangla.sitestree.com\/?p=78425","url_meta":{"origin":78430,"position":2},"title":"Factory Design Pattern: Examples in Java","author":"Sayed","date":"August 28, 2025","format":false,"excerpt":"Observe the code below: ShapeFactory factory = new ShapeFactory(); Shape shape1 = factory.getShape(\"circle\"); Shape shape2 = factory.getShape(\"square\"); Shape shape3 = factory.getShape(\"rectangle\"); shape1.draw(); shape2.draw(); shape3.draw(); We wanted to create objects such as shape1, shape2, and shape 3. We did not directly (create and) call the class for the required objects. We\u2026","rel":"","context":"In &quot;Java Design Patterns and OOP&quot;","block_context":{"text":"Java Design Patterns and OOP","link":"http:\/\/bangla.sitestree.com\/?cat=1962"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":22529,"url":"http:\/\/bangla.sitestree.com\/?p=22529","url_meta":{"origin":78430,"position":3},"title":"Resources Checked Recently on PHP, OOP, OOD, Java, Angular, Bootstrap, Design Patterns #Root #By Sayed Ahmed #Misc. Reading","author":"Author-Check- Article-or-Video","date":"March 15, 2021","format":false,"excerpt":"Resources Checked Recently on PHP, OOP, OOD, Java, Angular, Bootstrap, Design Patterns http:\/\/www.fluffycat.com\/PHP-Design-Patterns\/PHP-Job-Interview-Questions\/ http:\/\/php.net\/manual\/en\/language.oop5.object-comparison.php http:\/\/www.programmerinterview.com\/index.php\/design-pattern-questions\/design-pattern-interview-questions-and-answers\/ https:\/\/www.glassdoor.co.in\/Interview\/canada-software-developer-interview-questions-SRCH_IL.0,6_IN3_KO7,25.htm http:\/\/www.tutorialspoint.com\/design_pattern\/decorator_pattern.htm http:\/\/www.fluffycat.com\/PHP-Design-Patterns\/ http:\/\/programmers.stackexchange.com\/questions\/7055\/what-is-the-most-frequently-used-design-pattern http:\/\/salearningschool.com\/displayArticle.php?table=Articles&articleID=893 http:\/\/salearningschool.com\/displayArticle.php?table=Articles&articleID=987 Abstract Factory: https:\/\/sourcemaking.com\/design_patterns\/abstract_factory\/php\/2 Decorator Pattern: http:\/\/www.tutorialspoint.com\/design_pattern\/decorator_pattern.htm Singleton: http:\/\/stackoverflow.com\/questions\/8776788\/best-practice-on-php-singleton-classes http:\/\/stackoverflow.com\/questions\/203336\/creating-the-singleton-design-pattern-in-php5\/203359#203359 https:\/\/ttmm.io\/tech\/making-singletons-safe-in-php\/ Java http:\/\/salearningschool.com\/displayArticle.php?table=Articles&articleID=858 http:\/\/stackoverflow.com\/questions\/9948008\/what-is-sapi-and-when-would-you-use-it Cookie - securing Cookie http:\/\/blog.teamtreehouse.com\/how-to-create-totally-secure-cookies http:\/\/salearningschool.com\/displayArticle.php?table=Articles&articleID=1357&title=PHP%20Security%20-%20Guidelines https:\/\/www.quora.com\/What-is-the-difference-between-htmlentities-and-htmlspecialchars-in-PHP http:\/\/stackoverflow.com\/questions\/46483\/htmlentities-vs-htmlspecialchars Prevent CSRF in PHP http:\/\/stackoverflow.com\/questions\/1780687\/preventing-csrf-in-php PHP Security Package:\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":22527,"url":"http:\/\/bangla.sitestree.com\/?p=22527","url_meta":{"origin":78430,"position":4},"title":"PHP Design Pattern Resources #Root #By Sayed Ahmed #Misc. Reading","author":"Author-Check- Article-or-Video","date":"March 15, 2021","format":false,"excerpt":"http:\/\/www.tutorialspoint.com\/design_pattern\/decorator_pattern.htm http:\/\/www.fluffycat.com\/PHP-Design-Patterns\/ http:\/\/programmers.stackexchange.com\/questions\/7055\/what-is-the-most-frequently-used-design-pattern http:\/\/salearningschool.com\/displayArticle.php?table=Articles&articleID=893 http:\/\/salearningschool.com\/displayArticle.php?table=Articles&articleID=987 Abstract Factory: https:\/\/sourcemaking.com\/design_patterns\/abstract_factory\/php\/2 Decorator Pattern: http:\/\/www.tutorialspoint.com\/design_pattern\/decorator_pattern.htm Singleton: http:\/\/stackoverflow.com\/questions\/8776788\/best-practice-on-php-singleton-classes http:\/\/stackoverflow.com\/questions\/203336\/creating-the-singleton-design-pattern-in-php5\/203359#203359 https:\/\/ttmm.io\/tech\/making-singletons-safe-in-php\/ Java http:\/\/salearningschool.com\/displayArticle.php?table=Articles&articleID=858 From: http:\/\/sitestree.com\/?p=3921 Categories:Root, By Sayed Ahmed, Misc. ReadingTags: Post Data:2016-09-15 14:08:19 Shop Online: https:\/\/www.ShopForSoul.com\/ (Big Data, Cloud, Security, Machine Learning): Courses: http:\/\/Training.SitesTree.com In Bengali: http:\/\/Bangla.SaLearningSchool.com http:\/\/SitesTree.com 8112223 Canada Inc.\/JustEtc: http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) Shop\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":21642,"url":"http:\/\/bangla.sitestree.com\/?p=21642","url_meta":{"origin":78430,"position":5},"title":"Strategy Pattern: Interface vs Abstract Classes #Web Development #By Sayed Ahmed","author":"Author-Check- Article-or-Video","date":"March 3, 2021","format":false,"excerpt":"Strategy Pattern: https:\/\/en.wikipedia.org\/wiki\/Strategy_pattern \"In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern that enables an algorithm's behavior to be selected at runtime. \" \"The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78430"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78430\/revisions"}],"predecessor-version":[{"id":78432,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78430\/revisions\/78432"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78430"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}