C++抽象工厂模式案例

前言:复习设计模式时想找设计模式的案例练练手,百度上没找到,自己想了一个案例来实现。
说明:本文章主要是使用抽象工厂模式,结合文件操作来实现的小例子
1、本篇中linux平台的代码有bug,读者自己debug(但在VS中可以运行)
2、开发环境VS2013

============================================================
本篇涉及到的知识点:
1、抽象工厂模式(这个读者自己百度,有了基础再看本篇)
2、文件操作

  • C语言文件操作
    • FILE* fopen(const char* path,const char* mode);
    • int fscanf(FILE* fp,“格式说明符%s”,char* str);//从文件流fp中读取字符串放入str
    • int fprintf(FILE* fp,"%s",char * str);//str输出到文件流fp中
    • char* fgets(char* str,int num,FILE* fp);//从文件流中获取num个字符放入str,成功返回str,失败返回NULL
      • fgets()默认一次读取一行
    • int fputs(const char* str,FILE* fp);//把str写入文件流
  • C++文件操作
    • 这一块读者自己百度,有机会会在出一个文件操作的结合案例
  • linux下文件的操作
    • int open(const char* path,int flag,mode_t mode);
    • ssize_t read(int fd,void* buf,size_t count);//从fd中读取count个字符放入buf中
    • ssize_t write(int fd,void* buf,size_t count);//将buf中数据写入fd

3、纯虚函数
4、父类指针指向子类对象

===================================================
head.h

#ifndef _HEAD_H_
#define _HEAD_H_
#define  SIZE 1024

#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
using namespace  std;

//#include <unistd.h>
//抽象产品类
class FileOperator{
public:
	virtual void readFile() = 0;
	virtual void writeFile() = 0;

};
//具体产品类--C语言操作文件
class CFile :public FileOperator{
	virtual void readFile(){
		FILE* fp = fopen("temp.txt", "r+");//允许读写文件
		if (fp == NULL)//文件打开失败
			return;
		
		char str[SIZE];
		memset(str, 0, sizeof(str));//全部初始化为‘0’
		fscanf(fp,"%s",str);	//从temp.txt文件中读取数据,放入str
		fprintf(stdout, "%s", str); //将从文件中获取的字符串输出到屏幕上
		fclose(fp);
	}
	virtual void writeFile(){
		FILE* fp = fopen("temp.txt", "w+");//允许读写文件
		if (fp == NULL)//文件打开失败
			return;
		char str[SIZE];
		printf("请输入要写入文件的内容:");
		if (NULL == fgets(str, SIZE, stdin)){		//从控制台获取输入
			return;									//读取错误返回NULL
		}
		fprintf(fp, "%s", str);
		/*if (EOF == fputs(str, (FILE*)fd)){						//写入temp.txt文件
			printf("写入文件出错\n");
			return;
		}*/
		fclose(fp);
		printf("已经成功写入文件\n");

	}
};
//具体产品类--C++语言操作文件
class CppFile:public FileOperator{
public:
	
	virtual void readFile(){
		ifstream ifile;
		ifile.open("tmp.txt", ios::in);
		if (!ifile.is_open()){
			cout << "读-文件打开失败" << endl;
			return;
		}
		cout << "读-文件已打开" << endl;
		char str[SIZE];
		while (!ifile.eof()){
			ifile.getline(str, 100);//第一个参数为char*类型
			cout << str;
		}
		ifile.close();
		cout << "读-文件已关闭" << endl;
	}
	virtual void writeFile(){
		ofstream ofile;
		ofile.open("tmp.txt", ios::out|ios::trunc);//写方式打开文件,如果文件存在则删除重建
		if (!ofile.is_open()){
			cout << "写-文件打开失败" << endl;
			return;
		}
		cout << "写-文件已打开" << endl;

		ofile << "你好,现在您用的是Cpp对文件的操作方式" <<"  ";
		ofile << "ok,cpp写文件已经完成" <<"   ";
		char* str = "其实我想换个行";
		ofile.write(str, strlen(str));
		ofile.close();
		cout << "写-文件已关闭" << endl;
	}
};
//具体产品类--linux下C语言操作文件(linux下调试遇到bug,读者自己debug)
class LinuxFile:public FileOperator{
public:

#include <stdio.h>

	virtual void readFile(){
#ifdef __linux__		//如果是linux平台
#include<sys/stat.h>
#include <unistd.h>
#include<sys/types.h>
#include <fcntl.h>

		int fd=open("/home/yhz/Desktop/temp.txt",O_RDONLY);
		if(fd==-1){
			printf("文件打开失败");
			return;
		}
		char str[SIZE];
		int ret=read(fd,str,sizeof(str));
		if(ret==-1)
			printf("读-文件读取失败");
		else{
			printf("文件中的内容长度为:%d byte!\n",ret);
			printf("文件内容:%s\n",str);
		}
		close(fd);
		printf("读-文件已经关闭");
#endif
	}
	virtual void writeFile(){
#ifdef __linux__	//如果是linux平台
#include <unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include <fcntl.h>

		int fd=open("/home/yhz/Desktop/temp.txt",O_WRONLY|O_CREAT,0665);
		if(fd==-1){
			printf("写-文件打开失败");
			return;
		}
		char str[1024]={"这是一个在windows平台下写的程序,期待linux下运行成功!"};
		int ret = write(fd, str, strlen(str));
		if (ret == -1)
			printf("文件读取失败");
		else{
			printf("文件中的内容长度为:%d byte!\n", ret);
			printf("文件内容:%s\n", str);
		}
		close(fd);
		printf("写-文件已经关闭\n");
#endif
	}
};
#endif

main.cpp

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "head.h"
using namespace std;
//抽象工厂类
class FileFactory{
public:
	virtual  FileOperator* CreateFileObject() = 0;//纯虚函数
};
//C语言对象类
class CFactory:public FileFactory{
public:
	virtual FileOperator* CreateFileObject(){
		FileOperator* cfo = new  CFile();
		return cfo;
	}
};
//C++对象类
class CppFactory :public FileFactory{
public:
	virtual FileOperator* CreateFileObject(){
		FileOperator* cppFo = new CppFile();
		return cppFo;
	}
};
//linux下对象类
class LinuxFactory :public FileFactory{
public:
	virtual FileOperator* CreateFileObject(){
#ifdef __linux__
		FileOperator* linux = new  LinuxFile();
		return linux;
#else
		return NULL;
#endif
	}
};
int main(){
	//父类指针指向子类对象
	FileFactory* linux = new LinuxFactory();
	//使用具体工程创建具体产品
	FileOperator* linuxObj = linux->CreateFileObject();
	if (linuxObj == NULL){
		printf("当前不是linux平台,无法实例出对象!");
		getchar();
		return -1;
	}
	linuxObj->writeFile();
	linuxObj->readFile();
	if (linuxObj){
		delete linuxObj;
		linuxObj = NULL;
	}
	if (linux){
		delete linux;
		linux = NULL; 
	}
	getchar();
	return 0;
}
//以下为测试代码
/*
void main51(){
	FileFactory* cpp = new CppFactory();
	FileOperator* cppObj = cpp->CreateFileObject();
	cppObj->writeFile();
	cppObj->readFile();

	system("pause");
}
void main4(){
	FILE* fp = fopen("D:\\Code\\project\\6_win_linux\\temp.txt", "w+");
	void* fd = (void*)fp;
	FILE* to = (FILE*)fd;
	printf("%d\n", fp);
	printf("%d\n", fd);
	char* str = "我爱你,yhz";
	//fprintf((FILE*)fd, "%s", str);
	fprintf(stdout, "%s", str);
	fprintf((FILE*)fd, "%s", str);
	fflush((FILE*)fd);
	fflush((FILE*)fd);
	char str1[1024];
	memset(str1, 0, sizeof(str1));
	fscanf(to, "%s", str1);
	puts(str1);

	//fclose(fp);

	system("pause");
}

void main5(){
	FileFactory* c = new CFactory();
	FileOperator* cObj = c->CreateFileObject();
	cObj->writeFile();
	cObj->readFile();
	if (cObj){
		delete cObj;
		cObj = NULL;
	}
	if (c){
		delete c;
		c = NULL;
	}
	system("pause");
	system("pause");
}
int main1(){
#ifdef _WIN32
	cout << "wo" << endl;
#endif
	char *str = "qoaini";
	fprintf(stdout, "%s", str);
	system("pause");
	return 0;
}
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值