C++设计模式

C++设计模式是C++学习的重点之一,也是常用的处理问题的方法之一。

一、单例模式

     类只能实例化一个对象

  • 屏蔽构造函数
  • 提供接口生成唯一对象 (1)不依赖对象 (2)不能返回类类型

(1)懒汉模式  延时加载  双重锁机制

class SingleTon
{
public:
	static SingleTon* getInstance()
	{
		if (psingle == NULL)
		{
			lock();
			if (psingle == NULL)
			{
				psingle = new SingleTon();
			}
			unlock();
		}
		return psingle;
	}
private:
	SingleTon(){}
	SingleTon(const SingleTon&);
	static SingleTon* psingle;
};
SingleTon* SingleTon::psingle = NULL;
int main()
{
	return 0;
}

(2)饿汉模式  贪婪加载(一旦加载,单例初始化完成) 

class SingleTon
{
public:
	static SingleTon* getInstance()
	{
		return psingle;
	}
private:
	SingleTon(){}
	SingleTon(const SingleTon&);
	static SingleTon* psingle;
};
SingleTon* SingleTon::psingle = new SingleTon();
int main()
{
	return 0;
}

二、工厂模式

工厂模式是一种创建型模式,它提供了一种创建对象的最佳方法,将大量有共同接口的类实例化,工厂模式可以动态决定将哪个类实例化,不必实现直到每次要哦实例化哪个类

(1)简单工厂模式

         即一个工厂多个产品,产品需要有一个虚基类,通过传入参数,生成具体的产品对象,并利用基类指针指向词对象。

        缺点:当增加新的产品时,需要在工厂内修改代码,违反扩展开放,对修改关闭的原则

class Fruit
{
public:
	Fruit(std::string name) :mname(name){}
	virtual void operation() = 0;
	virtual ~Fruit(){}
protected:
	std::string mname;
};

class Apple:public Fruit
{
public:
	Apple(std::string name) :Fruit(name){}
	virtual void operation()
	{
		std::cout <<"this is an apple!" << std::endl;
	}
};

class Banana :public Fruit
{
public:
	Banana(std::string name) :Fruit(name){}
	virtual void operation()
	{
		std::cout <<"this is an banana!" << std::endl;
	}
};

class Factory
{
public:
	Fruit* createFruit(int flag)
	{
		switch (flag)
		{
		case 1:
			return new Apple("apple");
			break;
		case 2:
			return new Banana("banana");
			break;
		default:
			std::cout << "flag is error!" << std::endl;
			return NULL;
			break;
		}
	}
};
int main()
{
	Factory f;
	Fruit* pf = f.createFruit(2);
	pf->operation();
	return 0;
}

(2)工厂方法模式

         多个工厂,多个产品,每个产品对应于一个工厂。工厂和产品都需要通过虚基类来构建,工厂方法模式增加新的产品会对应增加新的工厂,属于扩展,不会修改之前的产品的工厂的代码。

class Fruit
{
public:
	Fruit(std::string name) :mname(name){}
	virtual void operation() = 0;
	virtual ~Fruit(){}
protected:
	std::string mname;
};
class Apple:public Fruit
{
public:
	Apple(std::string name) :Fruit(name){}
	virtual void operation()
	{
		std::cout <<"this is an apple!" << std::endl;
	}
};
class Banana :public Fruit
{
public:
	Banana(std::string name) :Fruit(name){}
	virtual void operation()
	{
		std::cout <<"this is an banana!" << std::endl;
	}
};
class Factory
{
public:
	Factory(std::string name):mname(name){}
	virtual Fruit* createFruit() = 0;
protected:
	std::string mname;
};
class Factory_1:public Factory
{
public:
	Factory_1(std::string name) :Factory(name){}
	virtual Fruit* createFruit()
	{
		return new Apple("apple");
	}
};
class Factory_2:public Factory
{
public:
	Factory_2(std::string name) :Factory(name){}
	virtual Fruit* createFruit()
	{
		return new Banana("banana");
	}
};

class Pear :public Fruit
{
public:
	Pear(std::string name) :Fruit(name){}
	virtual void operation()
	{
		std::cout << "this is a pear!" << std::endl;
	}
};

class Factory_3 :public Factory
{
public:
	Factory_3(std::string name) :Factory(name){}
	virtual Fruit* createFruit()
	{
		return new Pear("pear");
	}
};

int main()
{
	Factory* pf1 = new Factory_3("f3");
	Factory* pf2 = new Factory_2("f2");
	Fruit* pff1 = pf1->createFruit();
	Fruit* pff2 = pf2->createFruit();
	pff1->operation();
	pff2->operation();
	return 0;
}

(3)抽象工厂模式

        即多个工厂,多个产品,并且每个产品可以包含多个型号,产品和方法均通过虚基类的方式构建,每个工厂可以同一个产品的多个型号。

class A
{
public:
	A(std::string name) :mname(name){}
	virtual void operation() = 0;
protected:
	std::string mname;
};
class A1:public A
{
public:
	A1(std::string name) :A(name){}
	virtual void operation()
	{
		std::cout << "A1" << std::endl;
	}
};
class A2:public A
{
public:
	A2(std::string name) :A(name){}
	virtual void operation()
	{
		std::cout << "A2" << std::endl;
	}
};

class B
{
public:
	B(std::string name) :mname(name){}
	virtual void operation() = 0;
protected:
	std::string mname;
};
class B1:public B
{
public:
	B1(std::string name) :B(name){}
	virtual void operation()
	{
		std::cout << "B1" << std::endl;
	}
};
class B2:public B
{
public:
	B2(std::string name) :B(name){}
	virtual void operation()
	{
		std::cout << "B2" << std::endl;
	}
};

class AbstractFactory
{
public:
	AbstractFactory(std::string name) :mname(name){}
	virtual A* createA() = 0;
	virtual B* createB() = 0;
protected:
	std::string mname;
};
class D1 :public AbstractFactory
{
public:
	D1(std::string name) :AbstractFactory(name){}
	virtual A* createA()
	{
		return new A1("a1");
	}
	virtual B* createB()
	{
		return new B1("b1");
	}
};
class D2 :public AbstractFactory
{
public:
	D2(std::string name) :AbstractFactory(name){}
	virtual A* createA()
	{
		return new A2("a2");
	}
	virtual B* createB()
	{
		return new B2("b2");
	}
};

int main()
{
	AbstractFactory* paf = new D2("d2");
	A* pa = paf->createA();
	B* pb = paf->createB();
	pa->operation();
	pb->operation();
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值