MOOC清华《面向对象程序设计》第7章:负载监视器的设计v2.0(采用基于模板的策略模式)

在第6章程序的基础上,按照本章要求做了改进。

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占用率较高的情况,是因为我在这一刻打开了一个网页。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值