Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts

Monday, September 22, 2008

Factory Pattern (a.k.a. virtual constructor)

Factory Method helps to model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate. We call this a Factory Pattern since it is responsible for "Manufacturing" an Object. It helps instantiate the appropriate Subclass by creating the right Object from a group of related classes. The Factory Pattern promotes loose coupling by eliminating the need to bind application-specific classes into the code.

The Factory Pattern is all about "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses" Thus, as defined by Gamma et al, "The Factory Method lets a class defer instantiation to subclasses".

This pattern is used when a class (the Creator) does not know beforehand all the subclasses that it will create.

* Factory methods are common in toolkits and frameworks where library code needs to create objects of types which may be subclassed by applications using the framework.
* Parallel class hierarchies often require objects from one hierarchy to be able to create appropriate objects from another.

Example: (http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Virtual_Constructor)
class Employee
{
public:
virtual ~Employee () {} // Native support for polymorphic destruction.
virtual Employee * create () const = 0; // Virtual constructor (creation)
virtual Employee * clone () const = 0; // Virtual constructor (copying)
};
class Manager : public Employee // "is-a" relationship
{
public:
Manager (); // Default constructor
Manager (Manager const &); // Copy constructor
~Manager () {} // Destructor
Manager * create () const // Virtual constructor (creation)
{
return new Manager();
}
Manager * clone () const // Virtual constructor (copying)
{
return new Manager (*this);
}
};
class Programmer : public Employee { /* Very similar to the Manager class */ };
Employee * duplicate (Employee const & e)
{
return e.clone(); // Using virtual constructor idiom.
}

More effective c++ Item 25

Because the function creates new objects, it acts much like a constructor, but because it can create different types of objects, we call it a virtual constructor. A virtual constructor is a function that creates different types of objects depending on the input it is given. Virtual constructors are useful in many contexts, only one of which is reading object information from disk (or off a network connection or from a tape).

A particular kind of virtual constructor — the virtual copy constructor — is also widely useful. A virtual copy constructor returns a pointer to a new copy of the object invoking the function.

etc.).


References:

http://gsraj.tripod.com/design/creational/factory/factory.html
http://en.wikipedia.org/wiki/Factory_method_pattern

Sunday, June 10, 2007

Bridge Pattern

Applying the bridge pattern to a class involving separating the class into two parts, an interface and an implementation.

The implementation of the class has been moved to an implementation class hidden from general use.

class C
{
public:
C(int val);
~C();
int get_a() const;
int get_b() const;
private:
Cimpl *impl_;
};

class Cimpl
{
public:
Cimpl(int val) : a_(val),b_(val) {}
~Cimpl() {}
int get_a() const {return a_;}
int get_b() const {return b_;}
private:
int a_;
int b_;
};

C::C(int val): impl_(new Cimpl(val)) {}
C::~C() {delete impl_;}
int C::get_a() const
{
return impl_->get_a();
}
int C:: get_b() const
{
return impl_->get_b();
}

Employing a Bridge incurs a clear runtime cost, each member function call is both indirect and non-inline. The advantages lie in the ability to update client code without recompilation.

Applications: patch updates(bug fixes, typically) into installed software.

Friday, June 8, 2007

Singleton Pattern

http://www.codeproject.com/cpp/singletonrvs.asp

The Singleton class is instantiated at the time of first access and same instance is used thereafter till the application quits. There is only one instance!

Using a private constructor and a static method to create and return an instance of the class is a popular way for implementing Singleton Pattern.

using namespace std;

class Singleton
{
private:
static bool instanceFlag;
static Singleton *single;
Singleton()
{
//private constructor
}
public:
static Singleton* getInstance();
void method();
~Singleton()
{
instanceFlag = false;
}
};

bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
if(! instanceFlag)
{
single = new Singleton();
instanceFlag = true;
return single;
}
else
{
return single;
}
}

void Singleton::method()
{
cout << "Method of the singleton class" << endl;
}

int main()
{
Singleton *sc1, *sc2;
sc1 =" Singleton::getInstance();
sc1->method();
sc2 = Singleton::getInstance();
sc2->method();

return 0;
}