Showing posts with label design pattern. Show all posts
Showing posts with label design pattern. Show all posts

Jan 21, 2009

Describe what a singleton is and when you would use it. the singleton pattern is a design pattern that is used to restrict instantiation of a class to

Describe what a singleton is and when you would use it.
the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This concept is also sometimes generalized to restrict the instance to more than one object

common use:
The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation.
Facade objects are often Singletons because only one Facade object is required.
State objects are often Singletons.

Implementation:
The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made protected (not private, because reuse and unit test could need to access the constructor).

Dec 26, 2008

Design pattern

Design pattern

Abstract factory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Factory method: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Builder: Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Lazy initialization: Tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.
Object pool: Avoid expensive acquisition and release of resources by recycling objects that are no longer in use
Prototype: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Singleton: Ensure a class has only one instance, and provide a global point of access to it.
Adapter: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Bridge: Decouple an abstraction from its implementation so that the two can vary independently.
Composite: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

Dec 24, 2008

Abstract factory design pattern case

Abstract factory design pattern case
An example of this would be an abstract factory class DocumentCreator that provides interfaces to create a number of products (eg. createLetter() and createResume()). The system would have any number of derived concrete versions of the DocumentCreator class like FancyDocumentCreator or ModernDocumentCreator, each with a different implementation of createLetter() and createResume() that would create a corresponding object like FancyLetter or ModernResume. Each of these products is derived from a simple abstract class like Letter or Resume of which the client is aware. The client code would get an appropriate instantiation of the DocumentCreator and call its factory methods. Each of the resulting objects would be created from the same DocumentCreator implementation and would share a common theme (they would all be fancy or modern objects). The client would need to know how to handle only the abstract Letter or Resume class, not the specific version that it got from the concrete factory.
Resource :
http://en.wikipedia.org/wiki/Abstract_factory_pattern


class DocumentCreater
{
virtual Letter* createL()=0;
virtual Resume* createR()=0;
virtual ~DocumentCreater();
};

class FancyDocumentCreater: public DocumentCreater
{
virtual FancyLetter* createL();
virtual FancyResume* createR();
};

class MordenDcumentCreater: public DocumentCreater
{
virtual MordenLetter* createL();
virtual MordenResume* createR();
};

class Letter()
{
irtual
};

class Resume()
{
};

class FancyLetter(): public Letter
{
virtual ~Letter();
};

class FancyResume(): public Resume
{
virtual ~Resume();
};

class MordenLetter(): public Letter
{
};

class MordenResume():public Resume
{
};

class AppL//the application to create letter. which type we create based on which factory passed in
{
Letter* AppL(DocumentCreater* factory)
{
return factory->createL();
}
};

class AppR//the application to create resume. which type we create based on which factory passed in
{
Resume* AppR(DocumentCreater* factory)
{
return factory->createR();
}
};


void main(void)
{
DocumentCreater* factory1= new FancyDocumentCreater();
DocumentCreater* factory2= new MordenDocumentCreater();

Letter* L=new AppL(factory1);
Resume* R=new AppR(factory2);


delete factory1, factory2, L, R;

}