⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 command.cpp

📁 自己写的c++实现的headfirst中的设计模式
💻 CPP
字号:
/* 命令模式
 */

#include <iostream>
#include <string>
#include <vector>
using namespace std;

//各类电器  //已提供各命令接口
class Light
{
private:
	string name;
	bool isOn;
	Light(){};
	
public:
	Light(string n):name(n),isOn(0){}	//按顺序声明!
	
	void on()
	{
		isOn=1;
		cout << "Light: "<<name << " Light ON!"<<endl;
	}
	
	void off()
	{
		isOn=0;
		cout << "Light: "<<name << " Light OFF!"<<endl;
	}
	
};

class TV
{
private:
	bool isOn;
	int set;
	int valum;
	
public:
	TV():isOn(0),set(1),valum(5){}
	
	void on()
	{
		isOn=1;
		cout << "TV: is ON!" <<endl;
	}
	
	void off()
	{
		isOn=0;
		cout << "TV: is OFF!" << endl;
	}
	
	void nextSet()
	{
		cout << "TV: Next -> It's Set NO." << ++set <<" Now."<<endl;
	}
	
	void frontSet()
	{
		cout << "TV: Front -> It's Set NO." << --set <<" Now."<<endl;
	}
	
	void plusValum()
	{
		cout << "TV: Valum Plus -> It's Valum "<< ++valum <<" Now." <<endl;
	}
	
	void minusValum()
	{
		cout << "TV: Valum Minus -> It's Valum "<< --valum <<" Now." <<endl;
	}
	
};

//指令
class Command
{
public:
	virtual void execute()=0;
};

class CMDLightOn:public Command
{
private:
	Light* l;
	CMDLightOn();
	
public:
	CMDLightOn(Light& tmp):l(&tmp){}
	
	void execute()
	{
		l->on();
	}
};

class CMDLightOff:public Command
{
private:
	Light* l;
	CMDLightOff();
	
public:
	CMDLightOff(Light& tmp):l(&tmp){}
	
	void execute()
	{
		l->off();
	}
};

class CMDTVOn:public Command
{
private:
	TV* t;
	CMDTVOn();
	
public:
	CMDTVOn(TV& tmp):t(&tmp){}
	
	void execute()
	{
		t->on();
	}
};

class CMDTVOff:public Command
{
private:
	TV* t;
	CMDTVOff();
	
public:
	CMDTVOff(TV& tmp):t(&tmp){}
	
	void execute()
	{
		t->off();
	}
};

class CMDTVNextSet:public Command
{
private:
	TV* t;
	CMDTVNextSet();
	
public:
	CMDTVNextSet(TV& tmp):t(&tmp){}
	
	void execute()
	{
		t->nextSet();
	}
};

class CMDTVFrontSet:public Command
{
private:
	TV* t;
	CMDTVFrontSet();
	
public:
	CMDTVFrontSet(TV& tmp):t(&tmp){}
	
	void execute()
	{
		t->frontSet();
	}
};

class CMDTVPlusValum:public Command
{
private:
	TV* t;
	CMDTVPlusValum();
	
public:
	CMDTVPlusValum(TV& tmp):t(&tmp){}
	
	void execute()
	{
		t->plusValum();
	}
};

class CMDTVMinusValum:public Command
{
private:
	TV* t;
	CMDTVMinusValum();
	
public:
	CMDTVMinusValum(TV& tmp):t(&tmp){}
	
	void execute()
	{
		t->minusValum();
	}
};

class CMDClub:public Command
{
public:	
	void execute()
	{}
};

//控制台
class Contraler
{
private:
	Command* cmd[10]; 	//How to use vector?
	//Contraler();
	string cmt[10];
	
public:
	void setCMD(int n,Command* c,string t)
	{
		if(n<10&&n>=0) 
		{
			cmd[n]=c;
			cmt[n]=t;
		}
		else {cout <<"wrong!"<<endl;}
	}
	
	void execute(int n)
	{
		if(n<10&&n>=0) cmd[n]->execute();
		else { cout <<"wrong!"<<endl;}
	}
	
	void display()
	{
		int i;
		for(i=0;i<=9;i++) cout << cmt[i] << endl;
	}
};

void command_main()
{
	//制作电器
	Light l_hall("Hall");
	Light l_bedroom("Bedroom");
	TV t;
	
	//建造指令条
	CMDLightOn l_hall_on(l_hall);
	CMDLightOff l_hall_off(l_hall);
	CMDLightOn l_bedroom_on(l_bedroom);
	CMDLightOff l_bedroom_off(l_bedroom);
	CMDTVOn t_on(t);
	CMDTVOff t_off(t);
	CMDTVNextSet t_next(t);
	CMDTVFrontSet t_front(t);
	CMDTVPlusValum t_plus(t);
	CMDTVMinusValum t_minus(t);
	
	//制作控制台
	Contraler hand;
	hand.setCMD(0,&l_hall_on,"Hall Light : ON");
	hand.setCMD(1,&l_hall_off,"Hall Light : OFF");
	hand.setCMD(2,&l_bedroom_on,"Bedroom Light : ON");
	hand.setCMD(3,&l_bedroom_off,"Bedroom Light : OFF");
	hand.setCMD(4,&t_on,"TV : ON");
	hand.setCMD(5,&t_off,"TV : OFF");
	hand.setCMD(6,&t_front,"TV : Front Set");
	hand.setCMD(7,&t_next,"TV : Next Set");
	hand.setCMD(8,&t_plus,"TV : Plus Valum");
	hand.setCMD(9,&t_minus,"TV : Minus Valum");

	//测试控制台	//读取一个字符函数?
	hand.display();
	while(1)
	{
		hand.execute(getchar()-48);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -