Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple Factory pattern in C language

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

C language how to achieve a simple factory model, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

I. Model motivation

First, let's take a look at such a demand: on this day, your boss said to you, "Xiao Li, the material of the company is not enough. You go to Jiali Chuang Mall to buy some 0805 capacitors, and then go to Jeddou to buy some 0603 capacitors." OK, so you go back to your station and get ready to start work.

Just imagine, if this scenario is implemented by a program, how should it be written? From the traditional process-oriented perspective of the C language, it should be written as follows:

# include

Void login_website (char * str)

Void enter_jlc ()

Void bug_jlc_capacity (char * str)

Void enter_jdb ()

Void bug_jdb_capacity (char * str)

Int main ()

{

/ * Log in to Taobao * /

Login_website ("www.taobao.com")

/ * enter Jialichuang flagship store * /

Enter_jlc ()

/ * purchase the 0805 capacitor of Jialichuang * /

Bug_jlc_capacity ("0805")

/ * enter the flagship store of Gettorio * /

Enter_jdb ()

/ * Buy 0805 capacitors from Jedoubang * /

Bug_jdb_capacity ("0603")

Return 0

}

Void login_website (char * str)

{

Printf ("Welcome to login:% s!\ n", str)

}

Void enter_jlc ()

{

Printf ("enter Jialichuang flagship store\ n")

}

Void bug_jlc_capacity (char * str)

{

Printf ("Buy Calico Capacitor:% s\ n", str)

}

Void enter_jdb ()

{

Printf ("enter the flagship store of Gettorio\ n")

}

Void bug_jdb_capacity (char * str)

{

Printf ("Buy Jedoubang Capacitor:% s\ n", str)

}

The code can be copied and pasted directly in the Cainiao C online tool to view the running results. The results are as follows:

Welcome to www.taobao.com!

Enter Jiali Chuang flagship store

Buy Jialichuang Capacitor: 0805

Enter the flagship store of Gettorio

Buy Jiedobang Capacitor: 0603

Don't laugh, it's true that every embedded software engineer will write this kind of code when he or she gets started. From the point of view of the function of the program, it does meet the requirements of the boss, go to different stores and merchants to buy back different materials.

But have you ever thought that if boss asks you to go to Jiedoubang first and then to Jialichuang tomorrow? Or ask you to go to other malls and buy more other materials?

Do you have to add a new function to achieve it every time you go into a different mall? And in each different mall to buy materials also need to add a new function to achieve? It won't take long, and you'll soon be overwhelmed by your program.

II. Solutions

How to solve the problem? First take a closer look at the requirements of boss, where two behaviors are repeated, namely, going to the mall and shopping at the mall, so we should extract it as an abstract class interface. Abstract class is usually called object-oriented language. In C language, class can be replaced by structure, and interface can be replaced by function pointer. An abstract class interface can be understood as a structure with only function pointers.

So here it is abstracted as:

Typedef struct shop_interface

{

Void (* enter) (); / * enter the mall * /

Void (* buy) (const char * str); / * purchase materials * /

} SHOP_INSTERFACE,*pSHOP_INSTERFACE

Jialichuang and Jiedoubang are also as a mall, entry and purchase and other basic functions must have. So it is only natural that both of them inherit this interface class.

/ * Jiali Chuang Mall * /

Struct jlc

{

SHOP_INSTERFACE jlc_interface

/ * Extensible other private attributes * /

}

/ * Gettorio Mall * /

Struct jdb

{

SHOP_INSTERFACE jdb_interface

/ * Extensible other private attributes * /

}

What we want is that the main program is like a customer. Customers only care about what services the mall can provide, and it doesn't care how the services are realized. Similarly, we do not want the main program to be disturbed by too many business details, and the main program should focus on business logic.

Therefore, the main program is concerned with interfaces (abstract class interfaces) that are closely related to business logic, and these interfaces must be separated from business and details, so what if the interface is separated from business and details? Take a look at the factory () function below.

PSHOP_INSTERFACE factory (const char * str)

{

If ("jlc" = = str)

{

Struct jlc* jlc_shop = (struct jlc*) malloc (sizeof (struct jlc))

/ * instantiate the interface * /

((pSHOP_INSTERFACE) jlc_shop)-> enter = enter_jlc

((pSHOP_INSTERFACE) jlc_shop)-> buy = bug_jlc_capacity

Return (pSHOP_INSTERFACE) jlc_shop

}

Else if ("jdb" = = str)

{

Struct jdb* jdb_shop = (struct jdb*) malloc (sizeof (struct jdb))

/ * instantiate the interface * /

((SHOP_INSTERFACE*) jdb_shop)-> enter = enter_jdb

((SHOP_INSTERFACE*) jdb_shop)-> buy = bug_jdb_capacity

Return (pSHOP_INSTERFACE) jdb_shop

}

}

As you can see, the abstract class interface is all instantiated in the factory () function. As long as the main program sets the specified parameters, it can get the interface it really wants through this factory () function.

Take a look at the modified main program.

Int main (void) {

PSHOP_INSTERFACE shop

/ * Log in to Taobao * /

Login_website ("www.taobao.com")

Shop = factory ("jlc")

/ * enter Jialichuang flagship store * /

Shop- > enter ()

/ * purchase the 0805 capacitor of Jialichuang * /

Shop- > buy ("0805")

Shop = factory ("jdb")

/ * enter the flagship store of Gettorio * /

Shop- > enter ()

/ * Buy 0603 capacitors from Jedoubang * /

Shop- > buy ("0603")

Return 0

}

The running result of the program:

Welcome to www.taobao.com!

Enter Jiali Chuang flagship store

Buy Jialichuang Capacitor: 0805

Enter the flagship store of Gettorio

Buy Jiedobang Capacitor: 0603

The function of the main program remains the same, but without any implementation details, it focuses entirely on the business logic. Business and details do not affect each other, which improves the maintainability and expansibility of the program.

This is an example of a simple factory pattern implemented in C language. Although it achieves the separation of business and details, it still has obvious defects, that is, the factory function factory () will inevitably appear if/else, switch/case and other judgment statements, so that every time you join a new mall, you have to modify this function, in violation of the open-closed principle (open and closed principle means that the module accepts extended function code, while the module should not modify its source code).

This problem is left to the "factory method model" in the next chapter.

The source code of the improved version is as follows, which can be directly copied and pasted in the Cainiao C online tool to view the running results.

# include

# include

# include

/ * Abstract class interface * /

Typedef struct shop_interface

{

Void (* enter) (); / * enter the mall * /

Void (* buy) (const char * str); / * purchase materials * /

} SHOP_INSTERFACE,*pSHOP_INSTERFACE

/ * Jiali Chuang Mall * /

Struct jlc

{

SHOP_INSTERFACE jlc_interface

/ * Extensible other private attributes * /

}

/ * Gettorio Mall * /

Struct jdb

{

SHOP_INSTERFACE jdb_interface

/ * Extensible other private attributes * /

}

Void login_website (const char * str)

Void enter_jlc ()

Void bug_jlc_capacity (const char * str)

Void enter_jdb ()

Void bug_jdb_capacity (const char * str)

PSHOP_INSTERFACE factory (const char * str)

Int main (void) {

PSHOP_INSTERFACE shop

/ * Log in to Taobao * /

Login_website ("www.taobao.com")

Shop = factory ("jlc")

Shop- > enter ()

Shop- > buy ("0805")

Shop = factory ("jdb")

Shop- > enter ()

Shop- > buy ("0603")

Return 0

}

Void login_website (const char * str)

{

Printf ("Welcome to login:% s!\ n", str)

}

Void enter_jlc ()

{

Printf ("enter Jialichuang flagship store\ n")

}

Void bug_jlc_capacity (const char * str)

{

Printf ("Buy Calico Capacitor:% s\ n", str)

}

Void enter_jdb ()

{

Printf ("enter the flagship store of Gettorio\ n")

}

Void bug_jdb_capacity (const char * str)

{

Printf ("Buy Jedoubang Capacitor:% s\ n", str)

}

PSHOP_INSTERFACE factory (const char * str)

{

If ("jlc" = = str)

{

Struct jlc* jlc_shop = (struct jlc*) malloc (sizeof (struct jlc))

/ * instantiate the interface * /

((pSHOP_INSTERFACE) jlc_shop)-> enter = enter_jlc

((pSHOP_INSTERFACE) jlc_shop)-> buy = bug_jlc_capacity

Return (pSHOP_INSTERFACE) jlc_shop

}

Else if ("jdb" = = str)

{

Struct jdb* jdb_shop = (struct jdb*) malloc (sizeof (struct jdb))

/ * instantiate the interface * /

((SHOP_INSTERFACE*) jdb_shop)-> enter = enter_jdb

((SHOP_INSTERFACE*) jdb_shop)-> buy = bug_jdb_capacity

Return (pSHOP_INSTERFACE) jdb_shop

}

}

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report