Creating Module Behaviors
Learn how to build behaviors in Elixir.
We'll cover the following...
Introduction to module behaviors
A contract sets the rules in an agreement between parties and indicates how the parties will benefit. For example, think of a job contract. It has rules for the employee and the employer, and by following those rules, both parties will reap specific benefits. If the rules are broken, though, those benefits aren’t guaranteed.
In Elixir, a behavior is a contract between a module and the client code that’s using it. It provides a common interface for a client across multiple modules. It means a client can use multiple modules in the same way since the modules provide the same functions with the same signatures defined in the behavior contract.
For example, Mix.Task
is a behavior. When we create a module that follows the Mix.Task
behavior, we must implement the function run/1
. If we don’t, Mix will have problems when trying to run our module as a task. It’s very useful to enforce the practice of developers creating consistent code when ...