4-) Prototype Design Pattern

recep orhan
Geek Culture
Published in
5 min readAug 3, 2021

--

I’ll explain to the fourth creational design pattern prototype. Before going on, I’ll talk about “stack-heap memory, value-reference types, mutable-immutable objects, shallow-deep copy” subjects to understand better.

Stack-Heap Memory

When developing an application, memory is allocated according to used objects and variables in any software development language. In runtime, created object in the method scope, continuous to exist. When the end of the method is reached, deleted the created objects and memory is freed. Memory is made reusable.

Memory is divided into two parts for more effective use. Different variable types are stored at different types at these parts.

Value-Reference Types

There are two variable types value-reference in dot net. Value types are derived from structure types. bool, byte, char, decimal, double, enum, float, int, long, sbyte, short, struct, uint, ulong, ushort are value type examples. When they are used alone, stored in the stack memory. In addition to they, enumerations are stored in the stack too. value1, value2, value3, and value4 are stored in the stack, in the code below.

Inherited types from System. Objects such as class, interface, delegate, record, dynamic, object are reference types and stored in the heap memory.

Int is a value type but in this code used as class property and turned into the reference type. It is stored in the heap memory for this use.

When reference types are assigned to each other, Pointers in the stack memory become different but the value in the heap memory becomes the same. If changed one of them value this affects the other.

I created a class whose name is Numbers. After getting an instance with name group1 I set the value. I created a new instance from the Number class whose name is group2. I assigned group2 to group1 with the “=” operator. In this phase, there are two different fields in the stack memory which point to the same value in the heap memory. When I changed the group1 value this affect group2. Object.Equals(group1, group2) methods returned the true. On the other side, I created two int value type and assigned to each other with “=” operator. Then I changed the first int value but not changed the other value. Because each other is stored in different fields in stack memory. Equals(a, b) returned false. I run the console application.

Mutable-Immutable Objects

Mutable means changeable and immutable means not changeable. When we make any change in a string that is an immutable object, becomes new allocations in the memory. The old field is cleared by GC sometime later. If used a loop and change string value in this loop, new strings are created as many as the number of entries to the loop. Instead, StringBuilder is used. StringBuilder allows changing string instead of new memory allocation.

Let’s create a console application afterward examine the memory when using string changing and StringBuilder.

Firstly, I used StringBuilder. When debugging the app, Memory usage was seen 8.6 Mb in the Diagnostic Tools.

I commented the StringBuilder and run string changing. This time 12 Mb memory usage was seen. Also, GC worked thousands of times in 1,2 s. First time compiler reached the Console.Writeline line in 200ms but second time 1000ms. Performance differences are clearly seen.

Classes are can be created as the immutable object too

Value only sets the NumberClass when getting an instance via the constructor. Afterward, can get value with the GetNumber method.

Shallow-Deep Copy

When we get a copy of a class we can choose two choices, shallow and deep copy. Shallow copy only the value type property of the class. If there are child objects, they are not copied, only a new address is created in the stack which points to the field of the object in the heap. Deep copy objects all of the members with the created new instance. I will give an example under the prototype design pattern heading.

Prototype Design Pattern

This design pattern concern about copy to objects.

I created BlogPost and Category classes. BlogPost has properties that types are value and reference. There were Shallow and Deep Copy methods. I printed to screen the class property values with override the ToString() method.

In the Main method of the console application, I got an instance of BlogPost then filled the property values. I got two copies with ShallowCopy and DeepCopy methods. At this stage, I have 3 instances for the BlogPost class. After printed the values with ToString method, I changed values the first instance. When printing the property values again we can see the difference between ShallowCopy and DeepCopy.

When run the app output will be this.

I changed all properties of blog1. In the ShallowCopy prototype Header, CreateTime, Status properties have not changed. But the BlogCategory which is a child object changed with the original instance. The DeepCopy prototype didn’t change any of the properties.

When need to copy the class, we can use the Prototype design pattern. Also according to the class structure that we have to copy we can select shallow or deep copy.

You can examine my github repository about design patterns.

https://github.com/orhanrecep90/DesignPatterns

--

--