设计模式 一

#include 

using namespace std;
class Bankworker
{
public:
	virtual void work() = 0;
};
class Bankworker_Save:public Bankworker
{
public:
	void work()
	{
		cout << "存钱" << endl;
	}
};
class Bankworker_transform:public Bankworker
{
public:
	void work()
	{
		cout << "转账" << endl;
	}
};
class Bankworker_getmoney:public Bankworker
{
public:
	void work()
	{
		cout << "取钱" << endl;
	}
};
int main()
{
	Bankworker *pb1 = new Bankworker_Save;
	pb1->work();

	Bankworker *pb2 = new Bankworker_transform;
	pb2->work();

	/*Bankworker *pb3 = new Bankworker_getmoney;
	pb3->work();*/


	system("pause");
	return 0;
}
#include 

using namespace std;

class CPU
{
public:
	virtual void work() = 0;
};
class CPU_inter:public CPU
{
public:
	void work()
	{
		cout << "inter CPU 工作" << endl;
	}
};
class CPU_SM :public CPU
{
public:
	void work()
	{
		cout << "SM CPU 工作" << endl;
	}
};

class YP
{
public:
	virtual void work() = 0;
};
class YP_SM :public YP
{
public:
	void work()
	{
		cout << "SM YP 工作" << endl;
	}
};
class YP_XJ :public YP
{
public:
	void work()
	{
		cout << "XJ YP 工作" << endl;
	}
};
class Computer
{
public:

	void setCPU(CPU *cpu)
	{
		this->cpu = cpu;
	}
	void setYP(YP *yp)
	{
		this->yp = yp;
	}

	void setComputer()
	{
		cpu->work();
		yp->work();
	}
private:
	CPU *cpu;
	YP *yp;
};
int main()
{
	Computer c;

	CPU *cpu = new CPU_inter;
	c.setCPU(cpu);

	YP *yp = new YP_SM;
	c.setYP(yp);

	YP *yp1 = new YP_XJ;
	c.setYP(yp1);

	CPU *cpu1 = new CPU_SM;
	c.setCPU(cpu1);

	c.setComputer();



	system("pause");
	return 0;
}

你可能感兴趣的:(设计模式 一)