// test_demo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
class L1Provider
{
public:
virtual void L1Service() = 0;
};
class L2Provider
{
public:
virtual void L2Service() = 0;
void SetLowerLayer(L1Provider* lv1)
{
m_pLv1 = lv1;
}
protected:
L1Provider* m_pLv1;
};
class L3Provider
{
public:
virtual void L3Service() = 0;
void SetLowerLayer(L2Provider* lv2)
{
m_pLv2 = lv2;
}
protected:
L2Provider* m_pLv2;
};
class DataLink : public L1Provider
{
public:
virtual void L1Service()
{
std::cout << "lv1 doing its job" << std::endl;
}
};
class Transport : public L2Provider
{
public:
virtual void L2Service()
{
std::cout << "lv2 start its job" << std::endl;
m_pLv1->L1Service();
std::cout << "lv2 end its job" << std::endl;
}
};
class Session : public L3Provider
{
public:
virtual void L3Service()
{
std::cout << "lv3 start its job" << std::endl;
m_pLv2->L2Service();
std::cout << "lv3 end its job" << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
// 实例化个层对象
DataLink dataLink;
Transport transport;
Session session;
// 安装好层
transport.SetLowerLayer(&dataLink);
session.SetLowerLayer(&transport);
// 工作
session.L3Service();
return 0;
}