Today, I will share a design template that I designed for the universal use of features, which I lacked when creating projects.
 

 

I named the design pattern as INIT Pattern and as is obvious from the name, it will be a module for loading functions and working with them..
The module will use explicit constructors and call objects via references.

At the theoretical level, imagine the initialization of 10 functions of different publishers with different parameters, the results of which we need to move between ourselves.

Next, imagine a set of functions that contains 1500 elements. We are talking, for example, about neural networks. How do we come up with a module that would store the individual results of x initialized functions for the use of other functions, at the same time allowing their initialization, detection and the like?

I asked myself the same question when creating communication between individual sets of functions and I decided to create an initialization pattern.


Pattern can be like this:


#ifndef _IntPtrn_H #define _IntPtrn_H #endif
#ifdef MAX_VALUE_NAME #define MAX_BUFFER MAX_VALUE_NAME #else #define MAX_BUFFER 16383 #endif
class InitPaternW {
public: bool isinitstatus;
private: ::UINT tmpresult; ::UINT *result_array[MAX_BUFFER]; bool instanceflag; static InitPaternW instance; bool (*startadress)(...); bool (*startadress_ptr[MAX_BUFFER])(::UINT,...); bool initstatus; int size;
public: explicit InitPaternW(bool(*func_ptr[])(::UINT,...), int size) { *this->startadress_ptr = *func_ptr; this->size = size;
/* Declaration of possible variables for mass initializing
the function in the field. The advantage is the ability to
set the transmission of the result initializations between
individual initializations functions, from the first to the last.
In the case of the function of function A, which ends in failure, e.g.
init function B does not execute and jumps to init function C. */

if(this->initstatus == true) { this->isinitstatus = true; } };
explicit InitPaternW(bool(*functionadress)(...)) { this->startadress = functionadress; this->size = 0; };
explicit InitPaternW(void) { this->instanceflag = true; this->initstatus = false; this->isinitstatus = false; this->tmpresult = 0; this->size = 0; };
bool Start(InitPaternW & init) { return init.InitMe(); };
private: bool InitMe(void) { if(this->instanceflag != true) { if(this->size>0) { for(int i=0; i<this->size; í++) { this->tmpresult = this->startadress_ptr[i](this->result_array[i] = this->tmpresult; } else { this->startadress(); } return true; } return false; };
};
InitPaternW * InitPatern = new InitPaternW();

 



Example of calls functions:


#include "init_pattern.h"

bool Function_A(...) { ::std::cout << "Im Here _A"; return 0; } bool Function_B(...) { ::std::cout << "Im Here _B"; return 0; }

int main(void) {
/* start #1 : */
InitPatern->Start(InitPaternW(Function_A));
InitPatern->Start(InitPaternW(Function_B));

/* start #2 : */
bool (*func_ptr[])(...) = {Funkcia_A, Funkcia_B}; InitPatern->Start(InitPaternW(func_ptr, sizeof(func_ptr)/sizeof(bool)));
return 0
}