{"id":2912,"date":"2023-08-11T16:05:37","date_gmt":"2023-08-11T16:05:37","guid":{"rendered":"https:\/\/msgprogramator.sk\/?p=2912"},"modified":"2025-07-07T10:57:26","modified_gmt":"2025-07-07T10:57:26","slug":"java-prototype","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/java-prototype\/","title":{"rendered":"Java design pattern Prototype"},"content":{"rendered":"<p>Today we will take a look at another <strong>Java<\/strong> <strong>design pattern<\/strong> from the category of patterns for creating objects <strong>(creational patterns)<\/strong> &#8211; Prototype.<\/p>\n<p>Read more about other design patterns:<\/p>\n<ul>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-builder\/\">Builder design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-singleton\/\">Singleton design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-factory\/\">Factory design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-abstract-factory\/\">Abstract Factory design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-adapter\/\">Adapter design pattern<\/a><\/li>\n<\/ul>\n<h2>What is the Prototype design pattern?<\/h2>\n<p>The <strong>Prototype <\/strong> <em>design pattern<\/em> is a software design pattern used to create new objects by copying an existing object.<\/p>\n<h2>What problem does the Prototype design pattern solve?<\/h2>\n<p>It solves the problem of creating new objects with minimal cost in computing time and memory by avoiding the repeated creation of objects using the constructor. Instead, we create an example instance (prototype) that we can then copy when we need to create a new object. <\/p>\n<p>This design pattern is useful when object creation is difficult, or when getting new instances of an object is different from simply calling its constructor. It is also useful for making deep copies of objects that contain complex data structures. <\/p>\n<h2>Example of Prototype design pattern implementation in Java<\/h2>\n<p>Now we will show you how to implement the Prototype pattern in Java.<\/p>\n<p><strong><u>Shape.java<\/u><\/strong><\/p>\n<p>The shape class represents a Prototype pattern that needs to implement the <em>Cloneable <\/em> interface and overload the <em>clone()<\/em> method.<\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic class Shape implements Cloneable {\n\n    private String type;\n\n    public Shape(String type) {\n        this.type = type;\n    }\n\n    public String getType() {\n        return type;\n    }\n\n    public void setType(String type) {\n        this.type = type;\n    }\n\n    @Override\n    public Shape clone() throws CloneNotSupportedException {\n        return (Shape) super.clone();\n    }\n}<\/code><\/pre>\n<p>Here is an example of using the Shape class with the Prototype design pattern implementation:<\/p>\n<p><strong><u>Main.java<\/u><\/strong><\/p>\n<p>In this example, we have created a Shape class, which represents a prototype object. In the main method, we first created a prototype instance of type Circle. We then copied this instance and created the Shape1 object. After changing the type of Shape1, we copied the pattern again and created the Shape2 object.   <\/p>\n<pre><code class=\"language-java\" data-line=\"\">import designpatterns.Shape;\n\npublic class Main {\n    public static void main(String[] args) {\n        \/\/ Shape creation\n        Shape prototype = new Shape(&quot;Circle&quot;);\n\n        try {\n            \/\/ Clone shape to create a new object\n            Shape shape1 = prototype.clone();\n            System.out.println(&quot;Object 1: &quot; + shape1.getType());\n\n            \/\/ Changing the type of new object\n            shape1.setType(&quot;Square&quot;);\n            System.out.println(&quot;Object 1 after type change: &quot; + shape1.getType());\n\n            \/\/ Creating another object by cloning the shape\n            Shape shape2 = prototype.clone();\n            System.out.println(&quot;Object 2: &quot; + shape2.getType());\n        } catch (CloneNotSupportedException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<p>The output of this example should be like this:<\/p>\n<figure id=\"attachment_4385\" aria-describedby=\"caption-attachment-4385\" style=\"width: 525px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4384\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/08\/prototype-design-pattern-eng.webp\" alt=\"prototype design pattern program output\" width=\"525\" height=\"256\" srcset=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/08\/prototype-design-pattern-eng.webp 641w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/08\/prototype-design-pattern-eng-300x146.webp 300w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><figcaption id=\"caption-attachment-4385\" class=\"wp-caption-text\">output of the prototype example<\/figcaption><\/figure>\n<p>We have prepared files with the above example prototype code that you can run directly in Java. <a href=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/09\/Prototype_Eng.zip\">Download the code here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the Prototype design pattern and what is it for? Read our new article.<\/p>\n","protected":false},"author":14,"featured_media":1863,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-2912","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/comments?post=2912"}],"version-history":[{"count":3,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2912\/revisions"}],"predecessor-version":[{"id":5206,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2912\/revisions\/5206"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/1863"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=2912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=2912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=2912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}