<?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 Maxwell Mapako on Medium]]></title>
        <description><![CDATA[Stories by Maxwell Mapako on Medium]]></description>
        <link>https://medium.com/@mwax911?source=rss-aa19c00cdf3f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ywH3ox_8uStzrbN1.</url>
            <title>Stories by Maxwell Mapako on Medium</title>
            <link>https://medium.com/@mwax911?source=rss-aa19c00cdf3f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:57:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mwax911/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[Building a plugin architecture with Python]]></title>
            <link>https://mwax911.medium.com/building-a-plugin-architecture-with-python-7b4ab39ad4fc?source=rss-aa19c00cdf3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b4ab39ad4fc</guid>
            <category><![CDATA[plugins]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Maxwell Mapako]]></dc:creator>
            <pubDate>Sat, 24 Apr 2021 21:36:57 GMT</pubDate>
            <atom:updated>2021-04-27T19:58:43.792Z</atom:updated>
            <content:encoded><![CDATA[<p><em>It’s no secret that one of the greatest software design principles when extending functionality is to consider using plugins (where possible)*</em></p><p>I will keep this post short and avoid repetition, if you don’t know what a plugin system is then now would be a good time to find out from <a href="https://openclassrooms.com/en/courses/6397806-design-your-software-architecture-using-industry-standard-patterns/6896171-plug-in-architecture">here</a>. Now that is out of the way, let us create a small use-case that we need a solution for.</p><h3>Problem statement</h3><p>Given a broad category of devices that share similar functionality and a communication interface with indecent protocols, in this case let’s call them inverters (for a solar system). These inverters can provide us with power readings for input (<strong>solar</strong> and or <strong>battery</strong>), output (to mains/load) and device information/status e.t.c that we can access though some text based. Say for example to change from hybrid mode (<strong>solar</strong> and <strong>grid</strong>) to just offline mode (<strong>solar</strong> and <strong>battery</strong> only).</p><p>At this point we know that a monolithic application would be either too large to accommodate all the possible inverter types and protocols, so our best bet would be to create a core application that only knows about the high level features (read device info/status, read power data and send commands to change some settings). We can achieve this by creating small units to handle the complexities of the communication protocols (taking in the core application commands and transforming them into commands the inverter can understand). This approach would allow us to ship a bare application and apply/download a plugin for a specific inverter/s which we have.</p><h3>Fundamental plugin concepts</h3><p>A common plugin system typically needs to define some form of contract/s that the plugins can extend and the core application can use as an abstraction layer for communication, this could be in the form of a package with some abstract classes. Typically the application would using the following steps/phases to interact with the plugins:</p><h4>Discovery</h4><p>This is the mechanism by which a running application can find out which plugins it has at its disposal. To “discover” a plugin, one has to look in certain places, and also know what to look for.</p><h4>Registration</h4><p>This is the mechanism by which a plugin tells an application — “I’m here, ready to do work”. Admittedly, registration usually has a large overlap with discovery.</p><h4>Application Hooks</h4><p>Hooks are also called “mount points” or “extension points”. These are the places where the plugin can “attach” itself to the application, signalling that it wants to know about certain events and participate in the flow. The exact nature of hooks is very much dependent on the application.</p><h4>Exposing application API to plugins</h4><p>To make plugins truly powerful and versatile, the application needs to give them access to itself, by means of exposing an API the plugins can use.</p><h3>Code examples</h3><blockquote>I will keep this section short so for the sake of time saving and simplicity, please see the full sample-project on github</blockquote><p><a href="https://github.com/wax911/plugin-architecture">wax911/plugin-architecture</a></p><p>Let’s start off by defining some sort of configuration file, which will tell our application which plugins to load and where to get them: (<em>settings/configuration.yaml</em>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*vyht-GDlQ4r7FxOJIag5GQ.png" /></figure><h4>Discovery</h4><p>Let us create a few contracts that all plugins will implement/extend, one contract will be used for registering a plugin when it is loaded/registered (will go into more detail later) and the other defines the behaviour of the plugin (what it can do and the information we can expect to get):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*_WIXWB6iQKUFioGCtkguQA.png" /><figcaption>The references models can be found <a href="https://github.com/wax911/plugin-architecture/blob/master/model/models.py">here</a>, these are just data classes for demonstration purposes</figcaption></figure><blockquote><strong>N.B. </strong><a href="http://github.com/wax911/plugin-architecture/blob/master/engine/engine_contract.py#L7">IPluginRegistry</a> extends <a href="https://realpython.com/python-metaclasses/">type</a> because we need some sort of mechanism to register plugins which we will not instantiating immediately after the discovery phase, think of this as just keeping information on what class type we’re dealing with (we would eventually call plugin_registries[index]() to instantiate the class)</blockquote><p>If we look at the <em>init(constructor)</em> block inside our <a href="http://github.com/wax911/plugin-architecture/blob/master/engine/engine_contract.py#L7"><em>IPluginRegistry</em></a><em> we can see that it </em>contains something interesting, it checks if the loaded class (this can be considered as part of the hook phase) is not the plugin contract (<a href="https://github.com/wax911/plugin-architecture/blob/master/engine/engine_contract.py#L16">PluginCore</a>) not to be confused with the <em>instanceof </em>operator/function.</p><h4>Registration</h4><p>In the sample project all our plugins will be placed in a <em>plugins</em> directory, this is to simulate a case where a plugins are downloaded and kept.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/284/1*zwubz3wAYC68snv9Cxf9Vg.png" /><figcaption>Sample plugin directory structure which can be found <a href="https://github.com/wax911/plugin-architecture/tree/master/plugins">here</a></figcaption></figure><blockquote>Each of the plugins will have a configuration file called plugin.yaml that will contain information regarding which “main” file to load, dependencies which we will install automatically (yes, I know this would be a big security risk in production if plugins are not audited against malicious intent)</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*F6YDjU8P93YkD70gKecfbw.png" /><figcaption><em>plugin.yaml</em> configuration file for the sample plugin</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*RfK7qSzLNfY8jymbQg233Q.png" /><figcaption><strong>sample.py </strong>the main sample plugin file which can be found <a href="https://github.com/wax911/plugin-architecture/blob/master/plugins/sample-plugin/sample.py">here</a></figcaption></figure><p>Our sample plugins extends <a href="http://github.com/wax911/plugin-architecture/blob/master/engine/engine_contract.py#L16"><em>PluginCore</em></a> and expects a logger as a dependency from the application (<em>This is to simulate the main application providing dependencies to its plugins, this could be in the form of a configured network client, a database wrapper to provide read only functionality or anything really</em>) and has an invoke method which is called by the core application to run commands on the destination device.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*spmyORehPn66bKxalD0vsg.png" /><figcaption><strong>PluginUseCase</strong> discover and load/hook plugins</figcaption></figure><blockquote>The above code snippet is from the <a href="https://github.com/wax911/plugin-architecture/blob/09fd2c761173d8510667bd518e6c2f26c2ad35eb/usecase/interactors.py#L11">PluginUseCase</a> class where the application will search the plugin directory for a configuration file (<em>plugin.yaml</em>) which is handled in <a href="https://github.com/wax911/plugin-architecture/blob/09fd2c761173d8510667bd518e6c2f26c2ad35eb/usecase/utilities.py#L17">PluginUtility</a> (for all the installation and dependency resolution details), and finally loaded/registered after all checks are done.</blockquote><p>You can read more about what <em>import_module </em>is<em> </em>from <a href="https://docs.python.org/3/library/importlib.html#functions">here</a></p><h4>Application hooks and exposing the API</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*_q2p7Sq1RJnCe9dLgxzA9w.png" /><figcaption>Plugins are loaded through <a href="https://github.com/wax911/plugin-architecture/blob/09fd2c761173d8510667bd518e6c2f26c2ad35eb/usecase/interactors.py#L38"><strong><em>__search_for_plugins</em></strong></a><strong><em> </em></strong><em>at this point</em></figcaption></figure><p>After the plugins are loaded we clean up the global registry (IPluginRegistry) since all loaded plugins will be stored in the <a href="https://github.com/wax911/plugin-architecture/blob/09fd2c761173d8510667bd518e6c2f26c2ad35eb/usecase/interactors.py#L11">PluginUseCase</a> scope, and we’re ready to use our plugins through the following functions which our core application will call on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*CpTzr47H1OglOEGwQQAa_g.png" /><figcaption>Plugin instance creation and invocation</figcaption></figure><blockquote>Arguably one could omit the <strong><em>register_plugin</em></strong> and <strong><em>hook_plugin</em></strong> functions and directly do this inline in the the calling core application module, but for the sake of demonstrating the phases separately we’re doing it this way</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*xE1M-2ZSYd3RRXWW6fyjPA.png" /><figcaption>Application core module which can be found <a href="https://github.com/wax911/plugin-architecture/blob/master/engine/engine_core.py">here</a></figcaption></figure><p>In this example we’re just calling calling all the loaded plugins, depending on our use-case we may just want one plugin and that’s easily done by simply specifying a single plugin in our <em>configuration.yaml</em> file.</p><p>Running the project and we’d get the following output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t95bxrPdbnySH2AV24a9ow.png" /></figure><p>Thank you for reading and let me know if you have any questions :)</p><blockquote>Credits for inspiration: <a href="https://eli.thegreenplace.net/2012/08/07/fundamental-concepts-of-plugin-infrastructures">https://eli.thegreenplace.net/2012/08/07/fundamental-concepts-of-plugin-infrastructures</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b4ab39ad4fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Navigating through runtime only modules and thinking outside of the box with androidx.startup]]></title>
            <link>https://mwax911.medium.com/navigating-through-runtime-only-modules-and-thinking-outside-of-the-box-with-androidx-startup-60accc8fab8e?source=rss-aa19c00cdf3f------2</link>
            <guid isPermaLink="false">https://medium.com/p/60accc8fab8e</guid>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[koin]]></category>
            <dc:creator><![CDATA[Maxwell Mapako]]></dc:creator>
            <pubDate>Fri, 20 Nov 2020 12:30:45 GMT</pubDate>
            <atom:updated>2020-11-20T15:32:51.576Z</atom:updated>
            <content:encoded><![CDATA[<h3>Navigating through runtime only modules and thinking outside of the box with androidx.startup and koin (decoupled multi-module projects)</h3><blockquote><a href="https://maven.google.com/web/index.html?q=androidx.star#androidx.startup:startup-runtime">androix.startup</a> is a new addition to the <a href="https://developer.android.com/jetpack">Jetpack suite</a> of libraries. Read more about it <a href="https://proandroiddev.com/androidx-app-startup-698855342f80">here</a></blockquote><blockquote><a href="https://medium.com/koin-developers/unboxing-koin-2-1-7f1133ebb790">Koin</a> is a pragmatic lightweight dependency injection framework for Kotlin developers. Written in pure Kotlin using functional resolution only: no proxy, no code generation, no reflection!</blockquote><h3>Introduction</h3><p>I’m sure you’ve read a lot about writing good code using best practises like <strong>first five object-oriented design(OOD) </strong>by Uncle Bob, if you haven’t now would be a good time to quickly touch up on the top <a href="https://www.digitalocean.com/community/conceptual_articles/s-o-l-i-d-the-first-five-principles-of-object-oriented-design"><strong>S.O.L.I.D: The First 5 Principles of Object Oriented Design</strong></a><strong> </strong>as we will make some references to some of these principles.</p><h3>Problem Statement</h3><p>Let’s assume you’re working on a project with several modules, either using <a href="https://medium.com/androiddevelopers/now-in-android-12-f221038fa0d5"><strong>dynamic feature modules</strong></a> or a <a href="https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph"><strong>runtimeOnly</strong></a>. In both cases the features modules code will not be made available at compile time but instead at runtime (<em>this just means we can’t reference any code defined in the our dynamic or runtime only modules</em>). Either way at some point you have to solve a certain number of challenges:</p><ul><li>Accessing components from your feature modules (<em>dynamic or runtime only</em>)</li><li>Initialization of sub-components of your modules without explicitly managing this yourself</li></ul><h3>Sample Structure</h3><p>There are a few ways to expose or interact with modules you may have at runtime, let’s briefly discuss each of them using a sample project with the following structure:</p><p><a href="https://github.com/AniTrend/trakt-trend">AniTrend/trakt-trend</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*vDnGXlO_25rbNWfyZlvOfA.png" /></figure><p>Let’s set up a few case scenarios which we need solutions to, at the very least we want to be able to get a static fragment class definition and a bunch of intents to launch activities or services .e.t.c</p><p>We’ll only focus on the following modules:</p><ul><li><em>discover-movie </em>(feature module that is only available at run time)</li><li><em>discover-series </em>(feature module that is only available at run time)</li><li><em>navigation</em> (module for handling how to navigate between modules)</li></ul><h3>Reflection</h3><p>Reflection is commonly used by programs which require the ability to examine or modify the runtime behaviour of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible. <a href="https://docs.oracle.com/javase/tutorial/reflect/">Source</a></p><p>While reflection is powerful it does have some drawbacks and limitations which you can read more about at the source above and <a href="https://stackoverflow.com/questions/22132804/understanding-java-reflection-drawbacks#:~:text=Is%20this%20drawback%20specific%20to%20Applets%20only%3F&amp;text=Since%20reflection%20allows%20code%20to,dysfunctional%20and%20may%20destroy%20portability">here</a></p><h4><strong>Example:</strong></h4><p>Let’s define some extensions to manage some class loading stuff (full code extensions code can be found <a href="https://github.com/AniTrend/trakt-trend/blob/develop/navigation/src/main/kotlin/io/wax911/trakt/navigation/extensions/NavigationExtensions.kt">here</a>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*q5H3GHPIABHuPUuKpzE4zA.png" /></figure><p>Now to define our components targets (full code extensions code can be found <a href="https://github.com/AniTrend/trakt-trend/blob/develop/navigation/src/main/kotlin/io/wax911/trakt/navigation/NavigationTargets.kt">here</a>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*NNQ8IOuB1powaNlg3Uk6rg.png" /></figure><blockquote>Each of our target have to know the fully qualified package name of the component we need to access, as you can imagine if we change class names, or minify then we might end up mismatching class package names.</blockquote><p>Accessing our components:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*nO44N25PE6-Yd44m6PQDlQ.png" /><figcaption>Model to hold fragment definition and params</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*9mdBYq-D4uMbDl9q7L8tJw.png" /><figcaption>Accessing navigation targets</figcaption></figure><blockquote>In the sample above we’re calling NavigationTargets and getting specific components specifically in this case only fragments in the form of Class&lt;out Fragment&gt; because I’m using the default fragment factory as the constructors for each of the feature fragments are empty</blockquote><h3>Service Loader</h3><p>A <em>service</em> is a well-known set of interfaces and (usually abstract) classes. A <em>service provider</em> is a specific implementation of a service. The classes in a provider typically implement the interfaces and subclass the classes defined in the service itself. Service providers can be installed in an implementation of the Java platform in the form of extensions, that is, jar files placed into any of the usual extension directories. Providers can also be made available by adding them to the application’s class path or by some other platform-specific means. <a href="https://developer.android.com/reference/java/util/ServiceLoader">Source</a></p><p>Typically using a service loader in android as is introduces some performance penalties unless you use R8 shrinker to optimize/rewrite the implementation to avoid the performance penalties. Since service loader is no longer supported by R8 for dynamic feature modules we won’t be covering this topic</p><h3>Dependency Injection</h3><p>Dependency injection is a programming technique that makes a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation. This helps you to follow SOLID’s <a href="https://stackify.com/dependency-inversion-principle/">dependency inversion</a> and <a href="https://stackify.com/solid-design-principles/">single responsibility principles</a>. <a href="https://stackify.com/dependency-injection/">Source</a></p><h4><strong>Example:</strong></h4><p>Applying the Interface Segregation rule we can define an interface in our <em>navigation</em><strong> </strong>module that we’ll use to expose fragments or intents similar to our <em>Reflection</em> approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*GZF6MFm609Wop2E66Ow47Q.png" /><figcaption>Notice that we’re not defining more than we need in this contract, this is because we’d rather define what we need and extend this interface if we need to access more modules. <a href="https://github.com/AniTrend/trakt-trend/blob/refactor/dependency-injection-navigation/navigation/src/main/kotlin/io/wax911/trakt/navigation/feature/INavigationFeature.kt">Full code</a></figcaption></figure><p>Define navigation targets that we’ll use in various parts of the system:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*yVl6eR55w7t8gNpzMz8E2Q.png" /><figcaption>Contract for navigation targets, we don’t really need to define <strong>invoke</strong> at this point, it could be an extension function or whatever you want to use. <a href="https://github.com/AniTrend/trakt-trend/blob/refactor/dependency-injection-navigation/navigation/src/main/kotlin/io/wax911/trakt/navigation/contract/NavigationComponent.kt">Full code</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*MgsQsXar4I8bbUNumxrTsA.png" /><figcaption>Take not that each of the defined navigation targets here extend the <strong>INavigationFeature</strong>. <a href="https://github.com/AniTrend/trakt-trend/blob/refactor/dependency-injection-navigation/navigation/src/main/kotlin/io/wax911/trakt/navigation/NavigationTargets.kt">Full code</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*OFn2P7ThKKW-p07lTYjeqQ.png" /><figcaption>An extension that we’ll showcase later to get fragment definitions</figcaption></figure><p>Let’s hook up the <em>Provider</em> for each of our navigation targets in the appropriate feature module and register them in our dependency injector.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*lGXG7mWq-21x5Fs06Iyr4w.png" /><figcaption>discover-show feature implements contract from navigation module and provides details of implementation. <a href="https://github.com/AniTrend/trakt-trend/blob/refactor/dependency-injection-navigation/discover-show/src/main/kotlin/io/wax911/trakt/discover/show/ShowFeature.kt">Full code</a></figcaption></figure><p>Let’s continue to define our DI modules</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*QR8BSs1VNMfPU1qcenL_EA.png" /><figcaption>Note how we’re setting our <strong>featureModule</strong> is set to provide a type of <strong>NavShow.Provider</strong>. <a href="https://github.com/AniTrend/trakt-trend/blob/refactor/dependency-injection-navigation/discover-show/src/main/kotlin/io/wax911/trakt/discover/show/di/Modules.kt">Full code</a></figcaption></figure><blockquote><em>DynamicFeatureModuleHelper is just a helper class that implements the detail of how how the koin should load or unload modules</em></blockquote><p>Registering the specific module to the application global dependency injection registry, since this is a run time only module we can leverage <em>androidx.startup</em> to load this features modules dependencies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*e6xGpsUpEPyLYas7dKs05g.png" /><figcaption>Feature initializer to initialize our declared DI modules 💁 <a href="https://github.com/AniTrend/trakt-trend/blob/refactor/dependency-injection-navigation/discover-show/src/main/kotlin/io/wax911/trakt/discover/show/initializer/FeatureInitializer.kt">Full code</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*_aT502qVAJiba9Aa8cehBg.png" /><figcaption>Manifest for FeatureInitializer, this exists in our feature module manifest. <a href="https://github.com/AniTrend/trakt-trend/blob/refactor/dependency-injection-navigation/discover-show/src/main/AndroidManifest.xml">Full code</a></figcaption></figure><p>Finally accessing a component from anywhere within the application 😋</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*83Ct5gNbg-8Dmy2YGhM1AA.png" /><figcaption><a href="https://github.com/AniTrend/trakt-trend/blob/refactor/dependency-injection-navigation/app/src/main/java/io/wax911/trakt/view/MainScreen.kt">Full code</a></figcaption></figure><h3>Conclusion</h3><p>What we’ve built here is something that enforces good separation of concerns and low cohesion, by not only having separate feature modules but by also exposing contracts of components.</p><p>I believe there’s nothing new about any of the approaches demonstrated here, but simply an alternative look approach specifically tailored to Koin in the DI example.</p><p>The concept should be the same regardless of whichever DI engine you use, but obviously constraints may exist with your selected library of choice which will influence your design. I hope this was informative and will help you in future, feel free to leave some suggestions, thoughts or criticism 😃</p><h4>Extras</h4><p>I personally have not jumped onto the dynamic feature delivery band wagon yet, so I’m not sure if <em>androidx.startup</em> would automatically invoke the the provider intializer for a module that is installed at a later point in the application lifecycle</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60accc8fab8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>