专业的编程技术博客社区

网站首页 > 博客文章 正文

C++ 设计模式 状态模式/解释器模式/迭代器模式(含代码)

baijin 2024-10-01 07:34:15 博客文章 8 ℃ 0 评论

前言:

很开心自己的笔记收获了一百位粉丝,想写在前面告诉大家,我也是一位跨专业学生,自己也在学习的过程中,发布的文章均为自己的笔记(未校对),不建议一些正在学习的同学把我的笔记作为资料.大家还是找一些专业的辅导机构或者是大v.有错误的地方会及时纠正.谢谢大家

状态模式就是对象的行为依赖于他所处的状态

通过用户的状态改变用户的行为

案例

我在八点起床,吃早饭,九点出发去上班,十点开始工作,十点吃午饭,我在不同的时间要干不同的事情.

#include<iostream>
#include"string"
#include"list"
using namespace std;

class State
{
public:
	virtual void doSomeThing(Worker *w) = 0;
};
class Worker
{
public:
	Worker();
	int getHour()
	{
		return m_hour;
	}
	void setHour(int hour)
	{
		m_hour = hour;
	}
	State* getCurrentState()

	{
		return m_currstate;
	}
 void  setCurrentState(State* state)

	{
		m_currstate=state;
	}
 void doSomeThing()
 {

	 m_currstate->doSomeThing(this);
 }

private:
	int m_hour;
	State  *m_currstate;
	};
class Statel :public State
{
public:
	void doSomeThing(Worker *w);

};
class State2 :public State
{
public:
	void doSomeThing(Worker *w);

};
void Statel::doSomeThing(Worker *w)
{
	if (w->getHour() == 7 || w->getHour() == 8)
	{
		cout << "吃早饭" << endl;
	}
	else
	{
		delete w->getCurrentState();
		w->getCurrentState(new State2);
		w->getCurrentState()->doSomeThing(w);
	}
}
void State2::doSomeThing(Worker *w)
{
	if (w->getHour() == 9 || w->getHour() == 10)
	{
		cout <<"逛街,买衣服,买鞋子" << endl;
	}
	else
	{
		delete w->getCurrentState();
		w->getCurrentState(new  State1);
		cout << "当前时间" << w->getHour() << "未知状态 " << endl;
	}
}
Worker::Worker()
{
	m_currstate = new State1;
}
void main()
{
	Worker *w1 = new Worker;
	w1->getHour(7);
	w1->doSomeThing();
	w1->getHour(9);
	w1->doSomeThing();

	delete w1;

	cout << "hello..." << endl;
	system("pause");
	return;
}

解释器模式就是描述了如何为简单的语言定义一个语法,如何在该语言中表示一个句子,以及如何解释这些句子

#include<iostream>
#include"string"
#include"list"
using namespace std;

class  Context
{
public:
	Context(int num)
	{
		this->m_num = num;
	}
	int getNum()
	{
		return m_num;
	}
	int getRes()
	{
		return m_res;
	}
	void setNum(int num)
	{
		this->m_num = num;
	}
	void setRes(int res)
	{
		this->m_res = res;
	}
private:
	int m_num;
	int m_res;
};
class Experssion
{
public:
	
	virtual void interpreter(Context *context) = 0;
private:
	Context *m_context;
};
//加法
class PlusExpression :public Experssion
{
public:
	PlusExpression()
	{
		this->context =NULL;
	}
	virtual void interpreter(Context*context)
	{
		int num = context->getNum();
		num++;
		context->setNum(num);
		context->setRes(num);
	}
private:
	Context *context;
};


class MinusExpression :public Experssion
{
public:
	MinusExpression()
	{
		this->context = NULL;
	}
	virtual void interpreter(Context*context)
	{
		int num = context->getNum();
		num--;
		context->setNum(num);
		context->setRes(num);
	}
private:
	Context *context;
};


void main()
{
	Experssion     *experssion = NULL;
	Context        *context = NULL;
	Experssion     *experssion2 = NULL;
	context = new Context(10);
	cout << context->getNum() << endl;
	experssion = new PlusExpression;
	expression->interpreter(context);


	cout << context->getRes() << endl;
	experssion2 = new MinusExpression;
    expression2->interpreter(context);
	cout << context->getRes() << endl;
	cout << "hello..." << endl;
	system("pause");
	return;
}

迭代器模式是提供了一种方法顺序来访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示


#include"iostream"
using namespace std;
typedef int Object;
#define SIZE 5
class MyIterator
{
public:
	virtual void First() = 0;
	virtual void Next() = 0;
	virtual bool IsDone() = 0;
	virtual Object CurrentItem() = 0;
};
class Aggregate
{
public:
	virtual Object getItem(int index) = 0;
	virtual MyIterator *CreateIterator() = 0;
	virtual int getSize() = 0;

protected :
	Object  object[SIZE];
};
class ContreteIterator :public MyIterator
{
	
public:
	ContreteIterator(Aggregate *ag)
	{
	_ag = ag;
	_current_index = 0;
	}
	
	virtual void First()
	{
		_current_index = 0;
	}
	virtual void Next()
	{
		if (_current_index < _ag->getSize())
		{
			_current_index++;
		}
	}
	virtual bool IsDone()
	{
		return (_current_index == _ag->getSize());
	}
	virtual Object CurrentItem()
	{
		return _ag->getItem(_current_index);
	}
	
private:
	int    _current_index;
	Aggregate  *_ag;
};
class ContreteAggregate :public Aggregate
{
public:
	ContreteAggregate()
	{
		for (int i = 0; i < SIZE; i++)
		{
			object[i] = i + 100;
		}
	}
	virtual Object getItem(int index)
	{
		return object[index];
	}
	virtual MyIterator *CreateIterator()
	{
		return new ContreteIterator(this);
	}
	virtual int getSize() 
	{
		return SIZE;
	}


};
void main()
{
	Aggregate *ag = new ContreteAggregate;
	MyIterator  *it = ag->CreateIterator();
	for (; !(it->IsDone()); it->Next())
	{
		cout << it->CurrentItem() << "";
	}
	delete it;
	delete ag;
	system("pause");
	return;
}


Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表