INTRO ๐
Recently I had to create a function with multiple methods๐ค. So I created a Factory Function
๐. If you don't know what a factory function is then don't worry๐, I will tell you in simple words that The function always returns an object called a factory function
. For more information visit this ๐๐ป factory factions๐ ๐๐ป. Then after I realised that all the methods have the same validation check๐ฌ. So I decided to create a decorator function to check the validation of all the methods๐ตโ๐ซ. So I started researching different blogs on how to achieve that but I didn't find any perfect solution, Even though I found, those are very complicated to understand and don't have an organised code format๐.
Today we will discuss how to implement the decorator function inside the factory function.๐
FACTORY FUNCTION ๐
As mentioned earlier, a factory function is simply a function that returns an object without using the keyword new so the end result of the function is the return of an object. ๐
๐๐ป ADVANTAGES
๐ First, it's a simple it's just a function it's there's no setup there's no fuss it's just a function and it's really easy to read
๐ No duplicate code and our logic is isolated in one place
๐ Have data privacy
๐๐ป EXAMPLE
//-----------------FACTORY FUNCTION----------------------
const factoryFn = (...args) => {
const sum = () => {
return args.reduce((curr, acc) => curr + acc, 0);
};
const multiple = () => {
return args.reduce((curr, acc) => curr * acc, 1);
};
const max=()=>{
return Math.max(...args)
}
return {
sum,
multiple,
max
};
};
const fn1 = factoryFn(1, 2, 3, 4);
console.log(fn1.sum());
console.log(fn1.multiple());
console.log(fn1.max());
//-------------------FACTORY FUNCTION WITH IIFE-----------------
const factoryFn = (() => {
const sum = (...args) => {
return args.reduce((curr, acc) => curr + acc, 0);
};
const multiple = (...args) => {
return args.reduce((curr, acc) => curr * acc, 1);
};
const max = (...args) => {
return Math.max(...args);
};
return {
sum,
multiple,
max,
};
})();
console.log(factoryFn.sum(1, 2, 3, 4));
console.log(factoryFn.multiple(1, 2, 3, 4));
console.log(factoryFn.max(1, 2, 3, 4));
DECORATOR FUNCTION ๐
A decorator function๐ฅ is simply a function๐ฅ that receives another function๐ฅ as a parameter and then returns a new function๐ฅ with extended behaviour. So you can pass a function๐ฅ into a decorator function๐ฅ and you'll get a new function๐ฅ back that does more than the function๐ฅ you passed in.
We already created one post for the ๐๐ปdecorator function๐๐ป. Please visit that post for more information. ๐๐ป
DECORATOR WITH FACTORY FUNCTION ๐๐๐
After a long discussion, finally, we came to the main topic ๐.
Here the code to implement decorator function inside the factory function ๐๐ป๐๐ป๐๐ป
const factoryFn = (() => {
const sum = (args) => {
return args.reduce((curr, acc) => curr + acc, 0);
};
const multiple = (args) => {
return args.reduce((curr, acc) => curr * acc, 1);
};
const max = (args) => {
return Math.max(...args);
};
const decorator = (callback) => {
return (args) => {
const isValidate = args.some((item) => Number.isInteger(item));
if (!isValidate) {
throw new TypeError("arguments cannot be non-integer");
}
return callback(args);
};
};
const passingFn = (fn, params) => {
const newFn = decorator(fn);
return newFn(params);
};
return {
sum(...params) {
return passingFn(sum, params);
},
multiple(...params) {
return passingFn(multiple, params);
},
max(...params) {
return passingFn(max, params);
},
};
})();
console.log(factoryFn.sum(1, 2, 3, 4));
console.log(factoryFn.multiple(1, 2, 3, 4));
console.log(factoryFn.max(1, 2, 3, 4));
๐Created one method named
passingFn
to avoid code duplication. That function helps to declare a new function by decorating existed function and returns that declared a function with enhanced feature (nothing but validation check)๐
decorator
method, we already discussed that. That returns the function that we passed as a callback with a validation check.๐The remaining methods are already existing ones
CONCLUSION ๐
I hope you guys understand how to implement decorator function inside the factory function.
We will meet in next with another concept
Peace ๐
Top comments (0)