{"id":1180,"date":"2022-08-28T22:16:00","date_gmt":"2023-06-04T08:41:05","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/the-java-language-implements-the-prototype-design-pattern\/"},"modified":"2024-03-13T15:52:28","modified_gmt":"2024-03-13T15:52:28","slug":"the-java-language-implements-the-prototype-design-pattern","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/","title":{"rendered":"The Java language implements the Prototype Design Pattern."},"content":{"rendered":"<p>The Prototype design pattern falls under the category of Creational Design patterns, offering a means of creating objects.<\/p>\n<h2>The Prototype Design Pattern is a widely used pattern for designing prototypes.<\/h2>\n<p>The Prototype design pattern is employed when creating an Object is expensive in terms of time and resources, but there is already a similar object available. This pattern allows us to duplicate the original object and make necessary modifications as per our requirements. The <a href=\"https:\/\/www.java.com\/\">Java<\/a> cloning technique is used in the Prototype design pattern for object duplication.<\/p>\n<h3>An example of the Prototype Design Pattern.<\/h3>\n<p>Understanding the prototype design pattern can be made easier through an example. Consider an Object that retrieves data from a database. If we need to make multiple modifications to this data in our program, creating a new Object using the &#8220;new&#8221; keyword and reloading all the data from the database each time is not efficient. A better approach would be to clone the existing object into a new object and then manipulate the data. The prototype design pattern requires that the Object being copied should provide the cloning feature, and it should not be done by any other class. However, whether a shallow or deep copy of the Object properties should be used depends on the requirements and a design decision. The following is a Java program that demonstrates an example of the prototype design pattern using the Employees.java class.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.design.prototype;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\npublic class Employees implements Cloneable{\r\n\r\n\tprivate List&lt;String&gt; empList;\r\n\t\r\n\tpublic Employees(){\r\n\t\tempList = new ArrayList&lt;String&gt;();\r\n\t}\r\n\t\r\n\tpublic Employees(List&lt;String&gt; list){\r\n\t\tthis.empList=list;\r\n\t}\r\n\tpublic void loadData(){\r\n\t\t\/\/read all employees from database and put into the list\r\n\t\tempList.add(\"Pankaj\");\r\n\t\tempList.add(\"Raj\");\r\n\t\tempList.add(\"David\");\r\n\t\tempList.add(\"Lisa\");\r\n\t}\r\n\t\r\n\tpublic List&lt;String&gt; getEmpList() {\r\n\t\treturn empList;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic Object clone() throws CloneNotSupportedException{\r\n\t\t\tList&lt;String&gt; temp = new ArrayList&lt;String&gt;();\r\n\t\t\tfor(String s : this.getEmpList()){\r\n\t\t\t\ttemp.add(s);\r\n\t\t\t}\r\n\t\t\treturn new Employees(temp);\r\n\t}\r\n\t\r\n}\r\n<\/code><\/pre>\n<p>Take note that the clone method has been redefined in order to create a thorough replica of the employees list. Below is an instance of a test program for the prototype design pattern that illustrates the advantages of the prototype pattern. The name of the file is PrototypePatternTest.java.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.design.test;\r\n\r\nimport java.util.List;\r\n\r\nimport com.scdev.design.prototype.Employees;\r\n\r\npublic class PrototypePatternTest {\r\n\r\n\tpublic static void main(String[] args) throws CloneNotSupportedException {\r\n\t\tEmployees emps = new Employees();\r\n\t\temps.loadData();\r\n\t\t\r\n\t\t\/\/Use the clone method to get the Employee object\r\n\t\tEmployees empsNew = (Employees) emps.clone();\r\n\t\tEmployees empsNew1 = (Employees) emps.clone();\r\n\t\tList&lt;String&gt; list = empsNew.getEmpList();\r\n\t\tlist.add(\"John\");\r\n\t\tList&lt;String&gt; list1 = empsNew1.getEmpList();\r\n\t\tlist1.remove(\"Pankaj\");\r\n\t\t\r\n\t\tSystem.out.println(\"emps List: \"+emps.getEmpList());\r\n\t\tSystem.out.println(\"empsNew List: \"+list);\r\n\t\tSystem.out.println(\"empsNew1 List: \"+list1);\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>The result of the program using the given prototype design pattern example is:<\/p>\n<pre class=\"post-pre\"><code>emps List: [Pankaj, Raj, David, Lisa]\r\nempsNew List: [Pankaj, Raj, David, Lisa, John]\r\nempsNew1 List: [Raj, David, Lisa]\r\n<\/code><\/pre>\n<p>If object cloning was not available, we would need to fetch the employee list from the database every time and perform resource-intensive manipulations. This concludes the prototype design pattern in Java.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>more\u00a0 tutorials<\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/the-command-design-pattern\/\" target=\"_blank\" rel=\"noopener\">The Command design pattern.<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/example-tutorial-for-the-strategy-design-pattern-in-java\/\" target=\"_blank\" rel=\"noopener\">Strategy Design Pattern in Java tutorial<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/ensuring-thread-safety-in-java-singleton-classes\/\" target=\"_blank\" rel=\"noopener\">Ensuring thread safety in Java Singleton Classes<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-include-items-to-a-list-in-python\/\" target=\"_blank\" rel=\"noopener\">How to include items to a list in Python<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/one-option-for-paraphrasing-the-given-phrase-could-be-different-server-configurations-frequently-used-for-your-web-application\/\" target=\"_blank\" rel=\"noopener\">Server Configurations Frequently Used for Your Web Application<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Prototype design pattern falls under the category of Creational Design patterns, offering a means of creating objects. The Prototype Design Pattern is a widely used pattern for designing prototypes. The Prototype design pattern is employed when creating an Object is expensive in terms of time and resources, but there is already a similar object [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1180","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The Java language implements the Prototype Design Pattern. - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"The Prototype design pattern is employed when creating an Object is expensive in terms of time and resources, but there is already a similar\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Java language implements the Prototype Design Pattern.\" \/>\n<meta property=\"og:description\" content=\"The Prototype design pattern is employed when creating an Object is expensive in terms of time and resources, but there is already a similar\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-04T08:41:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-13T15:52:28+00:00\" \/>\n<meta name=\"author\" content=\"Ava Mitchell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ava Mitchell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/\"},\"author\":{\"name\":\"Ava Mitchell\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64\"},\"headline\":\"The Java language implements the Prototype Design Pattern.\",\"datePublished\":\"2023-06-04T08:41:05+00:00\",\"dateModified\":\"2024-03-13T15:52:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/\"},\"wordCount\":406,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/\",\"name\":\"The Java language implements the Prototype Design Pattern. - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2023-06-04T08:41:05+00:00\",\"dateModified\":\"2024-03-13T15:52:28+00:00\",\"description\":\"The Prototype design pattern is employed when creating an Object is expensive in terms of time and resources, but there is already a similar\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Java language implements the Prototype Design Pattern.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64\",\"name\":\"Ava Mitchell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g\",\"caption\":\"Ava Mitchell\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/avamitchell\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"The Java language implements the Prototype Design Pattern. - Blog - Silicon Cloud","description":"The Prototype design pattern is employed when creating an Object is expensive in terms of time and resources, but there is already a similar","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/","og_locale":"en_US","og_type":"article","og_title":"The Java language implements the Prototype Design Pattern.","og_description":"The Prototype design pattern is employed when creating an Object is expensive in terms of time and resources, but there is already a similar","og_url":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2023-06-04T08:41:05+00:00","article_modified_time":"2024-03-13T15:52:28+00:00","author":"Ava Mitchell","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Ava Mitchell","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/"},"author":{"name":"Ava Mitchell","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64"},"headline":"The Java language implements the Prototype Design Pattern.","datePublished":"2023-06-04T08:41:05+00:00","dateModified":"2024-03-13T15:52:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/"},"wordCount":406,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/","url":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/","name":"The Java language implements the Prototype Design Pattern. - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2023-06-04T08:41:05+00:00","dateModified":"2024-03-13T15:52:28+00:00","description":"The Prototype design pattern is employed when creating an Object is expensive in terms of time and resources, but there is already a similar","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/the-java-language-implements-the-prototype-design-pattern\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"The Java language implements the Prototype Design Pattern."}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64","name":"Ava Mitchell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g","caption":"Ava Mitchell"},"url":"https:\/\/www.silicloud.com\/blog\/author\/avamitchell\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=1180"}],"version-history":[{"count":0,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1180\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=1180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=1180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=1180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}