Monday, 31 March 2014

Static blocks in C++

Java Code:

Class NewObjectType{

static{
Factory.register("MyType", new NewObejctType);
}
 //Rest of the code goes here.
}

This is fine, your class registers with factory, but how will you achieve this in C++????
How to get static blocks in C++?

Here is the solution/code for it:

#include "stdafx.h"
#include <iostream>
#include<map>

using namespace std;
class Abs{
public:
    static void reg();
};

class Factory{
    static std::map<int,Abs*> map;
public:
    static void regsiter(int id,Abs *a)
    {
        cout<<"child registered"<<endl;
        map.insert(make_pair(id,a));
    }
};

std::map<int,Abs*> Factory::map;

class child:public Abs{
    static int ret;
public:
    child()
    {
        display();
    }
    void display()
    {
        cout<<"child"<<endl;
    }
    static int reg()
    {
        cout<<"static method called"<<endl;
        Factory::regsiter(10,new child());
        return 0;
    }
};

 int child::ret = child::reg();

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

Factory Pattern - Better way

when we write factory pattern, generally we write if-else bock to create the objects of the input type
something like

Base* Factory::getObject(int type)
{
if(type==1)
return new TypeOne();
else if(type==2)
return new TypeTwo();
...
...
}

But in this case, we need to modify the factory class every time we add a new object type, this violates the principle of Dependency Inversion, where higher level objects should not depend on the lower level objects.

 So how to get the new objects returned from Factory without modifying it?

The solution is using the registration mechanism, when ever a new object type is introduced , you register that with factory with a id/type value. This way factory maintains all the new obejcts with some type/id.
 You can you java reflection if you register with class name or can you protptype pattern to clone the new objects.
Java Code:

Class NewObjectType{

static{
Factory.register("MyType", new NewObejctType);
}
 //Rest of the code goes here.
}

This is fine, your class registers with factory, but how will you achieve this in C++????
How to get static blocks in C++?

Here is the solution/code for it:

#include "stdafx.h"
#include <iostream>
#include<map>

using namespace std;
class Abs{
public:
    static void reg();
};

class Factory{
    static std::map<int,Abs*> map;
public:
    static void regsiter(int id,Abs *a)
    {
        cout<<"child registered"<<endl;
        map.insert(make_pair(id,a));
    }
};

std::map<int,Abs*> Factory::map;

class child:public Abs{
    static int ret;
public:
    child()
    {
        display();
    }
    void display()
    {
        cout<<"child"<<endl;
    }
    static int reg()
    {
        cout<<"static method called"<<endl;
        Factory::regsiter(10,new child());
        return 0;
    }
};

 int child::ret = child::reg();

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

Parking lot design

Point Of Sale design


AWS Data Pipeline Services

https://www.youtube.com/watch?v=tykcCf-Zz1M