Welcome 微信登录

首页 / 操作系统 / Linux / 基于工厂模式的Windows和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(" ");
}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---- ");
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(" ");
}void writefile(char* path,char buffer[])
{
printf("writing start---- ");
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(" ");
}/*    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;
} 解决Linux下Weblogic9.2 web应用乱码问题笔记虚拟机Linux上网的方法相关资讯      Linux教程 
  • Linux教程:如何在命令行中查看目  (07/28/2014 12:22:23)
  • Linux 修改root密码  (11/03/2012 07:53:38)
  • su - root 与su root的区别  (06/06/2012 00:39:40)
  • Linux进程间通信:消息队列  (01/28/2013 09:43:00)
  • U盘安装Linux开机无法启动解决方法  (10/07/2012 08:55:52)
  • Windows 7/Linux 同步时间  (05/15/2012 06:17:55)
本文评论 查看全部评论 (3)
表情: 姓名: 字数


评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 3 楼 匿名 发表于 2013/10/18 9:23:59你这个工厂模式和封装linux windows平台下的文件操作没有直接关系回复