Hey we are now the last video of this section prototype design pattern.
Previously we looked at Abstract Factory.
In this video we will build a small component of an imaginary customized shirt shop that will have a
few shirts with their default colors and prices.
Each shirt will also have a stock keeping unit to identify items stored at a specific location.
The prototype pattern we will use an already created instance of some type to clone it and complete
it with the particular needs of each context.
The aim of the prototype pattern is to have an object or a set of objects that is already created at
compilation time but which you can clone as many times as you want at runtime.
This is useful for example as a default template for a user who has just registered with your web page
or a default pricing plan in some service.
The key difference between this and a builder pattern is that objects are cloned for the user.
Instead of building them at runtime You can also build a cache like solution storing information using
a prototype.
The main objective for the prototype design pattern is to avoid repetitive object creation.
Imagine a default object composed of dozens of fields and embedded types.
We don't want to write everything needed by this type.
Every time that we use the object especially if we can mess it up by creating instances with different
foundations.
So we need to maintain a set of objects that will be cloned to create new instances and provide a default
value of some type to start working on top of it.
And lastly free CPSU of complex object initialization to take more memory resources.
We will build a small component of an imaginary customised shirts shop that will have a few shirts with
their default colours and prices.
Each shirt will also have a stock keeping unit that is S.K. EU which is a system to identify items stored
at a specific location that will need an update to achieve what is described in the example we will
use a prototype of shirts.
Each time we need a new shirt we will take this prototype clone it and work with it.
In particular those are the acceptance criteria for using the prototype pattern design method.
In this example to have a shirt clone or object and interface to ask for different types of shirts white
black and blue at fifteen point zero sixteen point zero and seventeen point zero dollars respectively.
When you're asked for a white shirt a clone of the white shirt must be made and the new instance must
be different from the original one.
The SCA you've created object shouldn't affect a new object creation an info method must give me all
the information available on the instance fields including the updated SDK you.
Now let's start with unit test.
First we will need a shirt clone or interface and an object that implements it.
Also we need a package level function called get shirts closer to retrieve a new instance of the clone.
So I have created a folder named prototype within the creation or folder and here I have created two
files and named them as prototype dot go and prototype underscored test dot GO LET'S OPEN THE PROTOTYPE
dot go file.
Here we are the creation or package and import errors.
Now we add shirt cleaner interface and shirt cache structure.
Now we need an object struct to clone which implements an interface to retrieve the information of its
own fields.
We will call the object shirt and the item info getter interface.
So here's the code which you need to add to this file.
Now have you realized that the type code shirt color that we defined is just a byte type.
Maybe you are wondering why we haven't simply used the byte type.
We could but this way we created an easily readable struct which we can upgrade with some methods in
the future if required.
For example we could write a string method that returns the color in the string format that is white
for type 1 black for type 2 and blue for Type 3.
Now let's save this file and move the prototype test dot go and let's write our first test.
Here we first import testing and here's the code which we'll be using for our first tests.
We will cover the first case of our scenario where we need a clone or object that we can use to ask
for different shirt colors for the second case.
We will take the original object which we can access because we are in the scope of the package and
we will compare it with our t shirt one instance.
Let's do it now for the third case.
First we will type assert item 1 to a shirt so we can set an SCA you.
We will create a second shirt.
Also white and we will type asserted to to check that the SCA use are different.
Let me add the lines of code for this.
Finally for the fourth case we log the invoke the First and Second shirts.
So we do this with T dot log F..
Let's add the line of code to print the memory positions of both shirts.
So we make this assertion at a more physical level.
Now save the file and move on to the terminal.
Finally we run the tests so that we can check to see if it fails.
We have to stop there so that the tests don't panic.
If we tried to use a nil object that is returned by the get shirts closure function.
Now we step ahead for implementation open the prototype dot go file.
We will start with the Get clone method.
This method should return an item of the specified type and we have three types.
White black and blue.
So now that we have the three prototypes to work over we can implement get clone s and method.
The shirt structure which you see highlighted also needs a Get Info implementation to print the contents
of the instances.
Now move back to the terminal.
Finally let's run the tests to see that everything is now working.
Remember to set the hyphen v flag when running the tests in the log.
You can check that shirt 1 and shirt to have different SDK use.
Also we can see the memory positions of both objects.
Take into account that the position shown on your computer will probably be different.
Cool.
That's all for this video.
Let us summarize what we learned about the prototype design pattern the prototype pattern is a powerful
tool to build caches and default objects.
You have probably realized too that some patterns can overlap a bit but they have small differences
that make them more appropriate in some cases and not so much in others in this section.
We have seen five main creation or design patterns commonly used in the software industry.
The purpose is to abstract the user from the creation of objects the complexity or maintain ability
purposes.
They have been the foundation of thousands of applications and libraries since the 1990s and most of
the software we use today has many of these creation or patterns under the hood.
It's worth mentioning that these patterns on not thread free in more advanced sanctions which we will
include in the third part of this course.
We will see concurrent programming and go and how to create some of the more critical design patterns
using a concurrent approach in the next section.
We will explore about structural patterns.