MOOC清华《面向对象程序设计》第7章:负载监视器的设计v2.0(采用基于模板的策略模式)
2017-09-14 20:25
411 查看
在第6章程序的基础上,按照本章要求做了改进。
Debug经验:采用模板定义类时,含有模板形参的函数必须在类里面定义,不能放到类的外面,不能另外放到实现文件中。
上图运行结果中出现了37.21等CPU占用率较高的情况,是因为我在这一刻打开了一个网页。
Debug经验:采用模板定义类时,含有模板形参的函数必须在类里面定义,不能放到类的外面,不能另外放到实现文件中。
//main.cpp #include <iostream> #include <windows.h> #include "Monitor.h" #include "LoadStrategy.h" #include "MemoryStrategy.h" #include "LatencyStrategy.h" #include "Display.h" using namespace std; int main() { Monitor<Load, Memory, Latency, Display> monitor; while(1){ monitor.getLoad(); monitor.getTotalMemory(); monitor.getUsedMemory(); monitor.getNetworkLatency(); monitor.show(); } return 0; }
//Monitor.h #ifndef Monitor_h #define Monitor_h #include "LoadStrategy.h" #include "MemoryStrategy.h" #include "LatencyStrategy.h" #include "Display.h" template <class _load, class _memory, class _latency, class _display> class Monitor:public _load, public _memory, public _latency, public _display{ public: Monitor(){} ~Monitor(){} void getLoad(){ load = _load::getCPULoad(); } void getTotalMemory(){ totalMemory = _memory::getTotal(); } void getUsedMemory(){ usedMemory = _memory::getUsed(); } void getNetworkLatency(){ latency = _latency::getLatency(); } void show(){ _display::show(load, totalMemory, usedMemory, latency); } private: LoadStrategy *m_loadStrategy; MemoryStrategy *m_memStrategy; LatencyStrategy *m_latencyStrategy; Display *m_display; float load, latency; long totalMemory, usedMemory; }; #endif
//LoadStrategy.h #ifndef LoadStrategy_h #define LoadStrategy_h #include <iostream> #include <windows.h> using namespace std; class LoadStrategy{ public: virtual ~LoadStrategy(){} virtual float getLoad() = 0; }; class LoadStrategyImpl_1:public LoadStrategy{ public: ~LoadStrategyImpl_1(){} __int64 CompareFileTime(FILETIME time1, FILETIME time2); float getLoad(); }; class LoadStrategyImpl_2:public LoadStrategy{ public: ~LoadStrategyImpl_2(){} float getLoad(){ //get load here float load = 0.0; return load; } }; class Load{ public: float getCPULoad(){ LoadStrategyImpl_1 obj; float CPU_LOAD = obj.getLoad(); return CPU_LOAD; } }; #endif
//LoadStrategy.cpp #include <iostream> #include <windows.h> #include "LoadStrategy.h" using namespace std; __int64 LoadStrategyImpl_1::CompareFileTime(FILETIME time1, FILETIME time2) { __int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime ; __int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime ; return (b - a); } float LoadStrategyImpl_1::getLoad(){ float load = 0.0; FILETIME idleTime;//空闲时间 FILETIME kernelTime;//核心态时间 FILETIME userTime;//用户态时间 GetSystemTimes(&idleTime, &kernelTime, &userTime ); //这里务必调用GetSystemTimes函数一次 HANDLE hEvent; FILETIME pre_idleTime; FILETIME pre_kernelTime; FILETIME pre_userTime; pre_idleTime = idleTime; pre_kernelTime = kernelTime; pre_userTime = userTime; hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //初始值为nonsignaled,并且每次触发后自动设置为nonsignaled WaitForSingleObject(hEvent, 500);//等待500毫秒 GetSystemTimes(&idleTime, &kernelTime, &userTime ); //这里务必再调用GetSystemTimes函数一次 __int64 idle = CompareFileTime(pre_idleTime, idleTime); __int64 kernel = CompareFileTime(pre_kernelTime, kernelTime); __int64 user = CompareFileTime(pre_userTime, userTime); load = (kernel + user - idle) * 100.0 / (kernel + user); //(总的时间 - 空闲时间)/ 总的时间 = 占用CPU时间的比率,即占用率 pre_idleTime = idleTime; pre_kernelTime = kernelTime; pre_userTime = userTime; return load; }
//MemoryStrategy.h #ifndef MemoryStrategy_h #define MemoryStrategy_h #include <iostream> using namespace std; class MemoryStrategy{ public: virtual ~MemoryStrategy(){} virtual long getTotal() = 0; virtual long getUsed() = 0; }; class MemoryStrategyImpl_1:public MemoryStrategy{ public: ~MemoryStrategyImpl_1(){} long getTotal(); long getUsed(); }; class MemoryStrategyImpl_2:public MemoryStrategy{ public: ~MemoryStrategyImpl_2(){} long getTotal(){ //get total memory here long total = 0; return total; } long getUsed(){ //get used memory here long used = 0; return used; } }; class Memory{ public: long getTotal(){ MemoryStrategyImpl_1 obj; long TOTAL_MEMORY = obj.getTotal(); return TOTAL_MEMORY; } long getUsed(){ MemoryStrategyImpl_1 obj; long USED_MEMORY = obj.getUsed(); return USED_MEMORY; } }; #endif
//MemoryStrategy.cpp #include <iostream> #include <windows.h> #include "MemoryStrategy.h" using namespace std; long MemoryStrategyImpl_1::getTotal(){ //get total memory here long total = 0; MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex);//必须有这一句,否则函数错误! bool res = GlobalMemoryStatusEx(&statex); HANDLE hEvent; hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //初始值为nonsignaled,并且每次触发后自动设置为nonsignaled const unsigned long long DIV = 1024; WaitForSingleObject(hEvent, 500);//等待500毫秒 total = statex.ullTotalPhys / DIV / DIV ; return total; } long MemoryStrategyImpl_1::getUsed(){ //get used memory here long used = 0; MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex);//必须有这一句,否则函数错误! bool res = GlobalMemoryStatusEx(&statex); HANDLE hEvent; hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); //初始值为nonsignaled,并且每次触发后自动设置为nonsignaled const unsigned long long DIV = 1024; WaitForSingleObject(hEvent, 500);//等待500毫秒 used = statex.ullTotalPhys / DIV / DIV - statex.ullAvailPhys / DIV / DIV ; return used; }
//LatencyStrategy.h #ifndef LatencyStrategy_h #define LatencyStrategy_h #include <iostream> using namespace std; class LatencyStrategy{ public: virtual ~LatencyStrategy(){} virtual float getLatency() = 0; }; class LatencyStrategyImpl_1:public LatencyStrategy{ public: ~LatencyStrategyImpl_1(){} float getLatency(){ //get load here float latency = 0.0; return latency; } }; class LatencyStrategyImpl_2:public LatencyStrategy{ public: ~LatencyStrategyImpl_2(){} float getLatency(){ //get load here float latency = 0.0; return latency; } }; class Latency{ public: float getLatency(){ //return NETWORK_LATENCY; } }; #endif
//Display.h #ifndef Display_h #define Display_h #include <iostream> #include <iomanip> using namespace std; class Display{ public: void show(float load, long totalMemory, long usedMemory, float latency){ //cout << load << ", " // << totalMemory << ", " // << usedMemory << ", " // << latency << endl; cout << setw(8) << setiosflags(ios::fixed) << setprecision(2) << load << ", " << totalMemory << ", " << usedMemory << endl; } }; #endif
上图运行结果中出现了37.21等CPU占用率较高的情况,是因为我在这一刻打开了一个网页。
相关文章推荐
- MOOC清华《面向对象程序设计》第6章:负载监视器的设计
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v1.0(采用迭代器模式)
- MOOC清华《面向对象程序设计》第5章:OOP版填充数字矩阵(采用模板方法)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.3(采用函数对象)
- MOOC清华《面向对象程序设计》第5章:填充数字矩阵-终极中文版 v 3.0(采用模板方法,并增加集中控制功能)
- 设计模式之模板方法模式和策略模式
- Spring整合JDBC模板方法设计模式之基于继承的实现
- 设计模式二(策略模式,采用python实现)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.4(单科及格与总分及格)
- 设计模式之行为型模式(责任链、命令、解释器、迭代式、仲裁者、备忘录、观察者、状态、策略、模板方法、访问者)
- java设计模式(工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式、适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式,。 行为型模式,共十一种:策略模式、模板方法)
- 基于设计模式的学习之旅-----模板方法模式(附源码)
- Spring整合JDBC模板方法设计模式之基于组合的实现
- java与设计模式(六)-模板方法二基于项目开发
- MOOC清华《面向对象程序设计》第4章:函数模板实验
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.1(“记住”及格分数线)
- 23种设计模式4--行为型模式(策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式)
- 设计模式之模板方法和策略模式的区别(一)
- MOOC清华《面向对象程序设计》第4章:类模板的特化实验
- 一道题采用两种设计模式:对比策略模式和模版方法