Symantec Interview Question
Senior Software Development EngineersCountry: India
public class SingletonExample {
/** Instance variable */
private static SingletonExample m_objSingle = null;
/**
* Private constructor. Not to access from other classes
*/
private SingletonExample() {}
/**
* Method to return singleton object if the instance is null
* Otherwise return the same instance.
* synchronized block is used in case of multithreading
* @return {@link SingletonExample}
*/
public SingletonExample getInstance(){
synchronized (m_objSingle) {
if(null == m_objSingle) return new SingletonExample();
else return m_objSingle;
}
}
}
some of the important creational design pattern that I know are:
Singleton
Factory method
Abstract factory
Prototype
Some of the important structural design patterns are
Adapter
Proxy
some of the important behavioral design patterns are
Observer
State
Command
Iterator
Strategy
template Method
Singleton : Only single instance of the class can be created. Make the constructor/destructor protected or private and make copy constructor and assignment operator private. Provide an static getInstance method that would allocate single object on heap if it hasn't already been allocated and return this single instance's pointer.
Factory method: This patterns relies on the derived class to create correct object for the target class. This pattern returns the product as an abstract product. For example to create a new product component the whole product class is derived and the new component's create method is overridden to return the new type of component. The final product is created by adding various components to it.
Abstract Factory: This pattern provides an interface for creating families of related object and returns objects as an abstract object so that the client code is not tightly coupled with the implementation of the interface.
Prototype: This pattern relies on the implementation of clone() method in each of the derived class. Clone operation is declared as pure virtual in the base class. Each derived class is responsible for cloning itself and returning the correct type of object through abstract base product interface.
Adapter: An old interface is adapted to the new interface that is required by the client application. For example if deque (STL class) need to be adapted to suit the stack abstraction then there is Stack (STL class) which works as an adapter which adapts queue to the need of application which requires stack abstraction.
Proxy: One object behaves as a proxy of another. Proxy object can implement resource hiding, security, smart manipulation of underlying object (smart pointer, shared pointer) etc.
Observer: One application (known as observer) registers with another application or library (Subject) dynamically by calling register or attach method on the subject. When ever there is an state change in the subject, subject->notify is called by the application which owns subject and inside notify each of the observer is updated (observer->update) is called for each registered observer object. List of observer can be maintained using any appropriate STL container class.
State : This pattern encapsulates the state machine mechanism for an object whos behavior on an event depends upon its current state. The class which needs to have state, contains a abstract instance of the state (pointer to state base class). When an event is to be handled by this class (class->handleEvent), then appropriate method is called on the state pointer (baseState->handleEvent1()). The base state is derived by concrete state and each concrete state implements only those operations which are valid in those states.
Command: command which implement execute or doit methods can be added dynamically to the objects which represents operations. These objects like a remote control represent a machinary having some functions and these functions have a dedicated object which represents that function. These objects are known as commands. The machinary (like remote control) contains all the commands in some associative control where a command object is mapped with a suitalbe enum (using stl maps).
Iteartor : Iterator provides a means for iterating through various objects hidden under a container, without depending upon the underlying implementation or representation of the container class. For example STL iterator classes which iterate through various sequence or associative containers like vector, list ,deque, map, multimap, set, multiset etc.
Singleton is machnism that only one instance will exist in system.So, in order to achieve it, you should make your Construct and Copy Construct(also Operator =) private so one can not by them to create new object, finally give an interface:
- about Singleton April 05, 2013ClassType * GetInstance(){
static ClassType *object = NULL;
if (NULL == object){
return new ClassType();
}
return object;
}
ps: think about inheritation, above code may have some problem.Also, in multi-thread enviroment, we should use double-lock to make sure only one instance will create by different thread.