Simple factory mode

1. What is a design pattern

The summary of code design experience makes the code more stable and extensible.
Design patterns usually describe a set of classes and objects that work closely with the project. (JAVA)
There are 23 design patterns, which can be learned in this link:
https://www.runoob.com/design-pattern/design-pattern-tutorial.html
The introduction of design patterns can make the code easier to be understood by others, ensure the reliability of the code and the reusability of the program.
C -- process oriented is also an unfriendly object-oriented language
JAVA -- object oriented

2. What are classes and objects

Class: it is a user-defined reference data type, also known as class type (structure similar to c)
Object: a representation of a class

struct Animal{
        int age;
        int sex;//Member properties
        void (*peat)();//Member method
        void (*pbeat)();

};
struct Animal dog;
struct Animal cat;
struct Animal person;

A structure is a class; The following dog, cat and person are objects;

#include<stdio.h>

struct Animal{
	int age;
	int sex;//Member properties
	void (*peat)();//Member method
	void (*pbeat)();

};	
void dogEat()
{
	printf("dog eat bone\n");
}

void catEat()
{
	printf("cat eat fish\n");
}

void personEat()
{
	printf("perosn eat everying\n");
}

void personBeat()
{
	printf("person beat\n");
}
int main()
{
	struct Animal dog={
		.peat = dogEat,
	};

	struct Animal cat={
		.peat = catEat,
	};
	struct Animal person={
		.peat = personEat,
		.pbeat = personBeat//Use (,) comma after assignment instead of (;) semicolon
		//You don't need to write anything in the last item. If you write a comma, you won't report an error
		//This is how the linux kernel source code assigns values to the structure, which can improve the force and make a better impression during the interview
	};

	dog.peat();
	cat.peat();
	person.peat();
	person.pbeat();
	return 0;
}

3. What is the factory model

Factory Pattern is one of the commonly used design patterns. This type of design pattern is a creation pattern, which provides the best way to create objects.
In factory mode, when creating objects, we do not expose the creation logic to the client, and point to the newly created objects by using a common interface.

Code example:
Header file:

#include<stdio.h>

struct Animal{
	char name[128];
	int age;
        int sex;//Member properties 
        void (*peat)();//Member method
        void (*pbeat)();

	struct Animal *next;

};

struct Animal* putCatInLink(struct Animal *phead);
struct Animal* putdogInLink(struct Animal *phead);
struct Animal* putpersonInLink(struct Animal *phead);

cat.c

#include"Animal.h"
#include<stdio.h>

void catEat()
{
	printf("cat eat fish\n");
}

void catBeat()
{
	printf("cat beat\n");
}
struct Animal* putCatInLink(struct Animal *phead)
{
	if(phead == NULL){
		phead = &cat;
		return phead;
	}else{
		cat.next = phead;
		phead = &cat;
		return phead;
	}
}

struct Animal cat={
	.name = "Cat"
	.peat = catEat,
	.pbeat = catBeat
};

dog.c

#include"Animal.h"
#include<stdio.h>

void dogEat()
{
	printf("dog eat bone\n");
}

void dogBeat()
{
	printf("dog beat\n");
}
struct Animal* putdogInLink(struct Animal *phead)
{
	if(phead == NULL){
		phead = &dog;
		return phead;
	}else{
		dog.next = phead;
		phead = &dog;
		return phead;
	}
}

struct Animal cat={
	.name = "Dog",
	.peat = catEat,
	.pbeat = catBeat
};

person.c

#include"Animal.h"
#include<stdio.h>

void personEat()
{
        printf("perosn eat everying\n");
}

void personEat()
{
        printf("perosn eat everying\n");
}


struct Animal* putPersonInLink(struct Animal *phead)
{
	if(phead == NULL){
		phead = &person;
		return phead;
	}else{
		person.next = phead;
		phead = &person;
		return phead;
	}
}

struct Animal cat={
	.name = "Person"
	.peat = personEat,
	.pbeat = personBeat
};

Main function:

#include"Animal.h"
#include<string.h>

struct Animal *findUtilByName(char *str,struct Animal *phead)
{
        struct Animal *tmp = phead;

        if(tmp == NULL){
                printf("without this animal\n");
                return NULL;
        }else{
                while(tmp != NULL){
                        if(strcmp(tmp->name,str)==0){
                                return tmp;
                        }
                        tmp=tmp->next;
                }
                return NULL;
        }

}

int main()
{
        char buf[128]={'\0'};
        struct Animal *phead;
        struct Animal *ptem;
        phead = putCatInLink(phead);
        phead = putPersonInLink(phead);
        phead = putDogInLink(phead);
        phead = putHouseInLink(phead);
        while(1){
                memset(buf,'\0',sizeof(buf));
                printf("please input animal's name:Cat,Dog,Person\n");
                scanf("%s",buf);
                ptem = findUtilByName(buf,phead);

                if(ptem == NULL)
                {
                        printf("you input name is error\n");
                        printf("please input again\n");
                }
                if(ptem != NULL){                        
                		ptem->peat();
                        ptem->pbeat();
                }
        }

}

Examples of results:

Using factory mode can make it easier for us to add the functions we need. For example, we need to add a house C go in, we just need to write house C file, and add the function to the header file

#include"Animal.h"
#include<stdio.h>

void houseEat()
{
        printf("house eat grass\n");
}

void houseBeat()
{
        printf("house beat\n");
}

struct Animal house={
        .name = "House",
        .peat = houseEat,
        .pbeat = houseBeat
};
struct Animal* putHouseInLink(struct Animal *phead)
{
        if(phead == NULL){
                phead = &house;
                return phead;
        }else{
                house.next = phead;
                phead = &house;
                return phead;
        }
}

So the house is added

The blog post here is more detailed. Interested partners can refer to it———— Design mode - simple factory mode

Added by BloodyMind on Fri, 04 Mar 2022 11:22:09 +0200