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 use template method pattern in C language

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

Share

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

What this article shares with you is about how to use the template method pattern in c language. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

Model motivation

In embedded application scenarios, managing resources (such as files, memory) is a very troublesome and error-prone thing. Because after allocating resources, resources must also be released. For example, after fopen () opens a file, you must use fclose () to close the file, while after using malloc to request memory resources, you must use the free () function to free memory.

In actual development work, a little carelessness to malloc can lead to memory leaks. And the template method model can be called an artifact to prevent such low-level errors!

Scene case

Scene: now that there are many movies stored on the hard drive card, we need to read and play them randomly on the computer.

Suppose we dynamically apply for 1 gigabyte of memory space to store the video. If the female host is a beautiful woman, then play the video normally and exit the program after playing. If the hostess looks touching, then quit the program immediately!

The pseudo code for a traditional implementation is as follows:

...

Int main ()

{

...

/ / apply for 1G memory

P_movie = malloc (1G)

/ / read the SD card video and store it in p_movie

...

/ / choose the playback method according to the type of movie

If (p_movie = = Beauty)

{

. / / normal movie playback

}

Else if (p_movie = = touching)

{

... / / stop playing the movie.

/ / release memory

Free (p_movie)

Return-1

}

/ / release memory

Free (p_movie)

Return 0

}

In the above code implementation, the code that manages memory and uses memory is coupled. In each branch case, you must always pay attention to memory usage and release (for example, in this case, the free function appears twice). As there are more and more branches in various programs, it is sometimes easy to ignore the release of memory, resulting in memory leaks.

Solution

Writing code related to this kind of resource processing is very troublesome because resource management and the code used by resources are coupled together. We only need to define a template method function to separate the two parts of the code. Their complex combinations can be avoided. Take a look at the following pseudo code:

/ / Resource usage code

Int act_movie (char* p)

{

If (p = = beauty)

{

. / / normal movie playback

}

Else if (p = = touching appearance)

{

... / / stop playing the movie.

Return-1

}

Return 0

}

/ / define template method function, responsible for resource management

Int template (int (* play_movie) (char* p))

{

Int ret

/ / apply for 1G memory

P_movie = malloc (1G)

/ / read the SD card video and store it in p_movie

...

/ / choose the playback method according to the type of movie

Ret = play_movie (p_movie)

/ / release memory

Free (p_movie)

Return ret

}

Int main ()

{

Int ret

...

/ / call the template method function

Ret = template (act_movie)

...

Return ret

}

In the above code implementation, we define a template function so that the allocation and release of resources are uniformly completed in the template function, which avoids the problem that it is easy to forget to release after allocating resources. In the process of using resources, we can focus more on the implementation of business logic, and the responsibilities of each function are clearer.

The allocation of memory is freed and may appear multiple times in the code. At this point, you only need to simply call the template method function, which reduces code duplication to a certain extent. If the usage scenario of the resource changes in the future, you only need to add a function similar to act_movie.

The above is how to use the template method pattern in the c language. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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