中介者模式-C++实现
2016-07-18 22:09
369 查看
跟我在公司搭的框架好像。。
// MediatorPattern.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student
{
private:
string m_strName;
string m_content;
public:
Student(string strName):m_strName(strName){}
void set_name(string name)
{
m_strName = name;
}
void set_content(string content)
{
m_content = content;
}
string get_name()
{
return m_strName;
}
string get_content()
{
if (m_content.empty())
return "Copy that";
else
return m_content;
}
virtual void talk() = 0;
};
class Monitor :public Student
{
public:
Monitor(string str):Student(str){}
virtual void talk()
{
cout << "班长" << get_name() << "说:" << get_content() << endl;
}
};
class Secretary :public Student
{
public:
Secretary(string str):Student(str){}
virtual void talk()
{
cout << "团支书" << get_name() << "说:" << get_content() << endl;
}
};
class StudentA :public Student
{
public:
StudentA(string str) :Student(str) {}
virtual void talk()
{
cout << "学生A" << get_name() << "说:" << get_content() << endl;
}
};
class Mediator
{
public:
vector<Student*> vec_student;
void addstu(Student* stu)
{
vec_student.push_back(stu);
}
virtual void notify(Student* stu) {};
};
class QQMediator :public Mediator
{
public:
virtual void notify(Student* stu)
{
stu->talk();
for (int i = 0; i < vec_student.size(); ++i)
{
if (stu != vec_student[i])
{
vec_student[i]->talk();
}
}
}
};
int main()
{
QQMediator qq;
Monitor* mon = new Monitor("mon");
Secretary *sec = new Secretary("sec");
StudentA *stu = new StudentA("stu");
qq.addstu(mon);
qq.addstu(sec);
qq.addstu(stu);
mon->set_content("明天放假");
qq.notify(mon);
return 0;
}
// MediatorPattern.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student
{
private:
string m_strName;
string m_content;
public:
Student(string strName):m_strName(strName){}
void set_name(string name)
{
m_strName = name;
}
void set_content(string content)
{
m_content = content;
}
string get_name()
{
return m_strName;
}
string get_content()
{
if (m_content.empty())
return "Copy that";
else
return m_content;
}
virtual void talk() = 0;
};
class Monitor :public Student
{
public:
Monitor(string str):Student(str){}
virtual void talk()
{
cout << "班长" << get_name() << "说:" << get_content() << endl;
}
};
class Secretary :public Student
{
public:
Secretary(string str):Student(str){}
virtual void talk()
{
cout << "团支书" << get_name() << "说:" << get_content() << endl;
}
};
class StudentA :public Student
{
public:
StudentA(string str) :Student(str) {}
virtual void talk()
{
cout << "学生A" << get_name() << "说:" << get_content() << endl;
}
};
class Mediator
{
public:
vector<Student*> vec_student;
void addstu(Student* stu)
{
vec_student.push_back(stu);
}
virtual void notify(Student* stu) {};
};
class QQMediator :public Mediator
{
public:
virtual void notify(Student* stu)
{
stu->talk();
for (int i = 0; i < vec_student.size(); ++i)
{
if (stu != vec_student[i])
{
vec_student[i]->talk();
}
}
}
};
int main()
{
QQMediator qq;
Monitor* mon = new Monitor("mon");
Secretary *sec = new Secretary("sec");
StudentA *stu = new StudentA("stu");
qq.addstu(mon);
qq.addstu(sec);
qq.addstu(stu);
mon->set_content("明天放假");
qq.notify(mon);
return 0;
}
相关文章推荐
- c++教程(二:Structure of a program)
- c++ 中 define
- C++ inline函数相关总结
- C++的继承与多态
- c++教程(一:Compilers)
- luogu1328[NOIP2014 T1]生活大爆炸版石头剪刀布
- 小波变换C++实现(一)----单层小波变换
- C++静态多态CRTP
- S3C2440A地址空间
- C++虚继承相关问题
- C++设计模式<六>:Decorator装饰模式
- C++ 提高3 STL基本概念 string 迭代器 容器
- LLVM与Clang的概述及关系
- c语言==一维数组与二维数组的详解(9)
- 备战NOIP每周写题记录(一)···不间断更新
- ROS学习之 cpp回调函数和轮转(spin)
- 01背包
- c语言中i++与++i的使用技巧
- 动态存储分配
- 【图解】Dev C++单步调试的方法