#include<iostream>#include<memory>usingnamespace std;// 将抽象部分(业务功能)与其实现部分(具体的平台实现)分离,使它们都可以独立地变化和扩展// 4种角色// Abstraction(抽象部分相关接口),定义抽象类接口,包含指向Implementor类型对象的指针// RefinedAbstraction(扩充抽象部分接口),实现Abstraction接口,并调用Implementor方法// Implementor(实现部分相关接口),定义实现类接口// ConcreteImplementor(实现部分具体类),实现Implementor接口namespace ns1
{classImage// 图像显示相关类{virtualchar*parsefile(constchar*pfilename,int&iLen)=0;// 文件名决定文件格式,不同读取和处理代码, 返回二进制数据格式public:virtual~Image(){}voiddraw(constchar*pfilename){int iLen =0;char*pData =parsefile(pfilename, iLen);if(iLen >0){
cout <<"Displays the image data in the buffer pointed by pData"<< endl;delete pData;// 模拟代码中因为pData的内存是new出来的,这里需要释放该内存}}};classImage_png:publicImage// 处理png格式图像文件的显示{char*parsefile(constchar*pfilename,int&iLen)override// 读取png文件内容并进行解析{// 以下是模拟代码:模拟从图像文件中读取到了数据,最终转换成了100个字节的数据格式(事先约定好的格式规范)并返回
cout <<"Start analyzing the data in the png file and put the analysis results in pData";
iLen =100;char*presult =newchar[iLen];return presult;}};classImage_jpg:publicImage// 处理jpg格式图像文件的显示{private:char*parsefile(constchar*pfilename,int&iLen)override{
cout <<"Start analyzing the data in the jpg file and put the analysis results in pData";
iLen =0;returnNULL;}};classImage_bmp:publicImage// 处理bmp格式图像文件的显示{private:char*parsefile(constchar*pfilename,int&iLen)override{
cout <<"Start analyzing the data in the bmp file and put the analysis results in pData";
iLen =0;returnNULL;}};}namespace ns2
{classImageOS// 操作系统类型相关类{public:virtual~ImageOS(){}virtualvoiddraw(char*pData,int iLen)=0;};classImageOS_Windows:publicImageOS// Windows操作系统{public:voiddraw(char*pData,int iLen)override{
cout <<"Display the image data in the buffer pointed by pData under Windows operating system"<< endl;}};classImageOS_Linux:publicImageOS// Linux操作系统{public:voiddraw(char*pData,int iLen)override{
cout <<"Display the image data in the buffer pointed by pData under Linux operating system"<< endl;}};classImageOS_Mac:publicImageOS// Mac操作系统{public:voiddraw(char*pData,int iLen)override{
cout <<"Display the image data in the buffer pointed by pData under Mac operating system"<< endl;}};classImageFormat// 图像文件格式相关类{protected:
shared_ptr<ImageOS> m_pImgOS;// 委托public:virtual~ImageFormat(){}ImageFormat(const shared_ptr<ImageOS>&pimgos):m_pImgOS(pimgos){}virtualvoidparsefile(constchar*pfilename)=0;// 文件名, 文件格式,不同读取和处理代码};classImage_png:publicImageFormat// png格式的图像文件{public:Image_png(const shared_ptr<ImageOS>&pimgos):ImageFormat(pimgos){}voidparsefile(constchar*pfilename)override{
cout <<"Start analyzing the data in the png file and put the analysis results in pData, ";// 以下是模拟代码:模拟从图像文件中读取到了数据,最终转换成了100个字节的数据格式(事先约定好的格式规范)并返回int iLen =100;char*presult =newchar[iLen];
m_pImgOS->draw(presult, iLen);delete presult;}};classImage_jpg:publicImageFormat// jpg格式的图像文件{public:Image_jpg(const shared_ptr<ImageOS>&pimgos):ImageFormat(pimgos){}voidparsefile(constchar*pfilename)override{
cout <<"Start analyzing the data in the jpg file and put the analysis results in pData, ";}};classImage_bmp:publicImageFormat// bmp格式的图像文件{public:Image_bmp(const shared_ptr<ImageOS>&pimgos):ImageFormat(pimgos){}voidparsefile(constchar*pfilename)override{
cout <<"Start analyzing the data in the bmp file and put the analysis results in pData, ";}};}intmain(){#if0usingnamespace ns1;
shared_ptr<Image>pImg(newImage_png());
pImg->draw("c:\\somedir\\filename.jpg");#endif#if1usingnamespace ns2;
shared_ptr<ImageOS>pimgos_windows(newImageOS_Windows());// 针对Windows操作系统
shared_ptr<ImageFormat>pimg_png(newImage_png(pimgos_windows));// 运行时把图像文件格式(png)和操作系统(Windows) 动态组合到一起
pimg_png->parsefile("c:\\somedir\\filename.png");#endif
cout <<"Over!\n";return0;}