{"id":351,"date":"2018-05-03T13:30:14","date_gmt":"2018-05-03T01:30:14","guid":{"rendered":"https:\/\/content.ronella.xyz\/apps\/wordpress\/?p=351"},"modified":"2023-10-13T15:57:15","modified_gmt":"2023-10-13T02:57:15","slug":"creational-design-patterns","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=351","title":{"rendered":"Creational Design Patterns"},"content":{"rendered":"<p>Creational design patterns are a subset of design patterns in software engineering that focus on the <strong>process of object creation<\/strong>. These patterns provide various ways to instantiate objects while hiding the complexities involved in the process. Creational design patterns promote flexibility, reusability, and maintainability in your codebase. In this article, we'll explore some of the most commonly used creational design patterns with java examples.<\/p>\n<h2>Abstract Factory Pattern<\/h2>\n<p>Provides an interface for creating families of related or dependent objects without specifying their concrete classes.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre><code class=\"language-java\">interface IShape {\n    void draw();\n}\n\nabstract class AbstractShapeFactory {\n    abstract IShape getShape(String targetShape);\n}\n\nclass Circle implements IShape {\n    @Override\n    public void draw() {\n        System.out.println(&quot;Drawing circle.&quot;);\n    }\n}\n\nclass Square implements IShape {\n    @Override\n    public void draw() {\n        System.out.println(&quot;Drawing square.&quot;);\n    }\n}\n\nclass Rectangle implements IShape {\n    @Override\n    public void draw() {\n        System.out.println(&quot;Drawing rectangle.&quot;);\n    }\n}\n\nclass NormalShapeFactory extends AbstractShapeFactory {\n    @Override\n    IShape getShape(String targetShape) {\n        return switch(targetShape.toLowerCase()) {\n            case &quot;circle&quot; -&gt; new Circle();\n            case &quot;square&quot; -&gt; new Square();\n            case &quot;rectangle&quot; -&gt; new Rectangle();\n            default -&gt; null;\n        };\n    }\n}\n\nclass RoundedSquare implements IShape {\n    @Override\n    public void draw() {\n        System.out.println(&quot;Drawing rounded square.&quot;);\n    }\n}\n\nclass RoundedRectangle implements IShape {\n    @Override\n    public void draw() {\n        System.out.println(&quot;Drawing rounded rectangle.&quot;);\n    }\n}\n\nclass RoundedShapeFactory extends AbstractShapeFactory {\n    @Override\n    IShape getShape(String targetShape) {\n        return switch(targetShape.toLowerCase()) {\n            case &quot;circle&quot; -&gt; new Circle();\n            case &quot;square&quot; -&gt; new RoundedSquare();\n            case &quot;rectangle&quot; -&gt; new RoundedRectangle();\n            default -&gt; null;\n        };\n    }\n}\n\nclass ShapeFactoryProducer {\n    public static AbstractShapeFactory getFactory(String shapeType){\n        return switch (shapeType.toLowerCase()) {\n            case &quot;rounded&quot; -&gt; new RoundedShapeFactory();\n            default -&gt; new NormalShapeFactory();\n        };\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        AbstractShapeFactory normalShapeFactory = ShapeFactoryProducer.getFactory(&quot;normal&quot;);\n        IShape normalSquare = normalShapeFactory.getShape(&quot;square&quot;);\n        normalSquare.draw();\n\n        AbstractShapeFactory roundedShapeFactory = ShapeFactoryProducer.getFactory(&quot;rounded&quot;);\n        IShape roundedSquare = roundedShapeFactory.getShape(&quot;square&quot;);\n        roundedSquare.draw();\n    }\n}<\/code><\/pre>\n<h2>Builder Pattern<\/h2>\n<p>Separates the construction of a complex object <em>(i.e. requires a lot of constructors and properties)<\/em> from its representation, allowing the same construction process to create different representations.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre><code class=\"language-java\">class Product {\n    private String part1;\n    private String part2;\n\n    public void setPart1(String part1) {\n        this.part1 = part1;\n    }\n\n    public void setPart2(String part2) {\n        this.part2 = part2;\n    }\n\n    public void show() {\n        System.out.println(&quot;Part 1: &quot; + part1);\n        System.out.println(&quot;Part 2: &quot; + part2);\n    }\n}\n\nclass ProductBuilder {\n    private String part1;\n    private String part2;\n\n    public ProductBuilder buildPart1(String part1) {\n        this.part1 = part1;\n        return this;\n    }\n\n    public ProductBuilder buildPart2(String part2) {\n        this.part2 = part2;\n        return this;\n    }\n\n    public Product build() {\n        Product product = new Product();\n        product.setPart1(part1);\n        product.setPart2(part2);\n        return product;\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        ProductBuilder builder = new ProductBuilder();\n        Product product = builder\n            .buildPart1(&quot;Part 1&quot;)\n            .buildPart2(&quot;Part 2&quot;)\n            .build();\n\n        product.show();\n    }\n}<\/code><\/pre>\n<h2>Factory Pattern<\/h2>\n<p>Defines an interface for creating an object but allows subclasses to alter the type of objects that will be created.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre><code class=\"language-java\">interface IShape {\n    void draw();\n}\n\ninterface IShapeFactory {\n    IShape createShape(String shapeType);\n}\n\nclass Circle implements IShape {\n    @Override\n    public void draw() {\n        System.out.println(&quot;Drawing circle.&quot;);\n    }\n}\n\nclass Rectangle implements IShape {\n    @Override\n    public void draw() {\n        System.out.println(&quot;Drawing rectangle.&quot;);\n    }\n}\n\nclass ShapeFactory implements IShapeFactory {\n    @Override\n    public IShape createShape(String shapeType) {\n        if (shapeType.equals(&quot;circle&quot;)) {\n            return new Circle();\n        }\n        else if (shapeType.equals(&quot;rectangle&quot;)) {\n            return new Rectangle();\n        }\n        else {\n            throw new IllegalArgumentException(&quot;Invalid shape type: &quot; + shapeType);\n        }\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        ShapeFactory shapeFactory = new ShapeFactory();\n        IShape shape = shapeFactory.createShape(&quot;circle&quot;);\n        System.out.println(shape);\n    }\n}<\/code><\/pre>\n<h2>Lazy Initialization Pattern<\/h2>\n<p>Delays the creation of an object or the calculation of a value until it is actually needed.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre><code class=\"language-java\">class LightObject1 {\n    public LightObject1() {\n        System.out.println(&quot;LightObject1 initialized&quot;);\n    }\n}\n\nclass LightObject2 {\n    public LightObject2() {\n        System.out.println(&quot;LightObject2 initialized&quot;);\n    }\n}\n\nclass ExpensiveObject {\n    public ExpensiveObject() {\n        \/\/ Simulate a time-consuming initialization process\n        try {\n            Thread.sleep(2000); \/\/ Sleep for 2 seconds to simulate initialization time\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n        System.out.println(&quot;ExpensiveObject initialized&quot;);\n    }\n}\n\nclass LazyInitialization {\n    \/\/ Lightweight objects to be instantiated in the constructor\n    private LightObject1 lightObject1;\n    private LightObject2 lightObject2;\n\n    \/\/ Declare a private field to hold the lazily initialized object\n    private ExpensiveObject expensiveObject;\n\n    public LazyInitialization() {\n        \/\/ Instantiate the lightweight objects in the constructor\n        lightObject1 = new LightObject1();\n        lightObject2 = new LightObject2();\n    }\n\n    \/\/ This method initializes and returns the expensive object on-demand\n    public ExpensiveObject getExpensiveObject() {\n        if (expensiveObject == null) {\n            \/\/ Lazy initialization: Create the expensive object when it&#039;s first requested\n            expensiveObject = new ExpensiveObject();\n        }\n        return expensiveObject;\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        LazyInitialization example = new LazyInitialization();\n\n        \/\/ At this point, the lightweight objects are already instantiated,\n        \/\/ but the expensive object has not been created yet\n        System.out.println(&quot;Lightweight objects initialized&quot;);\n        System.out.println(&quot;Expensive object not yet initialized&quot;);\n\n        \/\/ When needed, get the expensive object\n        ExpensiveObject obj = example.getExpensiveObject();\n\n        \/\/ Now, the expensive object has been initialized\n        System.out.println(&quot;Expensive object initialized&quot;);\n    }\n}<\/code><\/pre>\n<h2>Prototype Pattern<\/h2>\n<p>Creates new objects by copying an existing object, known as the prototype, rather than instantiating new objects using constructors.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre><code class=\"language-java\">class Prototype implements Cloneable {\n    private String name;\n\n    public Prototype(String name) {\n        this.name = name;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    @Override\n    public Prototype clone() throws CloneNotSupportedException {\n        return (Prototype) super.clone();\n    }\n}\n\npublic class Main {\n\n    public static void main(String ... args) {\n        Prototype prototype1 = new Prototype(&quot;Prototype 1&quot;);\n\n        try {\n            Prototype prototype2 = prototype1.clone();\n            System.out.println(&quot;Clone: &quot; + prototype2.getName()); \/\/ Output: Clone: Prototype 1\n        } catch (CloneNotSupportedException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<h2>Singleton Pattern<\/h2>\n<p>Ensures that a class has only one instance and provides a global point of access to that instance.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre><code class=\"language-java\">final class Singleton {\n    private static Singleton instance;\n\n    private Singleton() {\n        \/\/ Private constructor to prevent instantiation\n    }\n\n    public static Singleton getInstance() {\n        if (instance == null) {\n            instance = new Singleton();\n        }\n        return instance;\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        Singleton singleton1 = Singleton.getInstance();\n        System.out.println(singleton1);\n        Singleton singleton2 = Singleton.getInstance();\n        System.out.println(singleton2);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Creational design patterns are a subset of design patterns in software engineering that focus on the process of object creation. These patterns provide various ways to instantiate objects while hiding the complexities involved in the process. Creational design patterns promote flexibility, reusability, and maintainability in your codebase. In this article, we&#8217;ll explore some of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/351"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=351"}],"version-history":[{"count":15,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":1628,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions\/1628"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}