<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged factory-pattern</title>
<link>https://coderlegion.com/tag/factory-pattern</link>
<description>Powered by Question2Answer</description>
<item>
<title>TechTalk 20250611: Factory Pattern - Separating Creation from Use</title>
<link>https://coderlegion.com/3322/techtalk-20250611-factory-pattern-separating-creation-from-use</link>
<description>&lt;p&gt;At its core, the &lt;strong&gt;Factory Pattern&lt;/strong&gt; centralizes object creation into dedicated methods or classes, instead of scattering &lt;code&gt;new&lt;/code&gt; calls or complex constructors throughout your codebase. This separation yields several benefits:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clarity of Intent&lt;/strong&gt;&lt;br&gt;
A method named &lt;code&gt;CreateFromCsv()&lt;/code&gt; or &lt;code&gt;ConfigureNumberDisplay()&lt;/code&gt; immediately tells the reader &lt;em&gt;what&lt;/em&gt; kind of object will emerge, whereas a bare constructor &lt;code&gt;new TextDisplayConfiguration(…)&lt;/code&gt; may require you to look up parameter lists and defaults.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexible Return Types&lt;/strong&gt;&lt;br&gt;
Static factory methods can return any subtype or cached instance they choose. A constructor must always return a fresh instance of its own class.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simplified Overloads&lt;/strong&gt;&lt;br&gt;
Instead of dozens of overloaded constructors to cover different initialization scenarios, you provide a handful of well-named static methods (e.g. &lt;code&gt;WithTitle()&lt;/code&gt;, &lt;code&gt;WithSubtitle()&lt;/code&gt;), each encapsulating its own logic and defaults.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Validation and Caching&lt;/strong&gt;&lt;br&gt;
Factories can centralize validation (&lt;code&gt;throw&lt;/code&gt; early on bad arguments), perform expensive setup only once (returning a shared singleton), or even pool and reuse objects.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;static-factory-methods-vs-constructors&quot;&gt;Static Factory Methods vs Constructors&lt;/h3&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Aspect                          &lt;/th&gt;&lt;th&gt;Constructors                 &lt;/th&gt;&lt;th&gt;Static Factory Methods&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Naming &amp;amp; Discoverability&lt;/strong&gt;    &lt;/td&gt;&lt;td&gt;No name beyond class name    &lt;/td&gt;&lt;td&gt;Method names describe purpose&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Return Flexibility&lt;/strong&gt;          &lt;/td&gt;&lt;td&gt;Fixed to class it belongs to &lt;/td&gt;&lt;td&gt;Can return subtypes or cached refs&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Overload Explosion&lt;/strong&gt;          &lt;/td&gt;&lt;td&gt;Many overloads → unreadable  &lt;/td&gt;&lt;td&gt;Distinct methods → self-documented&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Default Configuration Logic&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;Scattered in callers         &lt;/td&gt;&lt;td&gt;Encapsulated in one place&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Conditional Instantiation&lt;/strong&gt;   &lt;/td&gt;&lt;td&gt;Hard to vary at runtime      &lt;/td&gt;&lt;td&gt;Easy to branch inside factory&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id=&quot;divooka-s-use-case-organizing-tools-by-category&quot;&gt;Divooka's Use Case: Organizing Tools by Category&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mz2j271vz39xe3ochc4m.png&quot; alt=&quot;Functions Tray in Divooka - Functions Organized as Categories under Toolboxes&quot;&gt;&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Divooka&lt;/strong&gt;, every static method in a class becomes a &lt;em&gt;toolbox&lt;/em&gt; entry - with the class name serving as the toolbox &lt;strong&gt;category&lt;/strong&gt;. By grouping related factory methods inside dedicated &lt;code&gt;static class&lt;/code&gt; containers (e.g. &lt;code&gt;PlotConfigurations&lt;/code&gt;), Divooka can automatically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Discover&lt;/strong&gt; all available tools at startup&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Organize&lt;/strong&gt; them under logical headings (one class = one category)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Expose&lt;/strong&gt; named methods like &lt;code&gt;ConfigureTextDisplayWithTitleAndSubtitle&lt;/code&gt; directly to end-users&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Were these methods instead constructors on each configuration type, Divooka would lack a natural grouping: you'd have to scan every class for all its constructors, losing that simple &quot;category = class&quot; mapping based on usage.&lt;/p&gt;
&lt;h3 id=&quot;case-study-asp-net-core-s-builder-based-factory-approach&quot;&gt;Case Study: ASP .NET Core's Builder-Based Factory Approach&lt;/h3&gt;
&lt;p&gt;ASP .NET Core also relies heavily on factories - but often via the &lt;strong&gt;builder pattern&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;Host.CreateDefaultBuilder(args)
    .ConfigureServices((ctx, services) =&amp;gt;
    {
        services.AddDbContext&amp;lt;MyDbContext&amp;gt;(opts =&amp;gt; 
            opts.UseSqlServer(connectionString));
        services.AddSingleton&amp;lt;IMyService, MyService&amp;gt;();
    })
    .Build();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here the &quot;factory&quot; is implicit in the &lt;code&gt;IHostBuilder&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fluent configuration&lt;/strong&gt; methods (&lt;code&gt;ConfigureServices&lt;/code&gt;, &lt;code&gt;UseUrls&lt;/code&gt;, etc.) collect options&lt;/li&gt;
&lt;li&gt;The final &lt;code&gt;Build()&lt;/code&gt; call &lt;em&gt;manufactures&lt;/em&gt; the fully wired &lt;code&gt;IHost&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;You get immutability until build time, plus a clear sequence of configuration steps&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;comparing-static-vs-builder-factories&quot;&gt;Comparing Static vs. Builder Factories&lt;/h4&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Feature             &lt;/th&gt;&lt;th&gt;Static Factory Methods              &lt;/th&gt;&lt;th&gt;Builder Pattern (ASP NET Core)&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Discovery&lt;/strong&gt;       &lt;/td&gt;&lt;td&gt;Simple reflection on static classes &lt;/td&gt;&lt;td&gt;Extension-method based; discoverable by IDE but less obvious at runtime&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Grouping&lt;/strong&gt;        &lt;/td&gt;&lt;td&gt;One class = one category            &lt;/td&gt;&lt;td&gt;Spread across many fluent calls&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Extensibility&lt;/strong&gt;   &lt;/td&gt;&lt;td&gt;Add new methods in classes          &lt;/td&gt;&lt;td&gt;Add new extension methods on interfaces&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Configurability&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;Encapsulated per-method logic       &lt;/td&gt;&lt;td&gt;Centralized in a mutable builder state&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Readability&lt;/strong&gt;     &lt;/td&gt;&lt;td&gt;Direct: &lt;code&gt;MyFactory.Create(…)&lt;/code&gt;       &lt;/td&gt;&lt;td&gt;Fluent chaining: &lt;code&gt;builder.Configure…&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;In many small‐to‐medium scenarios, &lt;strong&gt;static factory methods&lt;/strong&gt; win on simplicity: you immediately see each creation scenario, and end-users can invoke exactly what they need. In very large frameworks (like ASP .NET Core), the &lt;strong&gt;builder&lt;/strong&gt; shines by aggregating dozens of configuration concerns into a single, coherent pipeline before creating the final product.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;The Factory Pattern - whether via simple static methods or a full builder API - decouples &lt;em&gt;what&lt;/em&gt; you need from &lt;em&gt;how&lt;/em&gt; it's built. Choosing static factory methods:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Keeps creation logic in one readable place&lt;/li&gt;
&lt;li&gt;Leverages method naming to self-document different initialization paths&lt;/li&gt;
&lt;li&gt;Allows frameworks like Divooka to auto-discover and categorize tools&lt;/li&gt;
&lt;li&gt;Avoids constructor overload chaos&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Meanwhile, builder-style factories excel when you have large configuration graphs to assemble in a defined sequence. By understanding both approaches, you can pick the pattern that best balances clarity, flexibility, and discoverability in your own projects.&lt;/p&gt;
</description>
<category>Best Practices</category>
<guid isPermaLink="true">https://coderlegion.com/3322/techtalk-20250611-factory-pattern-separating-creation-from-use</guid>
<pubDate>Wed, 11 Jun 2025 13:28:15 +0000</pubDate>
</item>
</channel>
</rss>