fireproofsocks
I’m working on a problem that requires that a vendor-specific module be loaded to handle vendor-specific tasks related to an order. I’ve been trying out the behaviours, and that all makes sense. Coming from an OO background, however, I’m feeling the need to have a factory in this case… where I want to load up the proper module that implements the given interface, then call the do_something method on it. Conceptually, it’s pretty common OO.
However, in Elixir, the best I’ve come up with is either a big case statement or a series of declared functions that rely on pattern-matching in their signatures to send the execution flow to a specific vendor module, e.g. something like this:
def process_order("vendor-one"), do: VendorOne.process()
def process_order("vendor-two"), do: VendorTwo.process()
def process_order("vendor-two"), do: VendorThree.process()
Or outlined more academically here design-patterns-in-elixir/factory/run.exs at master · joshnuss/design-patterns-in-elixir · GitHub
Is this idiomatic? Is there some other way to dynamically get a module name based on a parameter?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #elixirconf-us
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
No.
Pass in the actual module name…
def process_order(module), do: module.process()and you are ready to go.PragTob
What @NobbZ said, you could just call
module.process()as then the function provides little extra value (I feel). Maybe call itvendor_moduleor something for more contextbenwilson512
Relatedly, this fits in nicely if you can define a Behaviour https://elixir-lang.org/getting-started/typespecs-and-behaviours.html for the module that will do the processing.
asummers
Using behaviour +
@impl MyBehaviourabove the callbacks will give the compiler the ability to help you if you get incorrect arity, or add an extra callback to the behaviour that your module doesn’t yet implement, for example.imetallica
Honestly, I don’t like the Factory pattern. I prefer to be a bit more data centric.
For example, each vendor has it’s own way to understand the orders, so you should have something like
%VendorOneOrder{},%VendorTwoOrder{}, etc… and they you apply theprocessingimplementing aProtocol(ProcessableOrder). Then you implement all the declared functions of the protocol. For example:And in each implementation, you do the following:
It will not solve the case problem, but at least you guarantee that the contract is kept in place if you add more
Vendors. But it’s another approach to the same solution.fireproofsocks
The responses here are far more interesting than I expected! Thanks!
Re passing the module name – to clarify, you are not passing a string representing the module name, right?
The problem, generally speaking, is that the logic that chooses which vendor (i.e. which module) to use must return a string because it is operating on database records and user input etc. So either I have a large
casestatement somewhere upstream that can specify the actual module names (not as strings), OR I have a largecasestatement and/or pattern matched functions somewhere downstream that establish the mapping between a string and an actual model name.In other words, I can force the mapping upstream like this:
And then enjoy a cleaner downstream with
def process_order(module), do: module.process()Or, I have a cleaner upstream, e.g.
But then downstream I have to do pattern-matching or a case statement, e.g.
david_ex
FYI you could use the following to get module names from strings:
Edit: as indicated by others below (e.g. @sasajuric’s), there are some better & safer approaches to this, so probably use one of those instead…
fireproofsocks
Mind. Blown. I hadn’t even considered this approach (because I hadn’t really grok’d protocols until I thought about it in this light).
sribe
ALL of the “classic” (i.e. Gang of Four) patterns are specific to statically-typed languages, and virtually disappear with idiomatic use of dynamically-typed languages.
See above comment
asummers
Peter Norvig’s article about that.