<?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[Design Patterns In Python - Medium]]></title>
        <description><![CDATA[Common GoF (Gang of Four) Design Patterns Implemented in Python. - Medium]]></description>
        <link>https://medium.com/design-patterns-in-python?source=rss----e49f3bd4ba46---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Design Patterns In Python - Medium</title>
            <link>https://medium.com/design-patterns-in-python?source=rss----e49f3bd4ba46---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 00:17:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/design-patterns-in-python" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Bridge Pattern in Python]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/design-patterns-in-python/bridge-pattern-in-python-e2b7a731423d?source=rss----e49f3bd4ba46---4"><img src="https://cdn-images-1.medium.com/max/950/1*L2PfMsmi9MkTncflbFhatw.png" width="950"></a></p><p class="medium-feed-snippet">The Bridge pattern is similar to the Adapter pattern except in the intent that you developed it.</p><p class="medium-feed-link"><a href="https://medium.com/design-patterns-in-python/bridge-pattern-in-python-e2b7a731423d?source=rss----e49f3bd4ba46---4">Continue reading on Design Patterns In Python »</a></p></div>]]></description>
            <link>https://medium.com/design-patterns-in-python/bridge-pattern-in-python-e2b7a731423d?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/e2b7a731423d</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[bridge-pattern]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Mon, 18 Apr 2022 07:00:04 GMT</pubDate>
            <atom:updated>2022-04-18T07:00:04.038Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Flyweight Patterns]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/design-patterns-in-python/flyweight-patterns-6202c2243ce8?source=rss----e49f3bd4ba46---4"><img src="https://cdn-images-1.medium.com/max/657/1*RIu3HL-B4-PeYTEzmaiacw.png" width="657"></a></p><p class="medium-feed-snippet">Fly in the term Flyweight means light/not heavy.</p><p class="medium-feed-link"><a href="https://medium.com/design-patterns-in-python/flyweight-patterns-6202c2243ce8?source=rss----e49f3bd4ba46---4">Continue reading on Design Patterns In Python »</a></p></div>]]></description>
            <link>https://medium.com/design-patterns-in-python/flyweight-patterns-6202c2243ce8?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/6202c2243ce8</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[flyweight-pattern]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Tue, 16 Mar 2021 08:04:40 GMT</pubDate>
            <atom:updated>2021-10-20T21:12:52.889Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Memento Pattern]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/design-patterns-in-python/memento-pattern-eba610b3b59c?source=rss----e49f3bd4ba46---4"><img src="https://cdn-images-1.medium.com/max/607/1*Te7g0EHylsotgYcWkrE2Ew.png" width="607"></a></p><p class="medium-feed-snippet">Throughout the lifecycle of an application, an objects state may change. You might want to store a copy of the current state in case of&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/design-patterns-in-python/memento-pattern-eba610b3b59c?source=rss----e49f3bd4ba46---4">Continue reading on Design Patterns In Python »</a></p></div>]]></description>
            <link>https://medium.com/design-patterns-in-python/memento-pattern-eba610b3b59c?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/eba610b3b59c</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[design-patter]]></category>
            <category><![CDATA[memento-pattern]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Wed, 03 Mar 2021 13:26:31 GMT</pubDate>
            <atom:updated>2021-09-15T07:17:22.710Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Visitor Pattern]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/design-patterns-in-python/visitor-pattern-b9227759d6be?source=rss----e49f3bd4ba46---4"><img src="https://cdn-images-1.medium.com/max/608/1*bBMMBNtCoLKuF0DAH5juow.png" width="608"></a></p><p class="medium-feed-snippet">Your object structure inside an application may be complicated and varied. A good example is what could be created using the composite&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/design-patterns-in-python/visitor-pattern-b9227759d6be?source=rss----e49f3bd4ba46---4">Continue reading on Design Patterns In Python »</a></p></div>]]></description>
            <link>https://medium.com/design-patterns-in-python/visitor-pattern-b9227759d6be?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/b9227759d6be</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[visitor-pattern]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Wed, 03 Mar 2021 11:33:52 GMT</pubDate>
            <atom:updated>2021-10-20T21:15:55.507Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Design Patterns In Python (Book)]]></title>
            <link>https://medium.com/design-patterns-in-python/design-patterns-in-python-book-d284b6dfe11b?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/d284b6dfe11b</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Sun, 28 Feb 2021 10:28:45 GMT</pubDate>
            <atom:updated>2021-09-15T08:27:12.504Z</atom:updated>
            <content:encoded><![CDATA[<p>Sometimes you just want to switch off your computer and read from a book. So, I’ve revisited and rewritten all of the 23 GoF design patterns and compiled them into a book titled Design Patterns In Python. ASIN B08XLJ8Z2J</p><p>Book provides FREE access to online instructional videos.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7LpjChvk34861JQ6IYdT9w.jpeg" /></figure><p><a href="https://www.amazon.com/dp/B08XLJ8Z2J">Design Patterns in Python: Common GOF (Gang of Four) Design Patterns implemented in Python (Software Engineering)</a></p><p>Design patterns will give you a useful and common vocabulary for when designing, documenting, analysing restructuring new and existing software development projects now and into the future.</p><p>Sean</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d284b6dfe11b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/design-patterns-in-python/design-patterns-in-python-book-d284b6dfe11b">Design Patterns In Python (Book)</a> was originally published in <a href="https://medium.com/design-patterns-in-python">Design Patterns In Python</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Proxy Design Pattern]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/design-patterns-in-python/proxy-design-pattern-84fcfd82c4be?source=rss----e49f3bd4ba46---4"><img src="https://cdn-images-1.medium.com/max/600/1*DOucYH97DQ616jg7wkDfTA.png" width="600"></a></p><p class="medium-feed-snippet">The proxy design pattern is a class functioning as an interface to another class or object.</p><p class="medium-feed-link"><a href="https://medium.com/design-patterns-in-python/proxy-design-pattern-84fcfd82c4be?source=rss----e49f3bd4ba46---4">Continue reading on Design Patterns In Python »</a></p></div>]]></description>
            <link>https://medium.com/design-patterns-in-python/proxy-design-pattern-84fcfd82c4be?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/84fcfd82c4be</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[proxy-design-pattern]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Mon, 17 Aug 2020 18:30:31 GMT</pubDate>
            <atom:updated>2021-09-15T07:33:19.130Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Composite Design Pattern]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/design-patterns-in-python/composite-design-pattern-7873cb695765?source=rss----e49f3bd4ba46---4"><img src="https://cdn-images-1.medium.com/max/872/1*wWG16SNY4VArXGqutPKSIw.png" width="872"></a></p><p class="medium-feed-snippet">The Composite design pattern,</p><p class="medium-feed-link"><a href="https://medium.com/design-patterns-in-python/composite-design-pattern-7873cb695765?source=rss----e49f3bd4ba46---4">Continue reading on Design Patterns In Python »</a></p></div>]]></description>
            <link>https://medium.com/design-patterns-in-python/composite-design-pattern-7873cb695765?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/7873cb695765</guid>
            <category><![CDATA[composite-design-pattern]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Mon, 17 Aug 2020 18:22:48 GMT</pubDate>
            <atom:updated>2022-04-18T07:26:08.081Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Decorator Design Pattern]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/design-patterns-in-python/decorator-design-pattern-f652070be7dd?source=rss----e49f3bd4ba46---4"><img src="https://cdn-images-1.medium.com/max/614/1*C39qQZZfNuJQox6jJI-HwA.png" width="614"></a></p><p class="medium-feed-snippet">The decorator pattern is a structural pattern, that allows you to attach additional responsibilities to an object at run time.</p><p class="medium-feed-link"><a href="https://medium.com/design-patterns-in-python/decorator-design-pattern-f652070be7dd?source=rss----e49f3bd4ba46---4">Continue reading on Design Patterns In Python »</a></p></div>]]></description>
            <link>https://medium.com/design-patterns-in-python/decorator-design-pattern-f652070be7dd?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/f652070be7dd</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[decorator-design-pattern]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Mon, 17 Aug 2020 18:13:32 GMT</pubDate>
            <atom:updated>2022-04-18T07:13:52.159Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Iterator Design Pattern]]></title>
            <link>https://medium.com/design-patterns-in-python/iterator-design-pattern-54655e97552c?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/54655e97552c</guid>
            <category><![CDATA[iterator-design-pattern]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Mon, 17 Aug 2020 18:09:22 GMT</pubDate>
            <atom:updated>2021-09-15T07:50:16.665Z</atom:updated>
            <content:encoded><![CDATA[<h3>Iterator Pattern</h3><p>The Iterator will commonly contain two methods that perform the following concepts.</p><ul><li><strong>next</strong>: returns the next object in the aggregate (collection, object).</li><li><strong>has_next</strong>: returns a Boolean indicating if the Iterable is at the end of the iteration or not.</li></ul><p>The benefits of using the Iterator pattern are that the client can traverse a collection of aggregates(objects) without needing to understand their internal representations and/or data structures.</p><h3>Terminology</h3><ul><li><strong>Iterator Interface</strong>: The Interface for an object to implement.</li><li><strong>Concrete Iterator</strong>: (Iterable) The instantiated object that implements the iterator and contains a collection of aggregates.</li><li><strong>Aggregate Interface</strong>: An interface for defining an aggregate (object).</li><li><strong>Concrete Aggregate</strong>: The object that implements the Aggregate interface.</li></ul><h3>Iterator Pattern UML Diagram</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*hP95MVmJ6ziowhvBi__lNA.png" /><figcaption>Iterator Pattern UML Diagram</figcaption></figure><h3>Source Code</h3><p>In this concept example, I create 4 objects called Aggregate and group them into a collection.</p><p>They are very minimal objects that implement one method that prints a line.</p><p>I then create an Iterable and pass in the collection of Aggregates.</p><p>I can now traverse the aggregates through the Iterable interface.</p><h3>./iterator/iterator_concept.py</h3><pre>&quot;&quot;&quot;<br>The Iterator Pattern Concept<br>https://sbcode.net/python/iterator/#iteratoriterator_conceptpy<br>&quot;&quot;&quot;</pre><pre>from abc import ABCMeta, abstractmethod</pre><pre>class IIterator(metaclass=ABCMeta):<br>    &quot;An Iterator Interface&quot;<br>    @staticmethod<br>    @abstractmethod<br>    def has_next():<br>        &quot;Returns Boolean whether at end of collection or not&quot;</pre><pre>    @staticmethod<br>    @abstractmethod<br>    def next():<br>        &quot;Return the object in collection&quot;</pre><pre>class Iterable(IIterator):<br>    &quot;The concrete iterator (iterable)&quot;</pre><pre>    def __init__(self, aggregates):<br>        self.index = 0<br>        self.aggregates = aggregates</pre><pre>    def next(self):<br>        if self.index &lt; len(self.aggregates):<br>            aggregate = self.aggregates[self.index]<br>            self.index += 1<br>            return aggregate<br>        raise Exception(&quot;AtEndOfIteratorException&quot;, &quot;At End of Iterator&quot;)</pre><pre>    def has_next(self):<br>        return self.index &lt; len(self.aggregates)</pre><pre>class IAggregate(metaclass=ABCMeta):<br>    &quot;An interface that the aggregates should implement&quot;<br>    @staticmethod<br>    @abstractmethod<br>    def method():<br>        &quot;a method to implement&quot;</pre><pre>class Aggregate(IAggregate):<br>    &quot;A concrete object&quot;<br>    @staticmethod<br>    def method():<br>        print(&quot;This method has been invoked&quot;)</pre><pre># The Client<br>AGGREGATES = [Aggregate(), Aggregate(), Aggregate(), Aggregate()]<br># AGGREGATES is a python list that is already iterable by default.</pre><pre># but we can create own own iterator on top anyway.<br>ITERABLE = Iterable(AGGREGATES)</pre><pre>while ITERABLE.has_next():<br>    ITERABLE.next().method()</pre><h3>Output</h3><pre>python ./iterator/iterator_concept.py<br>This method has been invoked<br>This method has been invoked<br>This method has been invoked<br>This method has been invoked</pre><h3>Video of the Iterator Pattern</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FTtszPXf3qjE%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DTtszPXf3qjE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FTtszPXf3qjE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a506c79b8e4187336db46c0e93a225c/href">https://medium.com/media/1a506c79b8e4187336db46c0e93a225c/href</a></iframe><h3>Example Use Case</h3><p>Visit <a href="https://sbcode.net/python/iterator/#example-use-case">Iterator — Design Patterns In Python (sbcode.net)</a> for an example use case of the Iterator pattern.</p><p>One reason for not using the inbuilt Python data structures that implement iterators already, or using the <a href="https://sbcode.net/python/iterator/#python-iter">iter</a> function directly over an existing collection, is in the case when you want to create an object that can dynamically create iterated objects, you want a custom order of objects or an infinite iterator.</p><p>The iterator in the use case example will return the next number in the iterator multiplied by 2 modulus 11. It dynamically creates the returned object (number) at runtime.</p><p>It has no has_next() method since the result is modulated by 11, that will loop the results no matter how large the iterator index is. It will also appear to alternate between a series of even numbers and odd numbers.</p><p>Also, just to demonstrate that implementing abstract classes and interfaces is not always necessary, this example uses no abstract base classes or interfaces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*AmM2pjrUxft_YrK9yRUDKQ.png" /><figcaption>Iterator Use Case Example UML Diagram</figcaption></figure><h3>Video of an Iterator Pattern use case</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FHvlAPUce_GU&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DHvlAPUce_GU&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2FHvlAPUce_GU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/87203a65d48ef07e340d1515278cf443/href">https://medium.com/media/87203a65d48ef07e340d1515278cf443/href</a></iframe><h3>Summary</h3><ul><li>There are many ways to create Iterators. They are already built into Python and used instead of creating custom classes.</li><li>Use an iterator when you need to traverse over a collection, or you want an object that can output a series of dynamically created objects.</li><li>At minimum, an iterator needs a next equivalent method that returns an object.</li><li>Optionally you can also create a helper function that indicates whether an iterator is at the end or not. This is useful if you use your iterator in a while loop.</li><li>Alternatively, use the sentinel option of the Python iter() method to indicate the last iteration. Note that the Iterator object needs to be callable. Set the __call__ reference to its next method.</li></ul><p>Thankyou for reading my quick tutorial on the Iterator Design Pattern. For more design patterns please visit my series on Design Patterns in Python at <a href="https://medium.com/design-patterns-in-python">https://medium.com/design-patterns-in-python</a></p><p>Sean</p><h3>Design Patterns In Python (Book)</h3><p>You can also buy this series in paperback.<br>Book provides FREE access to online instructional videos. <br>Design Patterns In Python : ASIN B08XLJ8Z2J</p><p><a href="https://www.amazon.com/dp/B08XLJ8Z2J">Design Patterns in Python: Common GOF (Gang of Four) Design Patterns implemented in Python (Software Engineering)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54655e97552c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/design-patterns-in-python/iterator-design-pattern-54655e97552c">Iterator Design Pattern</a> was originally published in <a href="https://medium.com/design-patterns-in-python">Design Patterns In Python</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mediator Design Pattern]]></title>
            <link>https://medium.com/design-patterns-in-python/mediator-design-pattern-5b1a8d05b8?source=rss----e49f3bd4ba46---4</link>
            <guid isPermaLink="false">https://medium.com/p/5b1a8d05b8</guid>
            <category><![CDATA[mediator-pattern]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Sean Bradley]]></dc:creator>
            <pubDate>Mon, 17 Aug 2020 18:09:09 GMT</pubDate>
            <atom:updated>2021-09-15T07:47:35.398Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mediator Pattern</h3><p>Objects communicate through the <strong>Mediator</strong> rather than directly with each other.</p><p>As a system evolves and becomes larger and supports more complex functionality and business rules, the problem of communicating between these components becomes more complicated to understand and manage. It may be beneficial to refactor your system to centralize some or all of its functionality via some kind of mediation process.</p><p>The mediator pattern is similar to creating a <a href="https://medium.com/design-patterns-in-python/facade-design-pattern-a29c94776870">Facade</a> pattern between your classes and processes. Except the Mediator is expected to transact data both ways between two or more other classes or processes that would normally interact directly with each other.</p><p>The resulting Mediator interface will be very custom to the use cases that it is now supporting.</p><p>The Mediator will generally look like an object that is managing one of more <a href="https://medium.com/design-patterns-in-python/observer-pattern-c58820ad3c9f">Observer</a> patterns perhaps between the other classes or processes (colleagues). Whether you use an Observer pattern to manage a particular piece of functionality or not depends on whether it is the best use of the resources you have available.</p><p>When refactoring your code, you may decide to approach your refactoring from the perspective of implementing an Observer pattern first. This means that all colleagues (Observers) will receive the notification whether it was intended for them or not. If you want to avoid redundant updates in the colleagues then you can write specific cases in your code, or create specific methods as I have done in mediator_concept.py example below.</p><p>Colleagues now will send and receive requests via a Mediator object rather than directly between each other. The Mediator is like a router in this case, but allows you to add extra programmatic functionality and also give the option of creating other kinds of colleagues that could utilize the communications in new ways.</p><h3>Terminology</h3><ul><li><strong>Mediator Interface</strong>: An interface that the Mediator and Colleagues implement. Note that different Colleagues will have varied use cases and won’t need to implement all the methods described in the Mediator interface.</li><li><strong>Concrete Mediator</strong>: The single source of truth and coordinator of communications between the components (colleagues).</li><li><strong>Colleague Classes</strong>: One of the many types of concrete components that use the mediator for its own particular use case.</li></ul><h3>Mediator Pattern UML Diagram</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*S-5SquNjmfRz6YQ7tYQZuQ.png" /><figcaption>Mediator Pattern UML Diagram</figcaption></figure><h3>Source Code</h3><p>In the example concept, there are two colleague classes that use each other’s methods. Instead of the Colleagues calling each other’s methods directly, they implement the Mediator interface and call each other via the Mediator. Each colleague class or process is designed for a different purpose, but they utilize some related functionality from each other.</p><p>The system would work without the Mediator, but adding the Mediator will allow extending functionality to a potential third colleague that provides a different service, such as AI analysis or monitoring, without needing to add specific support or knowledge into the two original colleagues.</p><h3>./mediator/mediator_concept.py</h3><pre>&quot;&quot;&quot;<br>Mediator Concept Sample Code<br><a href="https://sbcode.net/python/mediator/#mediatormediator_conceptpy">https://sbcode.net/python/mediator/#mediatormediator_conceptpy</a><br>&quot;&quot;&quot;</pre><pre>from abc import ABCMeta, abstractmethod</pre><pre>class IMediator(metaclass=ABCMeta):<br>    &quot;The Mediator interface indicating all the methods to implement&quot;<br>    @staticmethod<br>    @abstractmethod<br>    def colleague1_method():<br>        &quot;A method to implement&quot;</pre><pre>    @staticmethod<br>    @abstractmethod<br>    def colleague2_method():<br>        &quot;A method to implement&quot;</pre><pre>class Mediator(IMediator):<br>    &quot;The Mediator Concrete Class&quot;</pre><pre>    def __init__(self):<br>        self.colleague1 = Colleague1()<br>        self.colleague2 = Colleague2()</pre><pre>    def colleague1_method(self):<br>        return self.colleague1.colleague1_method()</pre><pre>    def colleague2_method(self):<br>        return self.colleague2.colleague2_method()</pre><pre>class Colleague1(IMediator):<br>    &quot;This Colleague calls the other Colleague via the Mediator&quot;</pre><pre>    def colleague1_method(self):<br>        return &quot;Here is the Colleague1 specific data you asked for&quot;</pre><pre>    def colleague2_method(self):<br>        &quot;not implemented&quot;</pre><pre>class Colleague2(IMediator):<br>    &quot;This Colleague calls the other Colleague via the Mediator&quot;</pre><pre>    def colleague1_method(self):<br>        &quot;not implemented&quot;</pre><pre>    def colleague2_method(self):<br>        return &quot;Here is the Colleague2 specific data you asked for&quot;</pre><pre># This Client is either Colleague1 or Colleague2<br># This Colleague will instantiate a Mediator, rather than calling<br># the other Colleague directly.<br>MEDIATOR = Mediator()</pre><pre># If I am Colleague1, I want some data from Colleague2<br>DATA = MEDIATOR.colleague2_method()<br>print(f&quot;COLLEAGUE1 &lt;--&gt; {DATA}&quot;)</pre><pre># If I am Colleague2, I want some data from Colleague1<br>DATA = MEDIATOR.colleague1_method()<br>print(f&quot;COLLEAGUE2 &lt;--&gt; {DATA}&quot;)</pre><h3>Output</h3><pre>python ./mediator/mediator_concept.py    <br>COLLEAGUE1 &lt;--&gt; Here is the Colleague2 specific data you asked for<br>COLLEAGUE2 &lt;--&gt; Here is the Colleague1 specific data you asked for</pre><h3>Video of the Mediator Pattern</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F9bcLUtBoO04&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D9bcLUtBoO04&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2F9bcLUtBoO04%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf594a314ccbd30eaa2798727e678c2a/href">https://medium.com/media/bf594a314ccbd30eaa2798727e678c2a/href</a></iframe><h3>Example Use Case</h3><p>Visit <a href="https://sbcode.net/python/mediator/#example-use-case">Mediator — Design Patterns In Python (sbcode.net)</a> for a use case example of the Mediator pattern.</p><p>The use case example is a simplified game engine. There is the main game engine component, a scheduler that manages game events and there are game clients that act as separate game players submitting bets into a game round.</p><p>All of the components implement the Mediators interface. They all implement one or some of the methods differently depending on their purpose. While they all perform different types of functionality, they all require a single source of truth being the Game Engine that acts as the Mediator.</p><h3>Summary</h3><ul><li>A mediator replaces a structure with many-to-many interactions between its classes and processes, with a one-to-many centralized structure where the interface supports all of the methods of the many-to-many structure, but via the mediator component instead.</li><li>The mediator pattern encourages usage of shared objects that can now be centrally managed and synchronized.</li><li>The mediator pattern creates an abstraction between two or more components that then makes a system easier to understand and manage.</li><li>The mediator pattern is similar to the <a href="https://medium.com/design-patterns-in-python/facade-design-pattern-a29c94776870">Facade</a> pattern, except the Mediator is expected to transact data both ways between two or more other classes or processes that would normally interact directly with each other.</li></ul><h3>Video of a Mediator Pattern use case</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FIIOkn92bVqA&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIIOkn92bVqA&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2FIIOkn92bVqA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a803a8b75658b7abaf1144a063bb3eaf/href">https://medium.com/media/a803a8b75658b7abaf1144a063bb3eaf/href</a></iframe><p>Thankyou for reading my quick tutorial on the Mediator Design Pattern. For more design patterns please visit my series on Design Patterns in Python at <a href="https://medium.com/design-patterns-in-python">https://medium.com/design-patterns-in-python</a></p><p>Sean</p><h3>Design Patterns In Python (Book)</h3><p>You can also buy this series in paperback.<br>Book provides FREE access to online instructional videos. <br>Design Patterns In Python : ASIN B08XLJ8Z2J</p><p><a href="https://www.amazon.com/dp/B08XLJ8Z2J">Design Patterns in Python: Common GOF (Gang of Four) Design Patterns implemented in Python (Software Engineering)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b1a8d05b8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/design-patterns-in-python/mediator-design-pattern-5b1a8d05b8">Mediator Design Pattern</a> was originally published in <a href="https://medium.com/design-patterns-in-python">Design Patterns In Python</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>