博客
关于我
设计模式(二)—工厂模式(简单工厂模式)(附代码)
阅读量:276 次
发布时间:2019-03-01

本文共 2850 字,大约阅读时间需要 9 分钟。

一、什么是工厂模式?

最常用的设计模式之一

这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

在工厂模式中,我们在创建对象不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

二、工程模式简图

在这里插入图片描述

三、实例:

我们将实现输入对象名字,程序将根据对象姓名进行查找,查找完成在屏幕打印对象执行的动作,同时打印对象的姓名和年龄,若查找失败,则打印“no find name”

首先建立个5个.c文件和1个.h文件。如下:

1、Animal.h

该.h文件主要用于 类声明 以及几个 函数声明,同时可以将几个.C文件常用的.h文件包含于此。
#include
#include
/*“动物”类声明*/struct Animal{ /*成员属性*/ char name[128]; int age; int sex; int others; /*成员方法*/ void (*eat)(); void (*beat)(); void(*character)(); void (*test)(); struct Animal *next;};/*头插法建立链表函数声明*/struct Animal* putHeadDogLink(struct Animal* head);struct Animal* putHeadCatLink(struct Animal* head);struct Animal* putHeadHumanLink(struct Animal* head);/*根据名字检索对象函数声明*/struct Animal * findObjectName(char *name,struct Animal *head);/*“吃”函数声明*/void catEat();void dogEat();void humanEat();

2、Dog.c

#include"Animal.h"/*“狗”对象赋值*/struct Animal dog={   	.name="lala",	.age=5,	.eat=dogEat};/*狗的eat函数*/void dogEat(){   	printf("eat baba\n");	}/*头插法建立链表函数,若链表原来为空链表,则将dog作为链首*/struct Animal* putHeadDogLink(struct Animal* head){   	if(head==NULL){   		head=&dog;		return head;	}else{   		dog.next=head;		head=&dog;		return head;	}		}

3、Cat.c

#include"Animal.h"/*“猫”对象赋值*/struct Animal cat={   		.name="hihi",		.age=2,		.eat=catEat};/*猫的eat函数*/void catEat(){   	printf("eat milk\n");}/*头插法建立链表函数,若链表原来为空链表,则将cat作为链首*/struct Animal* putHeadCatLink(struct Animal* head){   	if(head==NULL){   		head=&cat;		return head;	}else{   		cat.next=head;		head=&cat;		return head;	}		}

4、Human.c

#include"Animal.h"/*“人”对象赋值*/struct Animal human={   	.name="bobo",	.age=18,	.eat=humanEat};/*人的eat函数*/void humanEat(){   	printf("eat rice\n");}/*头插法建立链表函数,若链表原来为空链表,则将human作为链首*/struct Animal* putHeadHumanLink(struct Animal* head){   	if(head==NULL){   		head=&human;		return head;	}else{   		human.next=head;		head=&human;		return head;	}		}

5、Function.c

#include"Animal.h"/*根据名字检索对象函数,查找成功返回值为查到对象的地址,查找失败,返回NULL*/struct Animal *	findObjectName(char *name,struct Animal *head){   		struct Animal *tmp=head;		/*判断链表是否为空链表*/		if(head==NULL){   			printf("The list is empty\n");			return NULL;		}else{   			while(tmp!=NULL){   						if(strcmp(tmp->name,name)==0){   					return tmp;				}				tmp=tmp->next;			}			printf("no find name\n");			return NULL;		}}

6、Main.c

#include "Animal.h"int main(){   	char buf[128]={   '\0'};	struct Animal* head=NULL;	struct Animal* tmp=NULL;	/*头插法建立链表,将dog、cat、human三个对象组成链表*/	head=putHeadDogLink(head);	head=putHeadCatLink(head);	head=putHeadHumanLink(head);	while(1){   	/*清空buf*/		memset(buf,'\0',sizeof(buf));		printf("input name:\n");		scanf("%s",buf);		/*根据名字检索对象函数,查找成功返回值为查到对象的地址,查找失败,返回NULL*/		tmp=findObjectName(buf,head);		/*查找成功,将对象的姓名、年龄输出,同时执行对象的eat函数*/		if(tmp!=NULL){   			tmp->eat();			printf("%s age:%d\n",tmp->name,tmp->age);		}	}	return 0;}
注:在Liunx环境下进行编译时,首先将这几个文件存放在同一个文件夹,然后需使用gcc *.c

实验结果:

在这里插入图片描述

转载地址:http://qtxo.baihongyu.com/

你可能感兴趣的文章
Mysql学习总结(73)——MySQL 查询A表存在B表不存在的数据SQL总结
查看>>
Mysql学习总结(76)——MySQL执行计划(explain)结果含义总结
查看>>
Mysql学习总结(77)——温故Mysql数据库开发核心原则与规范
查看>>
Mysql学习总结(78)——MySQL各版本差异整理
查看>>
Mysql学习总结(79)——MySQL常用函数总结
查看>>
Mysql学习总结(7)——MySql索引原理与使用大全
查看>>
Mysql学习总结(80)——统计数据库的总记录数和库中各个表的数据量
查看>>
Mysql学习总结(81)——为什么MySQL不推荐使用uuid或者雪花id作为主键?
查看>>
Mysql学习总结(82)——MySQL逻辑删除与数据库唯一性约束如何解决?
查看>>
Mysql学习总结(83)——常用的几种分布式锁:ZK分布式锁、Redis分布式锁、数据库分布式锁、基于JDK的分布式锁方案对比总结
查看>>
Mysql学习总结(84)—— Mysql的主从复制延迟问题总结
查看>>
Mysql学习总结(85)——开发人员最应该明白的数据库设计原则
查看>>
Mysql学习总结(8)——MySql基本查询、连接查询、子查询、正则表达查询讲解
查看>>
Mysql学习总结(9)——MySql视图原理讲解与使用大全
查看>>
MySQL学习笔记十七:复制特性
查看>>
Mysql学习第一课-mysql的定义及sql语句
查看>>
mysql安全模式: sql_safe_updates
查看>>
mysql安装,卸载,连接
查看>>
MySQL安装之没有配置向导
查看>>
mysql安装出现 conflicts with mysql*的解决办法
查看>>