Wednesday, July 21, 2010

Assignment Day 2

Case Study 1
•Rajneesh has approached you for help. He has one global object of each class A,B,C,D & E. He needs to ensure that they are created in the order they are listed and destroyed in the reverse order. He also shares with you that they have to be thread safe. Please suggest him.
-----------------------------------
* Think about the problems associated with Singleton and how can you resolve them...
* If the Manager class in your Code is a singleton evaluate and share the repercurssions of that decision. Does it in any way inhibit the future design changes? Under what circumstances will it become a bottleneck.?
-----------------------------------
Case Study 2
•Problem: Design a plugin framework such that anyone can write a reusable component that can be accessed by your application. To keep it simple let us only talk about database plugins that allow information to be written in various formats, like XML, SQL lite, Oracle, File, ODBC driver, OLE DB/ADO, basically anything that can persist the information. The focus should be how to instantiate such plugins.
----------------------------------
•Double Check Locking singleton (For Further Reading)

9 comments:

  1. Singleton is not safe for multithreaded environments. We can solve this by using the below code(C#)Double-Check Locking

    public sealed class Singleton
    {
    private static volatile Singleton instance;
    private static object syncRoot = new Object();

    private Singleton() {}

    public static Singleton Instance
    {
    get
    {
    if (instance == null)
    {
    lock (syncRoot)
    {
    if (instance == null)
    instance = new Singleton();
    }
    }

    return instance;
    }
    }
    }

    ReplyDelete
  2. Case Study 1
    •Rajneesh has approached you for help. He has one global object of each class A,B,C,D & E. He needs to ensure that they are created in the order they are listed and destroyed in the reverse order.

    Answer:
    A a;
    B b;
    C c;
    D d;
    E e;
    int main(void)
    {
    return 0;
    }

    ReplyDelete
  3. Case Study 2
    •Problem: Design a plugin framework such that anyone can write a reusable component that can be accessed by your application.

    Answer:

    class Format
    {
    virtual int read() = 0;
    virtual int write() = 0;
    }

    class Oracle: public Format
    {
    virtual int read();
    virtual int write();
    }

    class File: public Format
    {
    virtual int read();
    virtual int write();
    }

    enum FormatTypes
    {
    FT_ORACLE,
    FT_FILE
    };

    Format* GetFormatObject(FormatTypes type)
    {
    Format* p = NULL;
    switch(type)
    {
    case FT_ORACLE:
    p = new Oracle;
    break;

    case FT_FILE:
    p = new File;
    break;
    }
    return p;
    }

    ReplyDelete
  4. Disadvantages of Singleton:
    1) Code Cluttering : The singleton class knows that its a singleton. Any other class that uses it also knows that its singleton

    2) Adding behavior through Inheritance is a problem since CreateInstance returns only base class Singleton

    3) Destruction of a singleton is not in our hands.
    4) If there are many singletons in your design (its a bad one though!!), the singletons accessing each other in their destructors could be a disaster

    5) Multi-threading is an issue.
    if only one lock is used as follows:
    class Singleton
    {
    public:
    Singleton * creatInstance()
    {
    m_mutex->lock();
    if(m_pInstance != NULL)
    {
    m_pInstance = new Singleton;
    }
    m_mutex->unlock();
    return m_pInstance;

    }

    private:
    static Singleton *m_pInstance;
    Mutex m_mutex

    }

    Disadv of single locking that it makes the access to the singleton instance sequential. Any thread, even if instance has been created is blocked unless, the lock is released.

    The solution to this is double-checked locking Pattern as suggested by Megha above.

    ReplyDelete
  5. Some other disadvantages are:
    6) Converting a singleton to multi instance class, later on.
    7) Writing unit test cases is a problem. Since Singleton instance is mostly used directly (not appearing in function arguments), one who tests the code has to know abt dependency on Singleton too!!

    ReplyDelete
  6. I've a question. I've seen many designs in which a manager class (which will be Singleton) holds a number of objects in it?
    What is the intent? Do we want to maintain a single instance of all such contained classes or do we want to control the order in which these objects co-ordinate or both?
    It'll be a great help if someone can explain

    ReplyDelete
  7. Case Study 2:
    class IPlugin
    {
    virtual void write() =0;
    virtual void read() = 0;
    };
    class IPluginCreator
    {
    virtual IPlugin* createPlugin() =0;
    };
    class XMLPluginCreator:IPluginCreator
    {
    IPlugin* createPlugin()
    {
    return new XMLPlugin;
    }
    };

    class ODBCPluginCreator:IPluginCreator
    {

    IPlugin* createPlugin()
    {
    return new ODBCPlugin;
    }
    };

    class App
    {

    public:
    App(enum PluginCreatorType)
    {

    if(PluginCreatorType == XML)
    {
    m_pPlugin = new XMLPluginCreator;
    }
    else if(PluginCreatorType == ODBC)
    {
    m_pPlugin = new ODBCPluginCreator;

    }

    }
    private:
    IPlugin *m_pPlugin;

    };

    ReplyDelete
  8. Case Study 2:
    I propose the below solution.

    // The main application need not know about what formats are supported.
    //The user should be able to support a new format just by writing a new plugin for it.
    //This Code will be prsent in the main application

    class DBBase
    {
    virtual int read(string s) = 0;
    virtual int write(string s) = 0;
    }

    int main()
    {
    //Read a configuration file to find the dll for the DB to be used and call GetDBObject() from that dll.

    DBBase *p = GetDBObject();
    p->Write("Hello World");
    p->Read("Hello World");
    }


    //This code will be present Oracle plugin dll

    DBBase *GetDBObject()
    {
    return new DBOracle();

    }
    class DBOracle: public DBBase
    {
    virtual int read(string s) ;
    virtual int write(string s);
    }


    //This code will be present in File plugin dll

    DBBase *GetDBObject()
    {
    return new DBFile();

    }


    class DBFile: public DBBase
    {
    virtual int read(string s) ;
    virtual int write(string s);
    }

    ReplyDelete
  9. Class ODS
    {
    public:
    virtual bool Read() = 0;
    virtual bool Write() = 0;
    virtual ~ODS(){};

    }


    class File :public ODS
    {
    public:
    bool Read();
    bool Write(); //uses mutex to lock object
    ~File(){};

    }

    class XMLFile : public ODS
    {
    public:
    bool Read();
    bool Write(); //excusive resource write using mutex
    ~XMLFile();
    }

    class SQL : public ODS
    {
    public:
    bool Read();
    bool Write(); //mutex lock
    ~SQL();
    }


    class IConnect
    {
    public:
    static ODS* OpenConnection(int iFormatType);
    private:
    IConnect();
    operator = ();
    Iconnect(IConnect&);
    };


    ODS* Iconnect :: OpenConnection(int iFormatType)
    {
    if( iFormatType)
    {
    return (ODS*)(class Name object);
    }

    }

    ReplyDelete