Singleton (object level) IntentGuarantee a single instance of a class Provide global point of access to it MotivationOne connection to db, instance of window manager // Extensible singleton
class CExtensibleSingleton ;
using * ( * TExtensibleSingletonFunction )() = CExtensibleSingleton ;
class CExtensibleSingleton {
protected:
CExtensibleSingleton () {};
static void Register ( const std :: string & instanceType , TExtensibleSingletonFunction func );
static std :: map < std :: string , TExtensibleSingletonFunction > & Registry () {
static std :: map < std :: string , TExtensibleSingletonFunction > StaticRegistry ;
return StaticRegistry ;
}
static CExtensibleSingleton * DUniqueInstance ;
void Register ( const std :: string & instanceType , TExtensibleSingletonFunction func ) {
Registry ()[ instanceType ] = func ;
}
public:
static CExtensibleSingleton * Instance ( const std :: string & instanceType ) {
if ( nullptr == DUniqueInstance ) {
if ( Registry (). end () != Registry (). find ( instanceType )) {
DUniqueInstance = Registry ()[ instanceType ]();
}
}
return DUniqueInstance ;
}
virtual void Operation () = 0 ;
}
CExtensibleSingleton * CExtensibleSingleton :: DUniqueInstance = nullptr ;
// Extended singleton
class CExtendedSingleton : public CExtensibleSingleton {
protected:
CExtendedSingleton () : CExtensibleSingleton () {}
static CExtensibleSingleton * ExtendedSingletonInstance () {
return new CExtendedSingleton ();
}
class CRegistrant {
public:
CRegistrant () {
CExtensibleSingleton :: Register ( "Extended" , CExtendedSingleton : ExtendedSingletonInstance );
}
};
static CRegistrant DRegistrant ;
public:
void Operation () {
// operation
}
}
// Thread safe lazy singleton
public class SingletonLazyTS {
// inner static class are initialized at FIRST use only
private static class LazyHolder {
private static final SingletonLazy SINGLETON = new SingletonLazyTS ();
}
private SingletonLazyTS (){};
public static SingletonLazyTS GetInstance () {
return LazyHolder . SINGLETON ;
}
}
Factory Method (class level) IntentHave an interface to create object MotivationReduce redundancy in creating objects BenefitsConnect parallel class hierarchies LimitationsRefactoring class to use Factory Method breaks existing clients // Creator
class CCreator {
public:
void Operation () {
CProduct * Product = CreateProduct ();
Product -> Operation ();
delete Product ;
}
virtual CProduct * CreateProduct () = 0 ;
}
// Concrete creator
class CConcreteCreator : public CCreator {
public:
CProduct * CreateProduct () {
return new CConcreteProduct ();
}
}
Builder (object level) IntentSeparate object construction from its representation Allow same construction process to create different representation MotivationDocuments that may require many type conversion BenefitsModify internal representation Gives finer control over construction process // Director
class CDirector {
protected:
CBuilder * DBuilder ;
public:
CDirector ( CBuilder * builder ) {
* DBuilder = builder ;
}
void Construct () {
for ( int i = 0 ; i < N ; ++ i ) {
DBuilder -> BuildPart ();
}
}
}
// Builder
class CBuilder {
public:
virtual void BuildPart () = 0 ;
}
class CConcreteBuilder : Public CBuilder {
protected:
std :: vector < CProduct *> DProducts ;
public:
CConcreteBuilder () : CBuilder {}
void BuildPart () {
DProducts . push_back ( new CProduct );
}
}
public class Product {
private String name ;
private int ID ;
//...
Product ( String name , int id , ...) {
this . name = name ;
this . ID = id ;
/...
}
}
public static class Builder {
private String name ;
private int ID = - 1 ;
//...
public Builder name ( String s ) {
this . name = s ;
return this ;
}
public Builder id ( int id ) {
this . ID = id ;
return this ;
}
//...
public Product build () throws IllegalStateException {
if ( this . name == null || this . ID = - 1 || / ...) {
throw new IllegalStateException ( "" );
}
return new Product ( name , ID , ...);
}
}
Abstract Factory (object level) IntentCreate families of objects without specifying concrete classes MotivationSupport multiple look-and-feel widgets Support multiple database implementation BenefitsIsolate concrete classes Make exchanging product family easy LimitationsSupporting new product types is difficult // Abstract Factory
class CAbstractFactory {
public:
CAbstractProductA * CreateProductA () = 0 ;
CAbstractProductB * CreateProductB () = 0 ;
}
// Abstract Products
class CAbstractProductA {
public:
virtual void OperationA () = 0 ;
}
class CAbstractProductB {
public:
virtual void OperationB () = 0 ;
}
class CConcreteFactory1 : public CAbstractFactory {
public:
CAbstractProductA * CreateProductA () {
return new CConcreteProductA1 ();
}
CAbstractProductB * CreateProductB () {
return new CConcreteProductB1 ();
}
}