注释:把生产者和产品做成了上层抽象类,具体实现由继承类完成。
/*
* C++ Design Patterns: Factory Method
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
* 2016
*
* Source code is licensed under MIT License
* (for more details see LICENSE)
*
*/
#include
#include
/*
* Product
* products implement the same interface so that the classes can refer
* to the interface not the concrete product
*/
class Product
{
public:
virtual ~Product() {}
virtual std::string getName() = 0;
// ...
};
/*
* Concrete Product
* define product to be created
*/
class ConcreteProductA : public Product
{
public:
~ConcreteProductA() {}
std::string getName()
{
return "type A";
}
// ...
};
/*
* Concrete Product
* define product to be created
*/
class ConcreteProductB : public Product
{
public:
~ConcreteProductB() {}
std::string getName()
{
return "type B";
}
// ...
};
/*
* Creator
* contains the implementation for all of the methods
* to manipulate products except for the factory method
*/
class Creator
{
public:
virtual ~Creator() {}
virtual Product* createProductA() = 0;
virtual Product* createProductB() = 0;
virtual void removeProduct( Product *product ) = 0;
// ...
};
/*
* Concrete Creator
* implements factory method that is responsible for creating
* one or more concrete products ie. it is class that has
* the knowledge of how to create the products
*/
class ConcreteCreator : public Creator
{
public:
~ConcreteCreator() {}
Product* createProductA()
{
return new ConcreteProductA();
}
Product* createProductB()
{
return new ConcreteProductB();
}
void removeProduct( Product *product )
{
delete product;
}
// ...
};
int main()
{
Creator *creator = new ConcreteCreator();
Product *p1 = creator->createProductA();
std::cout << "Product: " << p1->getName() << std::endl;
creator->removeProduct( p1 );
Product *p2 = creator->createProductB();
std::cout << "Product: " << p2->getName() << std::endl;
creator->removeProduct( p2 );
delete creator;
return 0;
}
注释:感觉和上面的模式1没有本质区别,欢迎讨论!
/*
* C++ Design Patterns: Abstract Factory
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
* 2016
*
* Source code is licensed under MIT License
* (for more details see LICENSE)
*
*/
#include
/*
* Product A
* products implement the same interface so that the classes can refer
* to the interface not the concrete product
*/
class ProductA
{
public:
virtual ~ProductA() {}
virtual const char* getName() = 0;
// ...
};
/*
* ConcreteProductAX and ConcreteProductAY
* define objects to be created by concrete factory
*/
class ConcreteProductAX : public ProductA
{
public:
~ConcreteProductAX() {}
const char* getName()
{
return "A-X";
}
// ...
};
class ConcreteProductAY : public ProductA
{
public:
~ConcreteProductAY() {}
const char* getName()
{
return "A-Y";
}
// ...
};
/*
* Product B
* same as Product A, Product B declares interface for concrete products
* where each can produce an entire set of products
*/
class ProductB
{
public:
virtual ~ProductB() {}
virtual const char* getName() = 0;
// ...
};
/*
* ConcreteProductBX and ConcreteProductBY
* same as previous concrete product classes
*/
class ConcreteProductBX : public ProductB
{
public:
~ConcreteProductBX() {}
const char* getName()
{
return "B-X";
}
// ...
};
class ConcreteProductBY : public ProductB
{
public:
~ConcreteProductBY() {}
const char* getName()
{
return "B-Y";
}
// ...
};
/*
* Abstract Factory
* provides an abstract interface for creating a family of products
*/
class AbstractFactory
{
public:
virtual ~AbstractFactory() {}
virtual ProductA *createProductA() = 0;
virtual ProductB *createProductB() = 0;
};
/*
* Concrete Factory X and Y
* each concrete factory create a family of products and client uses
* one of these factories so it never has to instantiate a product object
*/
class ConcreteFactoryX : public AbstractFactory
{
public:
~ConcreteFactoryX() {}
ProductA *createProductA()
{
return new ConcreteProductAX();
}
ProductB *createProductB()
{
return new ConcreteProductBX();
}
// ...
};
class ConcreteFactoryY : public AbstractFactory
{
public:
~ConcreteFactoryY() {}
ProductA *createProductA()
{
return new ConcreteProductAY();
}
ProductB *createProductB()
{
return new ConcreteProductBY();
}
// ...
};
int main()
{
ConcreteFactoryX *factoryX = new ConcreteFactoryX();
ConcreteFactoryY *factoryY = new ConcreteFactoryY();
ProductA *p1 = factoryX->createProductA();
std::cout << "Product: " << p1->getName() << std::endl;
ProductA *p2 = factoryY->createProductA();
std::cout << "Product: " << p2->getName() << std::endl;
delete p1;
delete p2;
delete factoryX;
delete factoryY;
return 0;
}
注释:单例模式有很多实现方式,特别是有面试官会问你多线程下的单例模式。试试手写单例吧,单例的实现离不开构造函数、拷贝函数,public、private的理解
class Singleton
{
public:
// The copy constructor and assignment operator
// are defined as deleted, which means that you
// can't make a copy of singleton.
//
// Note: you can achieve the same effect by declaring
// the constructor and the operator as private
Singleton(Singleton const&) = delete;
Singleton& operator=(Singleton const&) = delete;
//对外部全部使用静态方式的函数
Static Singleton* get(){
if(!instance){
instance = new Singleton();
}
return instance;
}
//对外部全部使用静态方式的函数
Static Singleton* get()
{
if(!instance){
instance = new Singleton();
}
return instance;
}
//对外部全部使用静态方式的函数
Static void restart()
{
if(instance){
delete instance;
}
}
void tell(){
std::cout << "This is Singleton." << std::endl;
}
private:
//唯一的构造函数私有化
Singleton(){}
//类内的静态成员变量
static Singleton* instance;
};
//类内的静态成员变量,要在外部初始化
Singleton* Singleton::instance = nullptr;
int main(){
//类的静态成员函数要使用这种方式调用,因为是公共的数据
Singleton::get()->tell();
Singleton::restart();
}
注释:对建造者进行上层抽象,指挥者使用get和set可以切换不同的构造者,感觉还是对抽象基类的运用,是新瓶装旧酒。
/*
* C++ Design Patterns: Builder
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
* 2016
*
* Source code is licensed under MIT License
* (for more details see LICENSE)
*
*/
#include
#include
/*
* Product
* the final object that will be created using Builder
*/
class Product
{
public:
void makeA( const std::string &part )
{
partA = part;
}
void makeB( const std::string &part )
{
partB = part;
}
void makeC( const std::string &part )
{
partC = part;
}
std::string get()
{
return (partA + " " + partB + " " + partC);
}
// ...
private:
std::string partA;
std::string partB;
std::string partC;
// ...
};
/*
* Builder
* abstract interface for creating products
*/
class Builder
{
public:
virtual ~Builder() {}
Product get()
{
return product;
}
virtual void buildPartA() = 0;
virtual void buildPartB() = 0;
virtual void buildPartC() = 0;
// ...
protected:
Product product;
};
/*
* Concrete Builder X and Y
* create real products and stores them in the composite structure
*/
class ConcreteBuilderX : public Builder
{
public:
void buildPartA()
{
product.makeA( "A-X" );
}
void buildPartB()
{
product.makeB( "B-X" );
}
void buildPartC()
{
product.makeC( "C-X" );
}
// ...
};
class ConcreteBuilderY : public Builder
{
public:
void buildPartA()
{
product.makeA( "A-Y" );
}
void buildPartB()
{
product.makeB( "B-Y" );
}
void buildPartC()
{
product.makeC( "C-Y" );
}
// ...
};
/*
* Director
* responsible for managing the correct sequence of object creation
*/
class Director {
public:
Director() : builder() {}
~Director()
{
if ( builder )
{
delete builder;
}
}
void set( Builder *b )
{
if ( builder )
{
delete builder;
}
builder = b;
}
Product get()
{
return builder->get();
}
void construct()
{
builder->buildPartA();
builder->buildPartB();
builder->buildPartC();
// ...
}
// ...
private:
Builder *builder;
};
int main()
{
Director director;
director.set( new ConcreteBuilderX );
director.construct();
Product product1 = director.get();
std::cout << "1st product parts: " << product1.get() << std::endl;
director.set( new ConcreteBuilderY );
director.construct();
Product product2 = director.get();
std::cout << "2nd product parts: " << product2.get() << std::endl;
return 0;
}
注释:对原形做上层抽象类,然后客户每次创造的都是原形子类的拷贝。
还是对抽象基类的应用。
/*
* C++ Design Patterns: Prototype
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
* 2016
*
* Source code is licensed under MIT License
* (for more details see LICENSE)
*
*/
#include
#include
/*
* Prototype
* declares an interface for cloning itself
*/
class Prototype
{
public:
virtual ~Prototype() {}
virtual Prototype* clone() = 0;
virtual std::string type() = 0;
// ...
};
/*
* Concrete Prototype A and B
* implement an operation for cloning itself
*/
class ConcretePrototypeA : public Prototype
{
public:
~ConcretePrototypeA() {}
Prototype* clone()
{
return new ConcretePrototypeA();
}
std::string type()
{
return "type A";
}
// ...
};
class ConcretePrototypeB : public Prototype
{
public:
~ConcretePrototypeB() {}
Prototype* clone()
{
return new ConcretePrototypeB();
}
std::string type()
{
return "type B";
}
// ...
};
/*
* Client
* creates a new object by asking a prototype to clone itself
*/
class Client
{
public:
static void init()
{
types[ 0 ] = new ConcretePrototypeA();
types[ 1 ] = new ConcretePrototypeB();
}
static void remove()
{
delete types[ 0 ];
delete types[ 1 ];
}
static Prototype* make( const int index )
{
if ( index >= n_types )
{
return nullptr;
}
return types[ index ]->clone();
}
// ...
private:
static Prototype* types[ 2 ];
static int n_types;
};
Prototype* Client::types[ 2 ];
int Client::n_types = 2;
int main()
{
Client::init();
Prototype *prototype1 = Client::make( 0 );
std::cout << "Prototype: " << prototype1->type() << std::endl;
delete prototype1;
Prototype *prototype2 = Client::make( 1 );
std::cout << "Prototype: " << prototype2->type() << std::endl;
delete prototype2;
Client::remove();
return 0;
}