# Factory Pattern in Elixir?

**URL:** <https://forum.elixirforum.com/t/factory-pattern-in-elixir/20100>\
**Category:** Questions\
**Tags:** design, patterns\
**Created:** [February 12, 2019, 6:30am UTC](https://forum.elixirforum.com/t/factory-pattern-in-elixir/20100 "2019-02-12T06:30:58Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [February 12, 2019, 4:46pm UTC](https://forum.elixirforum.com/t/factory-pattern-in-elixir/20100/12 "2019-02-12T16:46:52Z")

</div>

> [@david\_ex](#):
>
> ```elixir
> module_atom = String.to_existing_atom("Elixir." <> my_module)
> 
> ```

This has some pitfalls.

1. it will only work if the module name already exist! Either by preloading all modules or by having it already used in another already loaded module.
2. It relies on the (documented) implementation detail, that module names are actually built like this. Might break later on when we get private modules.

I tend to use `Module.concat/1/2` and `Module.safe_concat/1/2` here, depending on the level of trust I have in the data source, or an explicit whitelist, if I know **all** possible values in advance.

> [@fireproofsocks](#):
>
> So either I [verify my data upstream or downstream].

Yes. whenever you deal with some kind of stringified data you should verify it. In your case, you also need to convert it. This should happen as close as possible to the stringified data source. The same goes backwards, if you need to stringify it, do it as close to the data drain as possible. The remainder of your application should never have to deal with the stringified form, neither does it need to know it were strings somewhere else…

---

_[View the full topic](https://forum.elixirforum.com/t/factory-pattern-in-elixir/20100)._
