红联Linux门户
Linux帮助

基于工厂模式的Windows和Linux文件操作的封装

发布时间:2015-03-18 09:54:03来源:linux网站作者:赵丽

工作好几天终于把这个问题搞定了。前些天linux老师给我们布置了一个作业,对window和linux的文件操作进行封装,并且将这两个封装写成一个基类的派生类,通过基类调用派生类访问。以前都是用直接访问派生类的,现在用基类访问派生类不知道怎么搞了,查了一下工厂模式可以帮我完成。


代码如下:在Windows和Linux 下都能运行。


/*
赵丽 电子科大
实现基于工厂模式的windows和linux文件操作的封装
*/
//head.h

#include <map>
#include <string>
#include <stdio.h>
class file;
typedef file* (*FactoryFunction)();
#ifdef WIN32
#define TEST 1
#else
#define TEST 0
#endif
#define NAME "test"
class DeviceFactory
{
public:
static void Register(std::string name, FactoryFunction instanceFunction)
{
m_FactoryFunctions[name] = instanceFunction;
}
static file* GetInstance(std::string name)
{
if (m_FactoryFunctions.count(name))
{
return m_FactoryFunctions[name]();
}
else
{
return NULL;
}
}
private:
static std::map<std::string, FactoryFunction> m_FactoryFunctions;
};
std::map<std::string, FactoryFunction> DeviceFactory::m_FactoryFunctions;

class file
{
public:
virtual void open() = 0;
virtual void createfile(char* path)=0;
virtual void writefile(char* path,char inText[])=0;
virtual void readfile(char* path)=0;
//virtual void closefile()=0;
};

#if TEST==1
#include <windows.h>
#include <stdio.h>
DWORD  szBuffer[4];
const char inText[] = "QQ:610847323";
char  Length;
class file_windows : public file
{
public:
void open()
{
std::cout <<"windows class"<<std::endl;
}

void createfile(char* path)
{  
HANDLE  hFile;  
hFile=CreateFile(path,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
}

void writefile(char* path,char inText[])
{
HANDLE hFile;
DWORD nBytes;
hFile=CreateFile(path,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,0,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
WriteFile(hFile,inText, lstrlen(inText),&nBytes,NULL);
CloseHandle(hFile);
}
}

void readfile(char* path)
{
HANDLE hFile;
DWORD  dwBytesRead;
char buffer[4096];
hFile = CreateFile("hello.txt", GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);                           
ReadFile(hFile, buffer, 4096, &dwBytesRead, NULL);
CloseHandle(hFile);    
for(int j=0;j<5;j++)
{
printf("%c",buffer[j]);
}
printf("\n");
}

static file* CreateInstance()
{
static file_windows* windows0=new file_windows;
return windows0;
}
};
#else
class file_linux : public file
{
public:
void open()
{
std::cout<<"linux class"<<std::endl;
}

void createfile(char* path)
{  
fp=fopen(path,"w");
}

void readfile(char* path)
{
printf("reading start----\n");
FILE * stream;
char buffer[10];
int i;
stream = fopen(path,"r");
fread(buffer,sizeof(char),5,stream);      
fclose(stream);
for(i=0;i<5;i++)
{
printf("%c",buffer[i]);
}
printf("\n");
}

void writefile(char* path,char buffer[])
{
printf("writing start----\n");
FILE * stream;
int i;
stream = fopen(path,"w");
fwrite(buffer,sizeof(char),5,stream);
fclose(stream);
for(i=0;i<5;i++)
{
printf("%c",buffer[i]);
}
printf("\n");
}

/*    void open(const char* path,const char* mode)
{
FILE * fp;
fp=fopen(path,mode);
if(fp==NULL)
{
return;
}
fclose(fp);
}
*/

static file* CreateInstance()
{
static file_linux* linux0=new file_linux;
return linux0;
}
};
#endif

#if TEST==1
FactoryFunction fun=&file_windows::CreateInstance;
#else
FactoryFunction fun=&file_linux::CreateInstance;
#endif

//main.cpp
#include <iostream>
#include "head.h"
using namespace std;
int main()
{
DeviceFactory::Register(NAME,fun);
file* file = DeviceFactory::GetInstance(NAME);
file->open();
file->createfile("hello0.txt");
file->writefile("hello.txt","hello");
file->readfile("hello.txt");
delete file;
return 0;
}