Wednesday, March 27, 2013

Polymorphic Ajax: See How Polymorphism Can Speed Code Writing


Polymorphic Ajax: See How  Polymorphism Can Speed Code Writing
In computer science, polymorphism means allowing a single definition to be used with different types of data (specifically, different classes of objects).  Polymorphism along with modularity and encapsulation are building blocks of  Object Oriantated Programming(OOP).  Polymorphism can alow programmers to reuse code, reduce code complexity and decrease inconsistancys in code design.

When designing Javascript methods it is often helpful to define standard interfaces. For example, if an object is substanciated with a transparency method and another object is substanciated with a transperenct method then the programmer does not have to nessecarely create two seperate methods.  For example lets look at a transparent method.

Two Function Two Method Example:

function menu()
{
  this.m_div = document.getElementById("container");
  this.m_div.innerHTML = '<table>'
                                                    + '<tr>'
                                                    + '<td>Home</td>'
                                                    + '<td>Approach</td>'
                                                    + '</tr>'
                                              + '</table>'
 }
menu.prototype.transparent = function()
{
  if(this.m_div)
  {
    this.m_div.style.filter = "alpha(opacity:40)";//IE
    this.m_div.style.opacity = .7;// Safari , Firefox
  }
}

function menu2()
{
  this.m_div = document.getElementById("container");
  this.m_div.innerHTML = this.m_div.innerHTML = '<img src="image/trees.jpg" />';
}
menu2.prototype.transparent = function()
{
  if(this.m_div)
  {
    this.m_div.style.filter = "alpha(opacity:40)";//IE
    this.m_div.style.opacity = .7;// Safari , Firefox
  }
}

 
We can access these methods in another fuction. 

Calling Function:
function main()
{
  g_menu = new menu();
  g_menu.transparent();
  g_menu2 = new menu2();
  g_menu2.transparent();
}
 
If we use polymorphism then we can have reusable code. Shortening out development time.We would keep our original function but replace the methods with one function.


PolyMorphic Function:
function transparent(obj)
{
  if(obj.m_div)
  {
      obj.m_div.style.filter = "alpha(opacity:40)";//IE
      obj.m_div.style.opacity = .7;// Safari , Firefox
  }
}

 
To call this we would replace the method calls with function calls in which we have passed the object.

function main()
{
  g_menu = new menu();
  transparent(g_menu);
  g_menu2 = new menu2();
  transparent(g_menu2);
}

 
 
This example is short and shows a simple example of polymorphism.  The code savings in this example is short and does not show significant code savings.  However, as a project scales the need for reusable code becomes more apperent.  Polymorphism is not for every situration.  Given the right crecomstance this core of OOP programming can increase be a real boon to any project. 
 

No comments:

Post a Comment