<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="115876" data-post-id="115876">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="fireproofsocks" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fireproofsocks/120/7669_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  fireproofsocks
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="OvermindDL1" data-post="16" data-topic="20100">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>This explicitness could even by a map in a config file</p>
</blockquote>
</aside>
<p>Could you elaborate on that?  One of the things I’m stuck on here is how I might have a package (say, something that gets published on Hex), and then how could I build it in a way that would open up customizations to users without requiring them to edit my code (which would be a big no-no)?</p>
<p>E.g. if I build out the mapping internally that handles the mapping of “condition-one” to <code>SomePrivateModule.do_something()</code> and “condition-two” to <code>SomeOtherPrivateModule.do_something()</code>, how could that mapping be extended via configuration so that a user could supply their own mapping so that “condition-three” maps to <code>UserDefinedModule.do_something()</code> ?</p>
<p>Thanks!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="115876" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/factory-pattern-in-elixir/20100/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-115876" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="115876"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="115880" data-post-id="115880">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="asummers" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/asummers/120/18515_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  asummers
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Protocols are what you’re looking for, I think. Elixir doesn’t know about all the enumerable data structures, but it defines a protocol that allows extension of your data structure to become enumerable, by doing</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defimpl Enumerable, for: MyModule do
  # enum protocol implementation
end
</code></pre>
<p>Similarly if your code defines a behaviour, and the library has a function that knows how to dispatch to the appropriate behaviour callbacks, you can simply pass your module into the dispatcher and it can execute the relevant callbacks, having faith that they’ll be there.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="115880" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/factory-pattern-in-elixir/20100/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-115880" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="115880"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="115906" data-post-id="115906">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="OvermindDL1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/OvermindDL1/120/2677_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  OvermindDL1
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="roblally" data-post="21" data-topic="20100">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/roblally/48/13834_2.png" class="avatar"> roblally:</div>
<blockquote>
<p>Neither Java nor C# existed when the GoF patterns were written down.</p>
</blockquote>
</aside>
<p>Just referencing modern day and what people would know it from.  I’ve used factories way back since before C was standardized so I know it is <em>very</em> old pattern.  ^.^;</p>
<aside class="quote no-group" data-username="roblally" data-post="21" data-topic="20100">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/roblally/48/13834_2.png" class="avatar"> roblally:</div>
<blockquote>
<p>Not all patterns are useful in all contexts, and you’re 100% right that different languages have different idioms and different needs.</p>
</blockquote>
</aside>
<p>This is the big key!!</p>
<aside class="quote no-group" data-username="fireproofsocks" data-post="22" data-topic="20100">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fireproofsocks/48/7669_2.png" class="avatar"> fireproofsocks:</div>
<blockquote>
<p>Could you elaborate on that? One of the things I’m stuck on here is how I might have a package (say, something that gets published on Hex), and then how could I build it in a way that would open up customizations to users without requiring them to edit my code (which would be a big no-no)?</p>
</blockquote>
</aside>
<p>You can put the ‘hardcoded’ map in the config files, then in the app anytime you need it access it via <code>Application.get_env/2</code> and query it directly.  You can even do that at compile-time to build up function matchers but by keeping it at run-time you can allow for it to be changed while the system is running to add/remove things to it without needing a hot code swap or reload.  <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>The configuration itself would just be a mapping of stringnames ↔ module names, or even full MFA’s if you want.  <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<aside class="quote no-group" data-username="asummers" data-post="23" data-topic="20100">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/asummers/48/18515_2.png" class="avatar"> asummers:</div>
<blockquote>
<p>Protocols are what you’re looking for, I think. Elixir doesn’t know about all the enumerable data structures, but it defines a protocol that allows extension of your data structure to become enumerable, by doing</p>
</blockquote>
</aside>
<p>Protocols are for dispatching on a type like a struct type so if you have that then absolutely yes, otherwise the plugin model is better (which you can also do via any arbitrary tag, like a string, with my ProtocolEx library ^.^).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="115906" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/factory-pattern-in-elixir/20100/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-115906" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="115906"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>