Design Patterns In WinJS #3: Builder

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

What is Builder Design Pattern?


Builder pattern allows you to create objects by specifying type and content. Client will not know the details of what happens in the background but will know the steps taken till the work ends.
 
According to DoFactory,Builder is:

 

Since Interfaces cant be used in AbstractBuilder object,it will be removed out of picture. Builder is a pattern when you know which steps needed to complete a job and thus create a new object afterwards.

A much more easy to understand example can be given about Universities-Lessons-Professional triangle of relationship. Student goes to a university,  attends a class, passes exams, finishes school and then become a professional:

 


You let others see what steps are taken but dont show the entire work at background.

According to the above sample, people know you need to pass 3-4 years of exams but they dont know which classes you get or which points you get from each exam.They only know whether you finished school or not.

Builder class is similar to this, we will create a few steps then finish our job.

Implementation 
 
Let's create our University object:
  1. var University = WinJS.Class.define(  
  2.     function() {  
  3.     this.construct = function (Lessons) {  
  4.         Lessons.step1();  
  5.         Lessons.step2();  
  6.         return Lessons.get();  
  7.     }  
  8. });  
As you can see University has steps to complete. I added only 2 as an example.

Lets add our StudentBuilder class
  1. var StudentBuilder = WinJS.Class.derive(Student,  
  2.     function() {  
  3.     this.student = null;  
  4.   
  5.     this.step1 = function () {  
  6.         this.student = new Student();  
  7.     };  
  8.   
  9.     this.step2 = function () {  
  10.         this.student.Finalize();  
  11.     };  
  12.   
  13.     this.get = function () {  
  14.         return this.student;  
  15.     };  
  16. });   
 which derives from Student class:
  1. var Student = WinJS.Class.define(  
  2.     function() {  
  3.     this.years = 0;  
  4.   
  5.     this.Finalize = function () {  
  6.         this.years = 4;  
  7.     };  
  8.   
  9.     this.say = function () {  
  10.         Helper.Func.add("I graduated a  " + this.years + " years university");  
  11.     };  
  12. });  
We've once more defined our Helper class for logging to console:
  1. WinJS.Namespace.define("Helper");  
  2. var log = "";  
  3.   
  4. Helper.Func =  
  5. {  
  6.     add: function (msg) { log += msg + "\n"; },  
  7.     show: function () { console.log(log); log = ""; }  
  8. };  
To use the Builder pattern,we simply call them by:
  1. var uni = new University();  
  2. var studentBuilder = new StudentBuilder();     
  3. var student = uni.construct(studentBuilder);  
  4. student.say();    
  5. Helper.Func.show();  
Finalize

The steps to Builder pattern is as below:
  1. We create a new University object
  2. We build our student using StudentBuilder object.
  3. Relation our Student with University
  4. Add Student's message
  5. Log Student's message
Here is what you see after build&run:



Why should you use it?

The Builder pattern simplifies complex architectures by splitting types of objects and theirs contents. It's a useful pattern for defining step-by-step jobs. 

Next Recommended Readings
ARAF GLOBAL
Araf Global is a software consultancy company founded in 2016 focusing on cutting edge technologies.