#5 Design Patterns — Mixin Pattern

Hello Everyone, Welcome to my blog 🙏.
I hope you are all safe and that this blog finds you in good health ❤️.

In my previous blogs, we have learnt about
1. What are design patterns and What is Observer Pattern?
2. What is the Constructor Pattern?
3. What is the Prototype Pattern?
4. What is the Decorator Pattern?

In this blog, we will learn more about
👉 Mixin Pattern

Mixin Pattern

In my previous blogs, we came across the Decorator pattern that is used for adding new properties to the individual objects similar to it, Mixin pattern helps to borrow functionalities from other objects. In simple words, It help objects to use existing functionalities without code duplicate and inheritance.

How do Mixin Pattern works?
To understand it better, let us take an example

We know that all living beings have something in common.
Animals can walk, eat, sleep, etc.
Humans can also do the same but, we also think.

Now, let's see how the mixin pattern helps us to achieve this with minimal code.


Functional based approach

As you have noticed we get type error if animal1 tried to access think method which clearly states think method is only applicable for human1.
Thus, common actions are shared and used by objects that leverage code reusability here.

The same goes for class based approach.


We have seen how the Mixin pattern works.

When to use the Mixin pattern?

By definition, it is pretty clear that the Mixin pattern is used to eliminate code duplicates and leverage Code reusability.
For a language like Javascript, which doesn’t support multiple-inheritance, the Mixin pattern definitely comes in very handy in this situation.

for example:-
We have animals that live on land and in water and there are few species that can live both on land and in water.
So, these species are called Amphibians and they borrow both properties.
This scenario is not possible through Inheritance in Javascript so, we use Mixin to achieve this scenario.

Here we go, That’s it folks for this blog. Hope everyone liked it.
If you like it, give it a clap 👏 ,
and share it with your friend.

For more exciting content on Frontend, Please do follow me 🎯.

Thanks a lot, Everyone 🙏.
Until Next time,
Happy Learning ✍️

Abhishek Kovuri, UI developer




UI developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store