天天看点

简单工厂模式的实现

软件设计模式,又称设计模式,是一套被反复使用,多数人知晓的,经过分类编目的。代码设计经验的总结。使用设计模式是为了可重用代码,让代码更容易被他人理解,保证代码可靠性,程序的重用性。设计模式一共有23种。算法不是设计模式,因为算法致力于解决问题而非设计问题。

设计模式通常描述了一组相互紧密作用的类与对象,c面向过程,是一门不太友好的面向对象的语言,java面向对象。

什么是类与对象?

类是一种用户定义的引用数据类型,也称类类型。类是一种抽象的概念,每个类包含数据说明和一组操作数据或传递消息的函数,对象是类的一种具体的实列。

例如:

类:struct  Animal {

                   char   name [128];

                    int age;

                    int sex;

                   void  (*peat)();

                  void  (*pbeat)();

}

对象:

            struct  Animal  dog;

            struct  Animal   cat;

            struct  Anima    person;

什么是工厂模式?

工厂模式(Factory  Pattern)是最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

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

例如:

Animal.h文件如下:

1 #include <stdio.h>
  2 
  3 struct Animal {
  4 
  5             char name [128];
  6             int age;
  7             int sex;
  8            void (*pbeat)();
  9            void (*peat)();
 10 
 11            struct Animal *next;
 12 
 13 };
 14 
 15 
 16 struct Animal *putdogintolink(struct Animal *phead);
 17 struct Animal *putcatintolink(struct Animal *phead);
 18 struct Animal *putpersonintolink(struct Animal *phead);
           

cat.c文件如下:

1 #include "Animal.h"
  2 
  3 
  4 void catBeat()
  5 {
  6       printf("猫抓挠\n");
  7 
  8 }
  9 void catEat()
 10 {
 11       printf("猫吃鱼\n");
 12 }
 13 
 14 
 15 
 16 
 17 /*对象  “猫”  */
 18 struct Animal cat = {
 19 
 20          .name = "Tom",
 21          .age  = 10,
 22          .pbeat = catBeat,
 23          .peat  = catEat
 24 
 25 
 26 };
 27 
 28 /*插入链表函数*/
 29 struct Animal *putcatintolink(struct Animal *phead)
 30 {
 31         if(phead == NULL)
 32         {
 33                 return  &cat;
 34         }
 35         else
 36         {
 37                cat.next = phead;
 38                phead = &cat;
 39 
 40                 return phead;
 41         }
 42 
 43 }
           

dog.c文件如下:

1 #include "Animal.h"
  2 
  3 
  4 
  5 void dogBeat()
  6 {
  7     printf("狗撕咬\n");
  8 }
  9 
 10 
 11 void dogEat()
 12 {
 13     printf("狗吃剩饭\n");
 14 }
 15 
 16 
 17 
 18 /*对象 “狗” */
 19 struct Animal dog ={
 20 
 21           .name = "xiaohui",
 22           .age  = 5,
 23           .pbeat =dogBeat,
 24           .peat  = dogEat,
 25 
 26 
 27 
 28 };
 29 
 30 /*插入链表函数*/
 31 struct Animal *putdogintolink(struct Animal *phead)
 32 {
 33         if(phead == NULL)
 34           {
 35                return &dog;
 36           }
 37         else
 38           {
 39               dog.next = phead;
 40               phead = &dog;
 41 
 42               return  phead;
 43           }
 44 }
           

person.c文件如下:

1 #include "Animal.h"
  2 
  3 
  4 void personBeat()
  5 {
  6     printf("人用武器\n");
  7 }
  8 
  9 void personEat()
 10 {
 11     printf("人吃饭\n");
 12 }
 13 
 14 
 15 
 16 
 17 /* 对象  “人” */
 18 struct Animal person ={
 19 
 20          .name = "xiaoming",
 21          .age  = 25,
 22          .pbeat = personBeat,
 23          .peat  = personEat
 24 
 25 };
 26 
 27 /*插入链表函数*/
 28 struct Animal *putpersonintolink(struct Animal *phead)
 29 {
 30         if(phead == NULL)
 31         {
 32                 return &person;
 33         }
 34         else
 35         {
 36                person.next = phead;
 37                phead = &person;
 38 
 39 
 40                 return phead;
 41         }
 42 
 43 }
           

main.c文件如下:

1 #include "Animal.h"
  2 #include <string.h>
  3 
  4 
  5 /*获取目标对象函数*/
  6 struct Animal *getDes(char *name,struct Animal *phead)
  7 {
  8         struct Animal *temp = phead;
  9 
 10         if(phead == NULL){
 11 
 12                 return NULL;
 13         }
 14         else
 15         {
 16               /*遍历链表*/
 17               while(temp != NULL){
 18 
 19                       if(strcmp(name,temp->name) == 0)
 20                       {
 21                             return temp;
 22                       }
 23                       temp = temp->next;
 24 
 25                }
 26 
 27               return NULL;
 28         }
 29 
 30 }
 31 
 32 int main ()
 33 {
 34     char buf[128]={'\0'};
 35     struct Animal *phead = NULL;
 36     struct Animal *des = NULL;
 37 
 38     /*将对象做成一个链表*/
 39     phead = putdogintolink(phead);
 40     phead = putcatintolink(phead);
 41     phead = putpersonintolink(phead);
 42 
 43     while(1){
 44 
 45 
 46     printf("请输入对象的名字\n");
 47     scanf("%s",buf);
 48 
 49 
 50 
 51     des = getDes(buf,phead);//获取目标对象
 52 
 53     if(des == NULL)
 54     {
 55             printf("目标对象不存在\n");
 56     }
 57     else
 58     {
 59 
 60             des->pbeat();
 61             des->peat();
 62     }
 63 
 64       memset(buf,'\0',sizeof(buf));/*清空buf*/
 65 
 66     }
 67 
 68 
 69 
 70 
 71 
 72 }
           

每添加一个对象就增加一个文件,在头文件中申明类类型和插入链表函数。在主程序中形成对象链表。根据对象的名字 来遍历链表,来找到目标对象。进而实现调用对象里面的功能函数。

继续阅读