What is Monkey Patching?
The Monkey Patch named, monkey patch because it allows you to change the behavior of the functionality runtime. You can think of monkey patching as patching a road to fix the inconvenience without blocking the road completely :).
It’s basically you are messing up the code at runtime and that’s why I think it is named as Monkey Patch.
You can do following things with Monkey patching.
- Replace Methods/attributes/functions at runtime.
- You can modify or extend the behavior of third party code at run time without maintaining a private copy of the product's source code.
- You can apply a patch at runtime to the objects in memory not on the source code.
Let's understand this by example.
Now let's take a hypothetical example, I am just trying to explain the Monkey-Patching concept, you can solve this problem with easy way. Let's consider that above functionality is from third party library and after saving the data you also want to audit the data somewhere in file. We can achieve this with help of Monkey-Patching we can change the existing behavior of saveData, which will Save the data and also Audit the information.
Simplest way is to override the functionality (i.e. copy of the TestSaveData code and modify it)
To fix the above issue, you need to slightly modify the code. You can achieve this by saving the reference of original method and call it by using call or apply method.
By Saving the reference of Original method:
Hope this article was useful and you have got a understanding of Monkey-Patching concept.
Summary: Monkey-Patching is useful in scenarios where you are working with third party code and you want to extend/modify the behavior as per your requirements.
But it is always recommended that you should avoid using Monkey-Patching in your code, because it is very hard to understand/predict where will the extra behavior will execute, because it's monkey-patched somewhere in your code or not. Also it is very hard to debug in case of fixing/finding defects, because of monkey-patching changing the actual behavior many places in your application.