简单是事件实现机制

1.传统写法

class Son
{
public:
	void DoSomeThing()
	{
		cout << "爸爸回来了,赶紧去写作业" << endl
	}
};
class Wife
{
public:
	void DoSomeThing()
	{
		cout << "老公回来了,去做饭" << endl;
	}
};
class Me
{
public:
	void Gohome()
	{
		wife.DoSomeThing();
		son.DoSomeThing();
	}
private:
	Wife wife;
	Son son;
};
}
int main()
{
    Me me;
    me.Gohome();
}

2.简单的观察者

class ObserverInterface
{
public:
	virtual void DoSomeThingDoSomeThing() = 0;
	virtual ~ObserverInterface() = default;
};
class Mife : public ObserverInterface
{
public:
	void DoSomeThingDoSomeThing() override
	{
		cout << "老公回来了,该做饭了" << endl;
	}
};
class Son : public ObserverInterface
{
public:
	void DoSomeThingDoSomeThing() override
	{
		cout << "爸爸回来了,该做作业了" << endl;
	}
};

class SubjectInterface
{
public:
	virtual void Add(ObserverInterface* obr) = 0;
	virtual void Remove(ObserverInterface* obr) = 0;
	virtual void Notify() = 0;
	virtual ~SubjectInterface() = default;
};

class Me :public SubjectInterface
{
public:
	void Add(ObserverInterface* obr) override
	{
		m_observers.push_back(obr);
	}
	void Remove(ObserverInterface* obr) override
	{
		auto pos = std::find(m_observers.begin(), m_observers.end(), obr);
		if (pos != m_observers.end())
		{
			m_observers.erase(pos);
		}
	}
	void Notify() override
	{
		for (const auto& e : m_observers)
		{
			e->DoSomeThingDoSomeThing();
		}
	}
private:
	std::vector<ObserverInterface*> m_observers;
};
int main()
{
	Me me;
    ObserverInterface* wife = new Wife;
    ObserverInterface* son = new Son;
	me.Add(wife);
	me.Add(son);
	me.Notify(); 

	return 0;
}

3.简单的观察者(share_ptr)

class ObserverInterface
{
public:
	virtual void DoSomeThingDoSomeThing() = 0;
	virtual ~ObserverInterface() = default;
};
using pObserverInterface = std::shared_ptr<ObserverInterface>;

class Mife : public ObserverInterface
{
public:
	void DoSomeThingDoSomeThing() override
	{
		cout << "老公回来了,该做饭了" << endl;
	}
};
class Son : public ObserverInterface
{
public:
	void DoSomeThingDoSomeThing() override
	{
		cout << "爸爸回来了,该做作业了" << endl;
	}
};

class SubjectInterface
{
public:
	virtual void Add(pObserverInterface obr) = 0;
	virtual void Remove(pObserverInterface obr) = 0;
	virtual void Notify() = 0;
	virtual ~SubjectInterface() = default;
};

class Me :public SubjectInterface
{
public:
	void Add(pObserverInterface obr) override
	{
		m_observers.push_back(obr);
	}
	void Remove(pObserverInterface obr) override
	{
		auto pos = std::find(m_observers.begin(), m_observers.end(), obr);
		if (pos != m_observers.end())
		{
			m_observers.erase(pos);
		}
	}
	void Notify() override
	{
		for (const auto& e : m_observers)
		{
			e->DoSomeThingDoSomeThing();
		}
	}
private:
	std::vector<pObserverInterface> m_observers;
};
int main()
{
	Me me;
	auto wife = std::make_shared<Mife>();

	auto son = std::make_shared<Son>();
	me.Add(wife);
	me.Add(son);
	me.Notify(); 

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值