{"id":78425,"date":"2025-08-28T16:59:52","date_gmt":"2025-08-28T16:59:52","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78425"},"modified":"2025-08-28T16:59:53","modified_gmt":"2025-08-28T16:59:53","slug":"factory-design-pattern-examples-in-java","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78425","title":{"rendered":"Factory Design Pattern: Examples in Java"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><strong>Observe the code below:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        ShapeFactory factory = new ShapeFactory();\n\n        Shape shape1 = factory.getShape(\"circle\");\n        Shape shape2 = factory.getShape(\"square\");\n        Shape shape3 = factory.getShape(\"rectangle\");\n\n        shape1.draw();\n        shape2.draw();\n        shape3.draw();<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>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 did not create or call a circle class, or a square class, or a rectangle class. We rather thought there was a factory class that could create objects based on our requirements. We asked the factory to create a circle object, then it gave us a circle object. In the same way, we asked for square and rectangle objects from the factory class, and the class returned us square and rectangular objects.<br><\/p>\n\n\n\n<p><strong>This is about: Factory Design Pattern: Examples are in Java<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>From AI Tools\/OpenAI\/Internet<\/p>\n\n\n\n<p>&#8220;<\/p>\n\n\n\n<p>Perfect \ud83d\udc4d \u2014 here\u2019s a <strong>copyright-free<\/strong> write-up of the <strong>Factory Pattern<\/strong> in Java that matches the same style as the Builder Pattern post, so you can share both on <strong>Facebook or your blog<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f Java Design Patterns \u2013 Factory Pattern<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 Definition<\/h3>\n\n\n\n<p>The <strong>Factory Pattern<\/strong> is a <strong>creational design pattern<\/strong> that provides a way to create objects <strong>without exposing the creation logic<\/strong> to the client. Instead of directly calling a constructor, you use a factory method that decides which object to return.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 Use Cases<\/h3>\n\n\n\n<p>You should use the Factory Pattern when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You want to <strong>delegate object creation<\/strong> to a central place.<\/li>\n\n\n\n<li>The exact type of object needed is <strong>determined at runtime<\/strong>.<\/li>\n\n\n\n<li>You want to reduce <strong>tight coupling<\/strong> between classes.<\/li>\n\n\n\n<li>Multiple subclasses share a common interface but have different implementations.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 Advantages<\/h3>\n\n\n\n<p>\u2714\ufe0f <strong>Encapsulation of object creation<\/strong> logic.<br>\u2714\ufe0f Makes code <strong>easier to extend<\/strong> when adding new object types.<br>\u2714\ufe0f Promotes <strong>loose coupling<\/strong> (client depends on interface, not implementation).<br>\u2714\ufe0f Simplifies <strong>maintenance and testing<\/strong>.<br>\u2714\ufe0f Encourages <strong>code reuse<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 Example in Java<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Product Interface\ninterface Shape {\n    void draw();\n}\n\n\/\/ Concrete Products\nclass Circle implements Shape {\n    public void draw() {\n        System.out.println(\"Drawing a Circle.\");\n    }\n}\n\nclass Square implements Shape {\n    public void draw() {\n        System.out.println(\"Drawing a Square.\");\n    }\n}\n\nclass Rectangle implements Shape {\n    public void draw() {\n        System.out.println(\"Drawing a Rectangle.\");\n    }\n}\n\n\/\/ Factory Class\nclass ShapeFactory {\n    public Shape getShape(String shapeType) {\n        if (shapeType == null) return null;\n        switch (shapeType.toLowerCase()) {\n            case \"circle\": return new Circle();\n            case \"square\": return new Square();\n            case \"rectangle\": return new Rectangle();\n            default: return null;\n        }\n    }\n}\n\n\/\/ Usage\npublic class FactoryPatternExample {\n    public static void main(String&#91;] args) {\n        ShapeFactory factory = new ShapeFactory();\n\n        Shape shape1 = factory.getShape(\"circle\");\n        Shape shape2 = factory.getShape(\"square\");\n        Shape shape3 = factory.getShape(\"rectangle\");\n\n        shape1.draw();\n        shape2.draw();\n        shape3.draw();\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u2705 <strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Drawing a Circle.\nDrawing a Square.\nDrawing a Rectangle.\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udccc <strong>In short:<\/strong><br>The Factory Pattern lets you <strong>create objects without knowing their exact class<\/strong>. It simplifies code, promotes <strong>loose coupling<\/strong>, and makes it easier to manage <strong>different object types<\/strong> from a single point.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udc49 Now that we have both <strong>Builder<\/strong> and <strong>Factory<\/strong>, would you like me to also prepare <strong>a comparison table (Builder vs Factory)<\/strong> in the same copyright-free format for FB\/Blog? That way your readers can instantly see the differences.<\/p>\n\n\n\n<p>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Observe the code below: 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 did not create or call a circle class, or a square class, or a rectangle class. We rather thought there was a factory class that &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=78425\">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-78425","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":78427,"url":"http:\/\/bangla.sitestree.com\/?p=78427","url_meta":{"origin":78425,"position":0},"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":78099,"url":"http:\/\/bangla.sitestree.com\/?p=78099","url_meta":{"origin":78425,"position":1},"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":26567,"url":"http:\/\/bangla.sitestree.com\/?p=26567","url_meta":{"origin":78425,"position":2},"title":"Example illustrating inheritance and abstract classes #Programming Code Examples #Java\/J2EE\/J2ME #Object Oriented Programming","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"*********************************** # Example illustrating inheritance and abstract classes. * Shape.java The parent class (abstract) for all closed, open, curved, and straight-edged shapes. * Curve.java An (abstract) curved Shape (open or closed). * StraightEdgedShape.java A Shape with straight edges (open or closed). * Measurable.java Interface defining classes with measurable 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":[]},{"id":78430,"url":"http:\/\/bangla.sitestree.com\/?p=78430","url_meta":{"origin":78425,"position":3},"title":"Factory Pattern vs \ud83c\udfed Abstract Factory Pattern in Java","author":"Sayed","date":"August 28, 2025","format":false,"excerpt":"Abstract Factory Pattern Example in Java From: AI Tools\/OpenAI\/Chatgpt \" 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\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-28.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-28.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-28.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-28.png?resize=700%2C400 2x"},"classes":[]},{"id":26860,"url":"http:\/\/bangla.sitestree.com\/?p=26860","url_meta":{"origin":78425,"position":4},"title":"Multithreaded Graphics and Double Buffering #Programming Code Examples #Java\/J2EE\/J2ME #Java Threads","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"ShipSimulation.java Illustrates the basic approach of multithreaded graphics whereas a thread adjusts parameters affecting the appearance of the graphics and then calls repaint to schedule an update of the display. import java.applet.Applet; import java.awt.*; public class ShipSimulation extends Applet implements Runnable { ... public void run() { Ship s; for(int\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":10541,"url":"http:\/\/bangla.sitestree.com\/?p=10541","url_meta":{"origin":78425,"position":5},"title":"Example illustrating inheritance and abstract classes","author":"","date":"August 29, 2015","format":false,"excerpt":"*********************************** # Example illustrating inheritance and abstract classes. \u00a0\u00a0\u00a0 * Shape.java The parent class (abstract) for all closed, open, curved, and straight-edged shapes. \u00a0\u00a0\u00a0 * Curve.java An (abstract) curved Shape (open or closed). \u00a0\u00a0\u00a0 * StraightEdgedShape.java A Shape with straight edges (open or closed). \u00a0\u00a0\u00a0 * Measurable.java Interface defining classes\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78425","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=78425"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78425\/revisions"}],"predecessor-version":[{"id":78426,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78425\/revisions\/78426"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78425"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}