<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jonathan Bartlett on Medium]]></title>
        <description><![CDATA[Stories by Jonathan Bartlett on Medium]]></description>
        <link>https://medium.com/@jonathanbartlett?source=rss-1356bd1caf0e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bVW35qVEctrAR8O1.jpg</url>
            <title>Stories by Jonathan Bartlett on Medium</title>
            <link>https://medium.com/@jonathanbartlett?source=rss-1356bd1caf0e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:30:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jonathanbartlett/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Bernie Bros should Love Trump]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@jonathanbartlett/bernie-bros-should-love-trump-f3f894b26491?source=rss-1356bd1caf0e------2"><img src="https://cdn-images-1.medium.com/max/1600/1*U6OYFf3okNMO86YGrEP-Sw.jpeg" width="1600"></a></p><p class="medium-feed-snippet">Let me start out by saying that I did not vote for Trump. I do not like his economic policies. But the reason that I don&#x2019;t like his&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/@jonathanbartlett/bernie-bros-should-love-trump-f3f894b26491?source=rss-1356bd1caf0e------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@jonathanbartlett/bernie-bros-should-love-trump-f3f894b26491?source=rss-1356bd1caf0e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3f894b26491</guid>
            <category><![CDATA[trump]]></category>
            <category><![CDATA[bernie-sanders]]></category>
            <category><![CDATA[economics]]></category>
            <category><![CDATA[economy]]></category>
            <category><![CDATA[donald-trump]]></category>
            <dc:creator><![CDATA[Jonathan Bartlett]]></dc:creator>
            <pubDate>Sat, 25 Apr 2020 04:23:23 GMT</pubDate>
            <atom:updated>2020-04-25T04:23:23.705Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Singletons and Singleton Factories are 1000x Better than Dependency Injection]]></title>
            <link>https://medium.com/@jonathanbartlett/singletons-and-singleton-factories-are-1000x-better-than-dependency-injection-a3675341f4f3?source=rss-1356bd1caf0e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3675341f4f3</guid>
            <category><![CDATA[singleton]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[coding-style]]></category>
            <dc:creator><![CDATA[Jonathan Bartlett]]></dc:creator>
            <pubDate>Thu, 11 Jul 2019 16:42:29 GMT</pubDate>
            <atom:updated>2019-07-11T16:42:29.516Z</atom:updated>
            <content:encoded><![CDATA[<p>A new development fad that is going on is “dependency injection”. The idea behind dependency injection is to avoid situations like this:</p><pre>class MyClass {<br>  let hardware = MyComplicatedHardware()</pre><pre>  func doStuff() {<br>    hardware.doStuffWithTheHardware()<br>  }<br>}</pre><p>The problem with this view is that, when it comes to testing, this is a really hard class to test. Since it instantiates an instance of something that deals with hardware, how can you run the test suite if the hardware isn’t attached? How do you mock the hardware in a way that isn’t crazy?</p><p>The suggested solution is dependency injection. This means changing your code to look like this:</p><pre>class MyClass {<br>  let hardware: MyComplicatedHardware<br>  init(hardware: MyComplicatedHardware) {<br>    self.hardware = hardware<br>  }</pre><pre>  func doStuff() {<br>    hardware.doStuffWithTheHardware()<br>  }<br>}</pre><p>So, here, we are passing in the dependency rather than creating it internally. This allows for easier testing. However, this style gets very cumbersome as complicated projects start to rack up dependencies. Your initializers start to get overly complicated, plus, you have to start carrying dependencies across your entire application. So, if, five view controllers down, you want to have a dependency injection, that dependency has to be carried all the way down to your class, meaning that all the other view controllers will need to carry it, too, adding to their own list of dependencies.</p><p>I’ve seen this get very out-of-control, and adding classes, refactoring classes, and especially adding dependencies, leads to what feels like a rewrite of a large percentage of the codebase.</p><p>Here’s an example:</p><pre>class BadDependencyInjection {<br>  // Giant list of dependencies here</pre><pre>  init(hardware: MyHardware, <br>        network: MyNetwork, <br>        locationManager: MyLocationManager, <br>        nextViewController: MyNextViewController, <br>        dependencyImCarryingForNextScreen1: DependencyType1, <br>        anotherDependencyForNextScreen: DependencyType2) {<br>    // Gosh, now we need variables to store all this junk.<br>    // And we&#39;ve got to assign them, too!!!  <br>    // And, with every new dependency, my interface changes! <br>    // And so does the interface of everything calling me<br>    // because they have to truck along the dependencies, too!<br>  }</pre><pre>  func goToNextViewController() {<br>    let vc = NextViewController(hardware: MyHardware, <br>        network: MyNetwork, <br>        locationManager: MyLocationManager, <br>        nextViewController: MyNextViewController, <br>        dependencyImCarryingForNextScreen1: DependencyType1, <br>        anotherDependencyForNextScreen: DependencyType2)<br>    // Oh shoot, NextViewController just added a dependency.<br>    // Now I have to add it, too, and change the way <br>    // I&#39;m calling it, and people who are calling me <br>    // have to change, too.<br>  }<br>}</pre><p>These are the sorts of programs that make me want to stop using computers. It takes what should be one line of code and expands it to hundreds. It takes what should be localized changes and turns them into breaking changes across your entire codebase. It’s the kind of thing that people who like charging by the hour dream of at night (disclosure — I charge by the hour, but I hate the kind of backward incentives it tends to create).</p><p>So what’s the solution?</p><p>Singletons and Singleton factories.</p><p>Singletons and singleton factories have all of the benefits of dependency injection, and none of the hassle. Let’s look at another way of writing this class. If there was only one instance of “MyComplicatedHardware” at any given time, we could make a singleton, like MyComplicatedHardware.shared, which could be accessed from anywhere. The code would then look like this:</p><pre>class MyClass {</pre><pre>  func doStuff() {<br>    MyComplicatedHardware.shared.doStuffWithTheHardware()<br>  }<br>}</pre><p>In this case, this is ridiculously easy to test, because you can simply make “shared” to be writeable, and replace it with your own mocking subclass (or protocol implementation) of MyComplicatedHardware. Nothing tricky or complicated.</p><p>Like this:</p><pre>class MyTest: XCTestCase {<br>  func testThis() {<br>    MyComplicatedHardware.shared = MyMockingComplicatedHardware()<br>    // Run test<br>  }<br>}</pre><p>If, on the other hand, we needed multiple instances of MyComplicatedHardware running around, instead of dependency injection, we can do a singleton factory. That is, we can have a factory which is a singleton, which generates our objects. You could call it something like “HardwareFactory”, or you could have a shared factory for all your dependencies called “DependencyFactory”. It would look like this:</p><pre>class MyClass {<br>  let hardware: MyComplicatedHardware<br>  init() {<br>    self.hardware = DependencyFactory.shared.makeHardware()<br>  }</pre><pre>  func doStuff() {<br>    hardware.doStuffWithTheHardware()<br>  }<br>}</pre><p>Here, rather than having to cart your hardware object from here to kingdom come, you can simply have a singleton factory taking care of making your objects for you. Then, for testing, you can subclass your DependencyFactory to create the kind of hardware you want, and replace DependencyFactory.shared with the version you are going to use to test with.</p><p>So, if you need something to be more testable, instead of junking up your code with dependency injection, use singletons or singleton factories instead!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3675341f4f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>