Thursday 23 February 2017

Call back function

Learning function pointer is not an easy job, Most of the people check for syntax and think about call back functions and try to understand before doing the job or attending an interview.

Let us forget about syntax and callback and all, we will be looking with problem domain.

Note: Callback concepts there before internet technology comes,  we implement the functionality using C++ style

Let us start, Akbar is a great guy, who always buy latest mobile brand whenever a new device available in market, So he is responsible to register on this to Nokia.

So he need to pass his identity(this)  with his address (method/function) and whenever nokia launching new mobile they will "callback" Akbar  via address 

This process will repeat, every time new mobile has been launched by Nokia.

Now its the time to put those stories in syntax of C++, C++ code is not compiled, just for explanation.

Nokia is global leader!!, Let it be singleton, Have only one brand, there is no duplicate.
Class NokiaPhone
{
   public:
     //Let everyone get an access of NokiaPhone service with original factory(finland)
     NokiaPhone*  getInstance()
     {
         if( NULL == m_CellPhone )
          {
             m_CellPhone =   new  CellPhone();
             return m_CellPhone;
          } 
         return m_CellPhone;    
     }

     // Let people register for the updates
     // Void*-Any of the person can register at any time.
    void register( void* customer,  addressforCallBack )
   {
       CustomerHandler* customerHandler =  new CustomerHandler( customer,                                                                                                                                addressforCallBack );
       m_listOfCustomers.append( customerHandler );
   }

   void unregister( void* customer )
   {
       //remove the 
      for( int i = 0; i < m_listOfCustomer.count(); i++)
     {
           CustomerHandler * customerHandler =  m_listOfCustomer.remove(customer);
     }
       
   }

 void newMobileLaunched()
 {
   // callbackAkbar
 }
   private://Let no one create  duplicate(Single ton )
     CellPhone();
         static   CellPhone* m_CellPhone;
         list m_listOfCustomer;
};


//I like human, Since his attributes are endless!!
class Person
{
private:
   int m_numberOfMobileCount;
public:
   void updateMe()
   {
       //Let me check out new mobile launched by my favourite brand!!!
      m_numberOfMobileCount++;
      if(  m_numberOfMobileCount == 10 )
          delete this;
   }
    Person()
   {  
         //Akbar born to buy latest mobiles!!!!
         // Akbar register to Nokia to get updates on his address.
         // How to pass address of a function as a parameter?, yes here in C++ we have sytax to get and            hold the address  of a function, like any other variable.
         //
         NokiaPhone->getInstance().register(this,  updateMe)
    }

    Person()
    {
        //Akbar is great, he is doing his duty before he leaves this world
        void unregister(this);
     }      
  }
};

int main()
{
  Person Akbar;
  // My creator only knows when I die!!!
  // Hi guys, I am creator of this Akbar, Let him get 10 new mobile updates from Nokia and I will         destroy( I don't like this word, for human, but i like it for "Person", since person don't have any soul.)
}



Thursday 19 February 2015

Constructor pattern in Javascript

Here I am going to explain on constructors, If you do not know C++, then please do not proceed further!!!

Objects are created from class/struct(C++), like wise Objects are created from "Constructor"( javascript).  
-------------------C++------------------  
Class Bus
{
      public:
      Bus( int model, int seatCapacity )
      {
          this.m_model = model;
          this.seatCapacity = seatCapacity;
       }
       private:
         int  m_model;
         int  seatCapacity;
  };
main()
{
    Bus   busObj( 4, 47 );
}
------------------------------------------

-----------------javascript------------
function Bus( model, seatCapacity)
{
    this.model = model;
    this.seatCapacity = seatCapacity;
}
var busObj = new Bus( 4, 47);
busObj.model = 5;//Access is graned to set 5 to "model" property.
------------------------------------------------

The limitation of Constructor patter in javascript is, you cannot hold private member variable. So you might me think that, how data encapsulation is taking place in JQuery or similar libraries ..Yes we have solution for that, that is what is called "Closure"

Note:

  • "new" keyword  is required in javascript to create the constructor, new is actually point to "this"
  • Use contructor, if you want to create 100's of similar objects.
  • there is no class in javascript