#include
using namespace std;
class Singleton
{
private:
Singleton()
{
cout << "Singleton 构造函数执行!" << endl;
}
public:
static Singleton *getInstance()
{
if (m_pSingleton == NULL) //懒汉式单例:1、每次获取实例都要判断;2、多线程会有问题
{
m_pSingleton = new Singleton;
}
return m_pSingleton;
}
static void freeInstance()
{
if (m_pSingleton != NULL)
{
delete m_pSingleton;
m_pSingleton = NULL;
}
}
private:
static Singleton *m_pSingleton;
};
Singleton* Singleton::m_pSingleton = NULL;
void main()
{
Singleton *p1 = Singleton::getInstance();//懒汉式——只有在获取单例时,才把对象new出来
Singleton *p2 = Singleton::getInstance();
if (p1 == p2)
{
cout << "p1和p2是同一个对象!" << endl;
}
else{
cout << "p1和p2不是同一个对象!" << endl;
}
Singleton::freeInstance();
}
//Singleton 构造函数执行一次!打印p1和p2是同一个对象!
为什么说懒汉式遇上多线程会出现问题?详细内容见:懒汉式单例的多线程问题——
#include
using namespace std;
class Singleton
{
private:
Singleton()
{
cout << "Singleton 构造函数执行!" << endl;
}
public:
static Singleton *getInstance()
{
//if (m_pSingleton == NULL) //懒汉式单例:1、每次获取实例都要判断——降低了效率;2、多线程会有问题
//{
// m_pSingleton = new Singleton;
//}
return m_pSingleton;
}
static void freeInstance()
{
if (m_pSingleton != NULL)
{
delete m_pSingleton;
m_pSingleton = NULL;
}
}
private:
static Singleton *m_pSingleton;
};
//Singleton* Singleton::m_pSingleton = NULL; //懒汉式——懒汉式 并没有创建单例对象
Singleton* Singleton::m_pSingleton = new Singleton; //饿汉式——不管你创建不创建实例,均把实例new出来,浪费了内存空间
void main()
{
Singleton *p1 = Singleton::getInstance(); //懒汉式——只有在获取单例时,才把对象new出来
Singleton *p2 = Singleton::getInstance();
if (p1 == p2)
{
cout << "p1和p2是同一个对象!" << endl;
}
else{
cout << "p1和p2不是同一个对象!" << endl;
}
Singleton::freeInstance();
}
//Singleton 构造函数执行一次!打印p1和p2是同一个对象!
class Fruit
{
public:
virtual void getFruit() = 0;
};
class Banana : public Fruit
{
public:
virtual void getFruit()
{
cout << "我是香蕉...." << endl;
}
};
class Apple : public Fruit
{
public:
virtual void getFruit()
{
cout << "我是苹果...." << endl;
}
};
class Factory
{
public:
Fruit *create(char *p)
{
if (strcmp(p, "banana") == 0)
{
return new Banana;
}
else if (strcmp(p, "apple") == 0)
{
return new Apple;
}
else
{
printf("不支持\n" ) ;
return NULL;
}
}
};
void main()
{
Factory *f = new Factory;
Fruit *fruit = NULL;
//工厂生产 香蕉
fruit = f->create("banana");
fruit->getFruit();
delete fruit;
//工厂生产苹果
fruit = f->create("apple");
fruit->getFruit();
delete fruit;
delete f;
}
在这个模式中,工厂类是整个模式的关键所在,它包含必要的判断逻辑,能够根据外界给定的信息,决定究竟应该创建哪个具体类的对象。用户在使用时可以直接根据工厂类去创建所需的实例,而无需了解这些对象是如何创建以及如何组织的。有利于整个软件体系结构的优化。简单工厂模式的缺点也正体现在其工厂类上,由于工厂类集中了所有实例的创建逻辑,所以“高内聚”方面做的并不好。另外,当系统中的具体产品类不断增多时,可能会出现要求工厂类也要做相应的修改,扩展性并不很好。
核心思想是用一个工厂来根据输入的条件产生不同的类,然后根据不同类的virtual函数得到不同的结果。
class Fruit
{
public:
virtual void sayname() = 0;
};
class Banana : public Fruit
{
public:
void sayname()
{
cout << "我是香蕉" << endl;
}
};
class Apple : public Fruit
{
public:
void sayname()
{
cout << "我是 Apple" << endl;
}
};
class AbFactory //抽象工厂
{
public:
virtual Fruit *CreateProduct() = 0;
};
class BananaFactory :public AbFactory //具体工厂
{
public:
virtual Fruit *CreateProduct()
{
return new Banana;
}
};
class AppleFactory :public AbFactory //具体工厂
{
public:
virtual Fruit *CreateProduct()
{
return new Apple;
}
};
/////////////////////////////////////////////////////////////////////////////////
//添加新的产品——添加新功能,增加代码,而不是去修改原来的代码(比较简单工厂模式)
class Pear : public Fruit
{
public:
virtual void sayname()
{
cout << "我是 pear" << endl;
}
protected:
private:
};
class PearFactory : public AbFactory
{
public:
virtual Fruit *CreateProduct()
{
return new Pear;
}
};
void main()
{
AbFactory *factory = NULL;
Fruit *fruit = NULL;
//吃 香蕉
factory = new BananaFactory;
fruit = factory->CreateProduct();
fruit->sayname();
delete fruit;
delete factory;
//Pear
factory = new PearFactory;
fruit = factory->CreateProduct();
fruit->sayname();
delete fruit;
delete factory;
}
具体工厂类都有共同的接口,或者有共同的抽象父类。当系统扩展需要添加新的产品对象时,仅仅需要添加一个具体对象以及一个具体工厂对象,原有工厂对象不需要进行任何修改,也不需要修改客户端,很好的符合了“开放-封闭”原则。
class Fruit
{
public:
virtual void SayName() = 0;
};
class AbstractFactory
{
//产品族
public:
virtual Fruit* CreateBanana() = 0;
virtual Fruit* CreateApple() = 0;
};
class NorthBanana : public Fruit
{
public:
virtual void SayName()
{
cout << "我是北方香蕉" << endl;
}
};
class NorthApple : public Fruit
{
public:
virtual void SayName()
{
cout << "我是北方苹果" << endl;
}
};
class SourthBanana : public Fruit
{
public:
virtual void SayName()
{
cout << "我是南方香蕉" << endl;
}
};
class SourthApple : public Fruit
{
public:
virtual void SayName()
{
cout << "我是南方苹果" << endl;
}
};
class NorthFacorty : public AbstractFactory //北方工厂
{
//产品族
virtual Fruit * CreateBanana()
{
return new NorthBanana;
}
virtual Fruit * CreateApple()
{
return new NorthApple;
}
};
class SourthFacorty : public AbstractFactory //南方工厂
{
//产品族
virtual Fruit * CreateBanana()
{
return new SourthBanana;
}
virtual Fruit * CreateApple()
{
return new SourthApple;
}
};
void main()
{
//客户端面向抽象类编程
Fruit *fruit = NULL;
AbstractFactory *af = NULL;
//------
af = new SourthFacorty; //南方工厂
fruit = af->CreateApple();
fruit->SayName();
delete fruit;
fruit = af->CreateBanana();
fruit->SayName();
delete fruit;
//------
af = new NorthFacorty; //北方工厂
fruit = af->CreateApple();
fruit->SayName();
delete fruit;
fruit = af->CreateBanana();
fruit->SayName();
delete fruit;
delete af;
}
class House
{
public:
void setDoor(string door)
{
this->m_door = door;
}
void setWall(string wall)
{
this->m_wall = wall;
}
void setWindow(string window)
{
this->m_window = window;
}
//--
string getDoor()
{
cout << m_door << endl;
return this->m_door;
}
string getWall()
{
cout << m_wall << endl;
return this->m_wall;
}
string getWindow()
{
cout << m_window << endl;
return m_window;
}
private:
string m_door;
string m_wall;
string m_window;
};
class Builder //把工程队抽象出来——实现多个、多种工程队
{
public:
virtual void buildWall() = 0;
virtual void buildDoor() = 0;
virtual void buildWindow() = 0;
virtual House* getHouse() = 0; //获取结果
};
//公寓 flat 工程队
class FlatBuilder : public Builder
{
public:
FlatBuilder()
{
m_house = new House;
}
virtual void buildWall()
{
m_house->setWall("flat wall");
}
virtual void buildDoor()
{
m_house->setDoor("flat door");
}
virtual void buildWindow()
{
m_house->setWindow("flat window");
}
virtual House* getHouse()
{
return m_house;
}
private:
House *m_house;
};
//别墅 villa 工程队
class VillaBuilder : public Builder
{
public:
VillaBuilder()
{
m_house = new House;
}
virtual void buildWall()
{
m_house->setWall("villa wall");
}
virtual void buildDoor()
{
m_house->setDoor("villa door");
}
virtual void buildWindow()
{
m_house->setWindow("villa window");
}
virtual House* getHouse()
{
return m_house;
}
private:
House *m_house;
};
//设计师(指挥者):负责建造业务逻辑,修改该类可以随时改变业务逻辑
//建筑队:干具体的活
//这就是 对象的构建 和 对象的表示 进行分离,把 建造具体事务过程 和 建造业务逻辑 进行分离
class Director
{
public:
Director(Builder * build)
{
m_build = build;
}
void Construct()
{
m_build->buildWall();
m_build->buildWindow();
m_build->buildDoor();
}
private:
Builder * m_build;
};
void main()
{
/*
客户直接造房子
House *house = new House;
house->setDoor("门");
house->setWall("墙面");//一方面,该建造过程较为复杂,所以应该单独地封装为一个类
house->setWindow("窗口");//另一方面,如果在客户端需要增加建造过程中的步骤,还需要去修改“该类”的源代码
delete house;
*/
House *house = NULL;
Director *director = NULL;
Builder *builder = NULL;
cout << "Villa:" << endl;
// 请一个建造别墅 Villa 的工程队
builder = new VillaBuilder;
//设计师 指挥 工程队 干活
director = new Director(builder);
director->Construct();
house = builder->getHouse();
house->getWall();
house->getWindow();
house->getDoor();
delete house;
delete builder;
cout << endl << "Flat:" << endl;
//请 FlatBuilder 公寓 工程队
builder = new FlatBuilder;
director = new Director(builder);
director->Construct();
house = builder->getHouse();
house->getWall();
house->getWindow();
house->getDoor();
delete house;
delete builder;
delete director;
}
Factory模式:
class Person
{
public:
virtual Person* clone() = 0; //让该类具有自我复制功能(当该对象的复制较为复杂时)
virtual void printT() = 0;
};
class CPlusPlusProgrammer : public Person
{
public:
CPlusPlusProgrammer()
{
m_name = "";
m_age = 0;
m_resume = NULL;
setResume("英语六级、GRE");
}
CPlusPlusProgrammer(string name, int age)
{
m_name = name;
m_age = age;
m_resume = NULL;
setResume("英语六级、GRE");
}
void setResume(char *p)
{
if (m_resume != NULL)
{
delete m_resume;
}
m_resume = new char[strlen(p) + 1];//下图扭转指针,防止浅拷贝
strcpy(m_resume, p);
}
virtual void printT()
{
cout << "m_name:" << m_name << " m_age:" << m_age << " m_resume:" << m_resume << endl;
}
virtual Person* clone() //克隆接口的实现
{
CPlusPlusProgrammer *tmp = new CPlusPlusProgrammer;
*tmp = *this; // =操作 浅拷贝
return tmp;
}
private:
string m_name;
int m_age;
char *m_resume; //类中有指针,涉及到浅拷贝、深拷贝的问题
};
void main()
{
Person *c1 = new CPlusPlusProgrammer("张三", 32);
c1->printT();
Person *c2 = c1->clone(); //复制,尤其注意m_resume虽然最后打印结果一样,但是指向的是两块不同的内存空间
c2->printT();
}
上述介绍了几种创建型模式——单例模式、简单工厂模式,多态工厂模式、抽象工厂模式、建造者模式、原型模式(prototype),接下来介绍结构型模式http://blog.csdn.net/songshimvp1/article/details/48970017——代理模式、装饰模式、适配器模式(adapter)、组合模式、桥接模式(bridge)、外观模式(facade)、享元模式(flyweight)。