例子:有一个老师类,有一个语文课的类
#include <iostream>
using namespace std;
class Chinese
{
public:
void Begin()
{
cout << "Let's begin Chinese lesson..." << endl;
}
};
class Teacher
{
public:
void StartLesson(Chinese* lesson)
{
lesson->Begin();
}
};
int main()
{
Chinese chinese;
Teacher teacher;
teacher.StartLesson(&chinese);
return 0;
}
将语文课的实例作为参数传给老师类的StartLesson方法 ,老师开始上语文课。
老师还可能上英语课(这个学校就一个全能老师),这个时候就需要再添加一个英语课的类。
#include <iostream>
using namespace std;
class Chinese
{
public:
void Begin()
{
cout << "Let's begin Chinese lesson..." << endl;
}
};
class English //++
{ //++
public: //++
void Begin() //++
{ //++
cout << "Let's begin English lesson..." << endl;//++
} //++
};
class Teacher
{
public:
void StartLessonChinese(Chinese* lesson) //+-
{ //+-
lesson->Begin(); //+-
} //+-
void StartLessonEnglish(English* lesson) //++
{ //++
lesson->Begin(); //++
} //++
};
int main()
{
Chinese chinese;
English english;
Teacher teacher;
teacher.StartLessonChinese(&chinese);
teacher.StartLessonEnglish(&english);
return 0;
}
显然,当课程变多后,这样不是个好办法。怎么办呢,可以增加一个抽象层,老师类只依赖课程这个抽象层,不再是具体的哪门课,这样,当课程增加时,老师类不需要修改,只需在课程抽象层的底层,也就是具体课程里面添加一个类
#include <iostream>
using namespace std;
//基类
class Lesson
{
public:
virtual void Begin() = 0;
};
//派生类
class Chinese : public Lesson
{
public:
void Begin()
{
cout << "Let's begin Chinese lesson..." << endl;
}
};
//派生类
class English : public Lesson
{
public:
void Begin()
{
cout << "Let's begin English lesson..." << endl;
}
};
class Teacher
{
public:
void StartLesson(Lesson* lesson)
{
lesson->Begin();
}
};
int main()
{
Chinese chinese;
English english;
Teacher teacher;
teacher.StartLesson(&chinese);
teacher.StartLesson(&english);
return 0;
}
这样一来,以后再增加新的课程时,只需要
class Math :public Lesson
{
public:
void Begin()
{
cout << "Let's begin Math lesson..." << endl;
}
};
然后再实际使用的时候,可以生成为DLL,将其接口放到头文件中,只暴露接口
BaseClass.h
#pragma once
#ifndef LOADMYDLL
#define MY_HEAD_API __declspec(dllexport)
#else
#define MY_HEAD_API __declspec(dllimport)
#endif
//基类
class MY_HEAD_API Lesson
{
public:
virtual void Begin() = 0;
};
//派生类
class MY_HEAD_API Chinese : public Lesson
{
public:
void Begin();
};
//派生类
class MY_HEAD_API English : public Lesson
{
public:
void Begin();
};
class MY_HEAD_API Math :public Lesson
{
public:
void Begin();
};
class MY_HEAD_API Teacher
{
public:
void StartLesson(Lesson* lesson);
};
BaseClass.cpp
#include <iostream>
using namespace std;
#include "BaseClass.h"
void Chinese::Begin()
{
cout << "Let's begin Chinese lesson..." << endl;
}
void English::Begin()
{
cout << "Let's begin English lesson..." << endl;
}
void Math::Begin()
{
cout << "Let's begin Math lesson..." << endl;
}
void Teacher::StartLesson(Lesson* lesson)
{
lesson->Begin();
}
另外一个项目中测试:将.lib文件放到项目源文件所在路径,.dll放到生成路径下
【当然也可以在测试项目中将dll和lib添加到属性中】
#include <iostream>
using namespace std;
#include "BaseClass.h"
#pragma comment(lib, "MyDll0813.lib")
int main()
{
Chinese chinese;
English english;
Math math;
Teacher teacher;
teacher.StartLesson(&chinese);
teacher.StartLesson(&english);
teacher.StartLesson(&math);
return 0;
}