C 语言-- 原型设计模式
C 语言-- 原型设计模式
小张525 发表于8个月前
C 语言-- 原型设计模式
  • 发表于 8个月前
  • 阅读 5
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

// main.h
#ifndef _MAIN_H
#define _MAIN_H

typedef struct _AdvTemplate AdvTemplate;
struct _AdvTemplate
{
	char acAdvSubject[50];
	char acAdvContext[50];

	char* (*getAdvSubject)(AdvTemplate *pAdvTemplate);
	char* (*getAdvContext)(AdvTemplate *pAdvTemplate);

	void (*DeleteAdvTemplate)(AdvTemplate *pAdvTemplate);
};


typedef struct _Mail Mail;
struct _Mail
{
	char receiver[20];
	char subject[50];
	char appellation[20];
	char context[50];
	char tail[20];

	AdvTemplate *pAdvTemplate;

	Mail*(*CloneMail)(Mail *pMail);


	char* (*getReceiver)(Mail *pMail);
	void (*setReceiver)(char *pString,Mail *pMail);
	void (*setAppellation)(char *pString,Mail *pMail);
	void (*setTail)(char *pString,Mail *pMail);

	void (*DeleteMail)(Mail *pMail);

};


#endif


// main.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"


char* getAdvSubject(AdvTemplate *pAdvTemplate)
{
	return pAdvTemplate->acAdvSubject;
}

char* getAdvContext(AdvTemplate *pAdvTemplate)
{
	return pAdvTemplate->acAdvContext;
}


void DeleteAdvTemplate(AdvTemplate *pAdvTemplate)
{
	if(pAdvTemplate != NULL)
	{
		free(pAdvTemplate);
		pAdvTemplate=NULL;
	}
}

AdvTemplate *CreateAdvTemplate(void)
{
	AdvTemplate *pAdvTemplate = (AdvTemplate*)malloc(sizeof(AdvTemplate));

	if(!pAdvTemplate)
	{
		return NULL;
	}

	memset(pAdvTemplate,0,sizeof(AdvTemplate));
	strcpy(pAdvTemplate->acAdvSubject,"This is subject");
	strcpy(pAdvTemplate->acAdvContext,"This is context");


	pAdvTemplate->getAdvSubject=getAdvSubject;
	pAdvTemplate->getAdvContext=getAdvContext;
	pAdvTemplate->DeleteAdvTemplate=DeleteAdvTemplate;

	return pAdvTemplate;
}


char* getReceiver(Mail *pMail)
{
	return pMail->receiver;
}

void setReceiver(char* pString,Mail *pMail)
{
	if(pString)
	{
		strcpy(pMail->receiver,pString);
	}
}

void setAppelltion(char* pString,Mail *pMail)
{
	if(pString)
	{
		strcpy(pMail->appellation,pString);
	}
}

void setTail(char *pString,Mail *pMail)
{
	if(pString)
	{
		strcpy(pMail->tail,pString);
	}
}

// 指向和预留空间的区别是什么.
Mail *CloneMail(Mail *pMail)
{
	Mail *pCloneMail = (Mail*)malloc(sizeof(Mail));
	if(pCloneMail)
	{
		memcpy(pCloneMail,pMail,sizeof(Mail));
	}
	return pCloneMail;
}


/**
	注意释放的过程, 先释放父类上的内容,
	在释放当前.
	需要与free操作做对比测试.
**/
void DeleteMail(Mail *pMail)
{
    
    if(pMail->pAdvTemplate)
    {
        pMail->pAdvTemplate->DeleteAdvTemplate(pMail->pAdvTemplate);
    }
    if(pMail)
    {
        free(pMail);
        pMail = NULL;
    }
}



Mail *CreateMail(void)
{
    Mail *pMail = (Mail *)malloc(sizeof(Mail));
    if(!pMail)
    {
        return NULL;
    }
    memset(pMail, 0, sizeof(Mail));
    pMail->pAdvTemplate = CreateAdvTemplate();
    if(!pMail->pAdvTemplate)
    {
        return NULL;
    }
    pMail->setAppellation = setAppelltion;
    pMail->setReceiver = setReceiver;
    pMail->setTail = setTail;
    pMail->getReceiver = getReceiver;
    pMail->DeleteMail = DeleteMail;
    pMail->CloneMail = CloneMail;
    strcpy(pMail->context,pMail->pAdvTemplate->acAdvContext);
    strcpy(pMail->subject,pMail->pAdvTemplate->acAdvSubject);
    return pMail;
}

char *GetAppell()
{
    return "abc";
}


char *GetReceiver()
{
    return "abc";
}


void sendMail(Mail *pMail,int i)
{
	printf("=======================================\r\n");
	printf("receiver:%d---%s\r\n",i,pMail->getReceiver(pMail));
	printf("subject:%s\r\n",pMail->subject);
	printf("appellation:%s\r\n",pMail->appellation);
	printf("context:%s\r\n",pMail->context);
	printf("=======================================\r\n");
}

int main()
{
	int max_count = 6; /*发送信件的数量*/
	int i=1;
    Mail *pMail = NULL;
    Mail *pCloneMail = NULL;

	pMail = CreateMail();
    pMail->setTail("xxx copyright.", pMail);

	while(max_count)
    {
        max_count--;
        
        /*克隆出一封Mail*/
        pCloneMail = pMail->CloneMail(pMail); 


        /*设置每封Mail的不同部分*/
        pCloneMail->setAppellation(GetAppell(), pCloneMail);
        pCloneMail->setReceiver(GetReceiver(), pCloneMail);


        /*发送邮件*/
		sendMail(pCloneMail,i);

		//pCloneMail->DeleteMail(pCloneMail);
		// 此处不要调用DeleteMail()操作, 主要的原因是因为父类中的内会被修改.导致克隆的数据不可用.
		// free只释放克隆时申请的空间, 不修改父类中的的信息. 是的程序服务调用父类中 的释放操作.
        free(pCloneMail);
		i++;
    }


    pMail->DeleteMail(pMail);
	return 0;
}

 

共有 人打赏支持
粉丝 6
博文 51
码字总数 30094
×
小张525
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: