最近在做课程设计——学生成绩管理系统
角色有:老师,学生,课程
老师,学生,课程都继承同一个接口 Entity
我采用工厂模式,一个接口工厂,三种不同的工厂,每种工厂分别产老师,学生和课程
通过工厂把产生的不同种类的 Entity 收集起来,由接口的工厂统一释放内存。
以下是程序分块:
Entity 实体定义:
class Entity { protected: int NO ; public: string name ; explicit Entity ( const int _NO , const string& _name ) : NO ( _NO ) , name ( std::move ( _name ) ) {} virtual ~Entity () = default ; virtual void display () const = 0 ; } ;
老师定义:
class Teacher : public Entity { private: double salary ; public: explicit Teacher ( const int _NO , const string& _name ) : Entity ( _NO , _name ) , salary ( 10000 ) {} void display () const override { cout << endl << "工号 : " << NO << "\t" << name ; cout << "\t\t薪水 : " << salary << endl ; } } ;
学生定义:
class Student : public Entity { public: explicit Student ( const int _NO , const string& _name ) : Entity ( _NO , _name ) {} void display () const override { cout << endl << "学号 : " << NO << "\t" << name << endl ; } } ;
课程定义:
class Course : public Entity { public: explicit Course ( const int NO , const string& name ) : Entity ( NO , name ) {} void display () const override { cout << endl << "编号 : " << NO << "\t" << name << endl ; } } ;
接口工厂定义:
class Factory { protected: // 一个静态的垃圾收集器, 收集由该工厂的子类产生的 Entity* 指针 static std::vector< Entity* > Litter ; private: // 这个函数用 atexit 登记, main 函数结束之后启动 // 设置为私有, 只能接口释放 static void End_OK () { for ( auto &it : Litter ) if ( it != nullptr ) delete it ; Litter.shrink_to_fit () ; // 清空容器 } public: Factory () { // 设置一个 once_flag, 只登记一次 static std::once_flag flag ; std::call_once ( flag , [&] () { atexit ( End_OK ) ; } ) ; } virtual ~Factory () = default ; virtual Entity* Make_Entity ( const int NO , const string& name ) = 0 ; } ; // 静态变量的类外声明 std::vector< Entity* > Factory::Litter ;
“老师”工厂定义:
class Teacher_Factory : public Factory { public: Entity* Make_Entity ( const int NO , const string& name ) override { Entity* One = new Teacher ( NO , name ) ; Litter.emplace_back ( One ) ; // 垃圾收集器收集 return One ; } } ;
“学生”工厂定义:
class Student_Factory : public Factory { public: Entity* Make_Entity ( const int NO , const string& name ) override { Entity* One = new Student ( NO , name ) ; Litter.emplace_back ( One ) ; // 垃圾收集器收集 return One ; } } ;
“课程”工厂定义:
class Course_Factory : public Factory { public: Entity* Make_Entity ( const int NO , const string& name ) override { Entity* One = new Course ( NO , name ) ; Litter.emplace_back ( One ) ; // 垃圾收集器收集 return One ; } } ;
测试代码:
#include <bits/stdc++.h> #define rep( i , j , n ) for ( int i = int(j) ; i < int(n) ; ++i ) #define dew( i , j , n ) for ( int i = int(n-1) ; i > int(j) ; --i ) #define _PATH __FILE__ , __LINE__ typedef std::pair < int , int > P ; using std::cin ; using std::cout ; using std::endl ; using std::string ; // 声明一个 enum class enum class Product { Student , Teacher , Course } ; enum class Institution { Computer , Economic , Mathmathics } ; class Entity { protected: int NO ; public: string name ; explicit Entity ( const int _NO , const string& _name ) : NO ( _NO ) , name ( std::move ( _name ) ) {} virtual ~Entity () = default ; virtual void display () const = 0 ; } ; class Student : public Entity { public: explicit Student ( const int _NO , const string& _name ) : Entity ( _NO , _name ) {} void display () const override { cout << endl << "学号 : " << NO << "\t" << name << endl ; } } ; class Teacher : public Entity { private: double salary ; public: explicit Teacher ( const int _NO , const string& _name ) : Entity ( _NO , _name ) , salary ( 10000 ) {} void display () const override { cout << endl << "工号 : " << NO << "\t" << name ; cout << "\t\t薪水 : " << salary << endl ; } } ; class Course : public Entity { public: explicit Course ( const int NO , const string& name ) : Entity ( NO , name ) {} void display () const override { cout << endl << "编号 : " << NO << "\t" << name << endl ; } } ; class Factory { protected: // 一个静态的垃圾收集器, 收集由该工厂的子类产生的 Entity* 指针 static std::vector< Entity* > Litter ; private: // 这个函数用 atexit 登记, main 函数结束之后启动 // 设置为私有, 只能接口释放 static void End_OK () { for ( auto &it : Litter ) if ( it != nullptr ) delete it ; Litter.shrink_to_fit () ; // 清空容器 } public: Factory () { // 设置一个 once_flag, 只登记一次 static std::once_flag flag ; std::call_once ( flag , [&] () { atexit ( End_OK ) ; } ) ; } virtual ~Factory () = default ; virtual Entity* Make_Entity ( const int NO , const string& name ) = 0 ; } ; // 静态变量的类外声明 std::vector< Entity* > Factory::Litter ; class Student_Factory : public Factory { public: Entity* Make_Entity ( const int NO , const string& name ) override { Entity* One = new Student ( NO , name ) ; Litter.emplace_back ( One ) ; // 垃圾收集器收集 return One ; } } ; class Teacher_Factory : public Factory { public: Entity* Make_Entity ( const int NO , const string& name ) override { Entity* One = new Teacher ( NO , name ) ; Litter.emplace_back ( One ) ; // 垃圾收集器收集 return One ; } } ; class Course_Factory : public Factory { public: Entity* Make_Entity ( const int NO , const string& name ) override { Entity* One = new Course ( NO , name ) ; Litter.emplace_back ( One ) ; // 垃圾收集器收集 return One ; } } ; int main () { Factory* A = new Student_Factory () ; Entity* YHL = A->Make_Entity ( 212 , "YHL" ) ; YHL->display () ; Entity* Fluence = A->Make_Entity ( 1229 , "Fluence" ) ; Fluence->display () ; YHL = Fluence = nullptr ; delete A ; Factory* B = new Teacher_Factory () ; YHL = B->Make_Entity ( 1998 , "YHL" ) ; YHL->display () ; Fluence = B->Make_Entity ( 1998 , "Fluence" ) ; Fluence->display () ; YHL = Fluence = nullptr ; // 不用担心内存泄漏 delete B ; Factory* C = new Course_Factory () ; YHL = C->Make_Entity ( 1000001 , "书法" ) ; YHL->display () ; Fluence = C->Make_Entity ( 1000002 , "散文" ) ; Fluence->display () ; YHL = Fluence = nullptr ; delete C ; A = B = C = nullptr ; return 0 ; }