Design Patterns In WinJS #5: Prototype

I've decided to write more OOP articles in WinJS library regarding the use of Design Patterns we all love! You can learn more in my previous parts:
Let's start with what this design pattern means.
 
What is Prototype Design Pattern?

Prototype helps you create prototype instances of the objects and lets you clone them. It's widely used in Javascript especially in jQuery library.

According to DoFactory,a prototype factory has three objects:


  • Client stage: The execution of the creation of objects.User creates the prototype object here.
  • Prototype: A class derived from another class with clone function in which you can use same object in another place.
  • Clones: The objects created from Prototype.
Now let's make an example Prototype diagram:



My Prototype example has the same amount of objects as DoFactory's.

Professional Object
ProfessionalPrototype Object
Clone Object


We shall create Professionals from Prototype and then create clones.

Let's see some code!

Implementation

Let's create our Professional object:
  1. var Professional = WinJS.Class.define(  
  2. function(name, job, age) {  
  3.   
  4.     this.name = name;  
  5.     this.job = job;  
  6.     this.age = age;  
  7.   
  8.     this.say = WinJS.Class.define(     
  9.         function () {  
  10.               
  11.         console.log(this.name + " is working as " + this.job +  
  12.               " who is " + this.age + " years old");  
  13.     });  
  14. });  
Time to create Prototype object:
  1. var ProfessionalPrototype = WinJS.Class.define(  
  2. function(proto) {  
  3.     this.proto = proto;  
  4.   
  5.     this.clone = WinJS.Class.derive(Professional,function(){  
  6.         var prof = new Professional();  
  7.   
  8.         prof.name = proto.name;  
  9.         prof.job = proto.job;  
  10.         prof.age = proto.age;  
  11.   
  12.         return prof;  
  13.     });  
  14. });  
Now, let's instantiate our objects and show the output:
  1. var proto = new Professional("Ibrahim Ersoy""Developer""31");  
  2.   
  3. var prototype = new ProfessionalPrototype(proto);  
  1. prof.say();  
Finalize
 
Let's build and run the project:

 

We've created a single clone and shown how Prototype works. But Prototype's power comes from clones. Now let's extend the functionality a bit more.

Update Professional object:
  1. var Professional = WinJS.Class.define(  
  2. function(name, job, age) {  
  3.   
  4.     this.name = name;  
  5.     this.job = job;  
  6.     this.age = age;  
  7.   
  8.     this.say = WinJS.Class.define(     
  9.         function (gender,altertext) {  
  10.             var gendertext;  
  11.             if (altertext) {  
  12.                 this.name = altertext;  
  13.             }  
  14.             if (gender=="M") {  
  15.                 gendertext = "he";  
  16.             }  
  17.             else {  
  18.                 gendertext = "she";  
  19.             }  
  20.         console.log(this.name + " is working as " + this.job +  
  21.               " & " + gendertext  + " is " + this.age + " years old");  
  22.     });  
  23. });  
We shall create a few clones and change the cloned object's properties.These properties include the name and gender.
 
Let's create 3 more professionals from our clone:
  1. var proto = new Professional("Ibrahim Ersoy""Developer""31");  
  2. var prototype = new ProfessionalPrototype(proto);  
  3.      
  4. var prof1 = prototype.clone();  
  5. var prof2 = prototype.clone();  
  6. var prof3 = prototype.clone();  
  7.   
  8. prof1.say("M","Dhanjay Kumar");  
  9. prof2.say("F","Sara SomeDeveloper");  
  10. prof3.say("F","Angelina Jolie");  
Now let's build and run our example:

  
 
Why should you use it?

Quite simple. If you want to reuse cloned objects Prototype is the design pattern for you, not to mention its popularity in the JS World.