歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 基於工廠模式的Windows和Linux文件操作的封裝

基於工廠模式的Windows和Linux文件操作的封裝

日期:2017/2/28 16:30:21   编辑: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;
}

Copyright © Linux教程網 All Rights Reserved