网站首页 > 博客文章 正文
前言:
很开心自己的笔记收获了一百位粉丝,想写在前面告诉大家,我也是一位跨专业学生,自己也在学习的过程中,发布的文章均为自己的笔记(未校对),不建议一些正在学习的同学把我的笔记作为资料.大家还是找一些专业的辅导机构或者是大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;
}
- 上一篇: 设计模式——备忘录模式(备忘录界面设计)
- 下一篇: 状态机的简单应用(状态机的优点)
猜你喜欢
- 2024-10-01 操作系统 : 按优先数调度算法实现处理器调度(C++)
- 2024-10-01 c++ 疑难杂症(13) allocator(c++ catch all exception)
- 2024-10-01 百度C++工程师的那些极限优化(内存篇)
- 2024-10-01 C++20 香不香?从四大新特性看起(c++20支持)
- 2024-10-01 用C++11打造智能观察者模式:详解实现步骤完整示例代码
- 2024-10-01 网络编程:手绘TCP状态机(tcp状态转换图详解)
- 2024-10-01 C++为什么不提倡使用单例模式?(c++为什么不用printf)
- 2024-10-01 智能系统机器人!C++实现三阶魔方自动求解程序源码
- 2024-10-01 ChaosBlade 发布对 C++ 应用混沌实验的支持
- 2024-10-01 C/C++编程笔记:C++智能指针及其类型的介绍!重点分析
你 发表评论:
欢迎- 07-07Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- 07-07Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- 07-07Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- 07-07Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- 07-07IT运维必会的30个工具(it运维工具软件)
- 07-07开源项目有你需要的吗?(开源项目什么意思)
- 07-07自动化测试早就跑起来了,为什么测试管理还像在走路?
- 07-07Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- 最近发表
-
- Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- IT运维必会的30个工具(it运维工具软件)
- 开源项目有你需要的吗?(开源项目什么意思)
- 自动化测试早就跑起来了,为什么测试管理还像在走路?
- Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- Cursor 太贵?这套「Cline+OpenRouter+Deepseek+Trae」组合拳更香
- 为什么没人真的用好RAG,坑都在哪里? 谈谈RAG技术架构的演进方向
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)