<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Design Pattern — Prototype in Angular - Medium]]></title>
        <description><![CDATA[Design Pattern — Prototype in Angular - Medium]]></description>
        <link>https://medium.com/design-pattern-prototype-in-angular?source=rss----f72374c1c35b---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Design Pattern — Prototype in Angular - Medium</title>
            <link>https://medium.com/design-pattern-prototype-in-angular?source=rss----f72374c1c35b---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 13 Nov 2026 02:46:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/design-pattern-prototype-in-angular" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Design Pattern — Prototype in Angular]]></title>
            <link>https://medium.com/design-pattern-prototype-in-angular/design-pattern-prototype-in-angular-1f6e224bb4f9?source=rss----f72374c1c35b---4</link>
            <guid isPermaLink="false">https://medium.com/p/1f6e224bb4f9</guid>
            <dc:creator><![CDATA[Bulicka Ola]]></dc:creator>
            <pubDate>Tue, 01 Apr 2025 04:17:51 GMT</pubDate>
            <atom:updated>2025-04-01T04:17:51.761Z</atom:updated>
            <content:encoded><![CDATA[<h3>Design Pattern — Prototype in Angular</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qZEGRue5yOwdiws3Z370MA.png" /><figcaption>Image generated by Copilot AI</figcaption></figure><p>Creational Design Pattern are concerned with<strong> the way of creating objects.</strong></p><p>In JavaScript, TypeScript, etc., the way to create an object is by using the <strong>new</strong> operator.</p><p>Creational patterns consist of:<br>1. <a href="https://medium.com/design-patterns-factory-in-angular/design-patterns-factory-simple-factory-in-angular-fa579e3849b2">Factory (Simple Factory)</a><br>2. Factory Method<br>3. Abstract Factory<br>4. Singleton<br>5. Prototype<br>6. <a href="https://medium.com/design-patterns-builder-in-angular/design-patterns-builder-in-angular-c9a05b91385f">Builder</a><br>7. Object Pool</p><p>In this article, I will explain how the <strong>Prototype </strong>design pattern works. You can see and run an example of the <strong>Prototype </strong>pattern in a demo project, which can be found at this link <a href="https://stackblitz.com/~/github.com/KasiaKasia/DESIGN-PATTERNS"><strong>here</strong></a>.</p><p>The Prototype design pattern delegates the cloning process to objects that need it. By defining a common interface for all objects that use cloning. This interface lets you clone an object without coupling your code to the class of that object.</p><p>Copying objects can be useful for many functionalities for example test data, comparing a copy od an object with another object,editing a copy before confirming the save of the original object and many others.</p><p>I will present an example of a user data form. Absolutely, the example may be different. The user enters data into the form. Then clicks the Copy User button. The form code with the cloneUser() method that copies the user object is below:</p><pre>import { Component, inject } from &#39;@angular/core&#39;;<br>import { FormBuilder, FormGroup, ReactiveFormsModule } from &#39;@angular/forms&#39;;<br>import { Address, User } from &#39;./prototype&#39;;<br><br><br>@Component({<br>  selector: &#39;app-prototype&#39;,<br>  standalone: true,<br>  imports: [ReactiveFormsModule],<br>  template: `<br><br>  &lt;form [formGroup]=&quot;userForm&quot;&gt;<br>    &lt;div&gt;<br>        &lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;<br>        &lt;input type=&quot;text&quot; formControlName=&quot;name&quot; id=&quot;name&quot;&gt;<br>    &lt;/div&gt;<br>    &lt;div&gt;<br>        &lt;label for=&quot;age&quot;&gt;Age:&lt;/label&gt;<br>        &lt;input type=&quot;number&quot; formControlName=&quot;age&quot; id=&quot;age&quot;&gt;<br>    &lt;/div&gt;<br>    &lt;div&gt;<br>        &lt;label for=&quot;city&quot;&gt;City:&lt;/label&gt;<br>        &lt;input type=&quot;text&quot; formControlName=&quot;city&quot; id=&quot;city&quot;&gt;<br>    &lt;/div&gt;<br>    &lt;div&gt;<br>        &lt;label for=&quot;street&quot;&gt;Street:&lt;/label&gt;<br>        &lt;input type=&quot;text&quot; formControlName=&quot;street&quot; id=&quot;street&quot;&gt;<br>    &lt;/div&gt;<br><br>    &lt;button type=&quot;button&quot; (click)=&quot;cloneUser()&quot;&gt;Clone User&lt;/button&gt;<br>  &lt;/form&gt;<br><br>  @if (clonedUser) {<br>    &lt;h3&gt;Cloned User&lt;/h3&gt;<br>    &lt;p&gt;Name: {{ clonedUser.getName() }}&lt;/p&gt;<br>    &lt;p&gt;Age: {{ clonedUser.getAge() }}&lt;/p&gt;<br>    &lt;p&gt;City: {{ clonedUser.getAddress().getCity() }}&lt;/p&gt;<br>    &lt;p&gt;Street: {{ clonedUser.getAddress().getStreet() }}&lt;/p&gt;<br>  }`,<br>  styleUrl: &#39;./prototype.component.scss&#39;<br>})<br>export class PrototypeComponent {<br><br>  private formBuilder = inject(FormBuilder);<br>  protected clonedUser!: User;<br>  protected userForm: FormGroup = this.formBuilder.group({<br>    name: [&#39;&#39;],<br>    age: [null],<br>    city: [&#39;&#39;],<br>    street: [&#39;&#39;],<br>  });<br><br><br>  cloneUser() {<br>    const user = new User(<br>      this.userForm.controls[&#39;name&#39;].value,<br>      this.userForm.controls[&#39;age&#39;].value,<br>      new Address(<br>        this.userForm.controls[&#39;city&#39;].value,<br>        this.userForm.controls[&#39;street&#39;].value));<br><br>    this.clonedUser = user.clone();<br><br>    if (user.getName() === this.clonedUser.getName()) {<br>      console.info(&#39;Primitive field values have been carried over to a clone.&#39;);<br>    } else {<br>      console.info(&#39;Primitive field values have not been copied.&#39;);<br>    }<br>  }<br>}</pre><p>The cloneUser() method implements the creation of the new User object. This class describes the user. The address parameter is an instance of the Address class. Both classes implement the Cloneable&lt;T&gt; interface containing the clone() method, which creates a new object of the class in which it is located. The method for the task of creating a copy ( in other words a clone). Below is the implementation of the copy/clone functionality:</p><pre>export interface Cloneable&lt;T&gt; {<br>    clone(): T;<br>}<br><br>export class Address implements Cloneable&lt;Address&gt; {<br>    constructor(<br>        private city: string,<br>        private street: string) { }<br><br>    clone(): Address {<br>        return new Address(this.city, this.street);<br>    }<br><br>    getCity(): string {<br>        return this.city;<br>    }<br>    setCity(city: string) {<br>        this.city = city;<br>    }<br><br>    getStreet(): string {<br>        return this.street;<br>    }<br>    setStreet(street: string) {<br>        this.street = street;<br>    }<br>}<br>export class User implements Cloneable&lt;User&gt; {<br>    constructor(<br>        private name: string,<br>        private age: number,<br>        private address: Address<br>    ) { }<br><br>    clone(): User {<br>        return new User(<br>            this.name,<br>            this.age,<br>            this.address.clone()<br>        );<br>    }<br><br>    getName(): string {<br>        return this.name;<br>    }<br>    setName(name: string) {<br>        this.name = name;<br>    }<br><br>    getAge(): number {<br>        return this.age;<br>    }<br>    setAge(age: number) {<br>        this.age = age;<br>    }<br>    <br>    getAddress(): Address {<br>        return this.address;<br>    }<br>    setAddress(address: Address) {<br>        this.address = address;<br>    }<br>}</pre><p>The following example implements changing the value of a copy/clone. However, the original value will not be changed because the copy/clone instance is at a different address in memory. It is a new object:</p><pre> const original = new Address(&quot;property initialization&quot;, &quot;Street&quot;);<br> let clone = original.clone();<br>    <br> console.info(clone.getCity()); // &quot;property initialization&quot;<br> clone.setCity(&quot;change properties for copy/clone&quot;) ; <br> console.info(original.getCity()); // &quot;property initialization&quot;  <br> console.info(clone.getCity()); // &quot;change properties for copy/clone</pre><p>The structure of the <strong>Prototype </strong>pattern consists of several elements:</p><ol><li>The <strong>Prototype</strong> interface declares the cloning methods. In our case, it is the Cloneable&lt;T&gt; interface.</li><li>The <strong>Concrete Prototype</strong> class implements the cloning method. In addition to copying the original object’s data to the clone. In our case these are the User and Address classes.</li><li>The <strong>Client </strong>creates a new object on which the copy/clone method is called. In our case, it is the PrototypeComponent component.</li></ol><h3>Summary</h3><p>The <strong>Prototype </strong>pattern is used to create new objects based on existing instances instead of creating them from scratch using a constructor. It allows for object cloning, which can be useful when you want to retain an object’s properties and state but need a new instance. In JavaScript, there are other methods for copying objects. I analyzed this in more detail in the article “<strong>Copy — Best Practices — JavaScript and TypeScript</strong>”, link <a href="https://medium.com/copy-best-practices-javascript-and-typescript/copy-best-practices-javascript-and-typescript-4ff8b9905af4">here</a>.</p><p>Materials:</p><ol><li><em>Java. Wzorce projektowe</em>. Translation: Piotr Badarycz. Original title: <em>Java design patterns. A Tutorial. </em>Author: James William Cooper</li><li><a href="https://refactoring.guru/design-patterns/prototype">https://refactoring.guru/design-patterns/prototype</a></li></ol><p>My other articles related to Design Patterns in Angular:</p><p>Structural patterns:<br><a href="https://medium.com/design-patterns-adapter-in-angular/design-patterns-adapter-in-angular-ca018478a93b">Design Pattern — Adapter in Angular</a><br><a href="https://medium.com/design-patterns-composite-in-angular/wzorce-projektowe-kompozytowe-w-angular-445c289e4e9f">Design Pattern — Composite in Angular</a><br><a href="https://medium.com/design-patterns-decorator-in-angular/design-patterns-decorator-in-angular-7957bbdbfe06">Design Pattern — Decorator in Angular</a><br><a href="https://medium.com/design-patterns-fasada/design-patterns-facade-in-angular-90bff55aee48">Design Pattern — Facade in Angular</a><br><a href="https://medium.com/design-patterns-proxy-in-angular/design-patterns-proxy-in-angular-8661c1049252">Design Pattern — Proxy in Angular</a></p><p>Creational patterns:<br><a href="https://medium.com/design-patterns-builder-in-angular/design-patterns-builder-in-angular-c9a05b91385f">Design Pattern — Builder in Angular</a><br><a href="https://medium.com/design-patterns-factory-in-angular/design-patterns-factory-simple-factory-in-angular-fa579e3849b2">Design Pattern — Factory (Simple Factory) in Angular</a></p><p>Behavioral patterns:<br><a href="https://medium.com/design-patterns-chain-of-responsibility-in-angular/design-patterns-chain-of-responsibility-in-angular-5bd0b10b011d">Design Pattern — Chain of Responsibility in Angular</a><br><a href="https://medium.com/design-patterns-iterator-in-angular/design-patterns-iterator-in-angular-9508f3b69d08">Design Pattern — Iterator in Angular</a><br><a href="https://medium.com/design-patterns-memento-in-angular/design-patterns-memento-in-angular-a4d8985f4771">Design Pattern — Memento in Angular</a><br><a href="https://medium.com/design-patterns-observer-in-angular/behavioral-patterns-are-design-patterns-that-deal-with-the-ways-objects-communicate-with-each-other-2b4ff0cba9e5">Design Pattern — Observer in Angular</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f6e224bb4f9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/design-pattern-prototype-in-angular/design-pattern-prototype-in-angular-1f6e224bb4f9">Design Pattern — Prototype in Angular</a> was originally published in <a href="https://medium.com/design-pattern-prototype-in-angular">Design Pattern — Prototype in Angular</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>