Core + Plug-ins
Microkernel (плагинная архитектура) отделяет минимальное ядро (core, инфраструктура) от плагинов/модулей, добавляющих функциональность.
┌───────────────┐ ┌──────────────┐ ┌──────────────┐ │ PLUGIN A │◀──────│ │ │ │ └───────────────┘ │ │ │ │ ┌───────────────┐ │ MICRO- │ │ ADAPTERS │ │ PLUGIN B │◀──────│ KERNEL │──▶│ (ports/drv) │ └───────────────┘ │ (CORE) │ │ │ ┌───────────────┐ │ │ │ │ │ PLUGIN C │◀──────│ │ │ │ └───────────────┘ └──────────────┘ └──────────────┘
from abc import ABC, abstractmethod
# Контракт плагина
class Plugin(ABC):
@abstractmethod
def name(self) -> str: ...
@abstractmethod
def initialize(self, ctx): ...
@abstractmethod
def execute(self, input): ...
# Ядро
class Microkernel:
def __init__(self):
self.plugins = {}
self.ctx = {}
def register(self, plugin: Plugin):
self.plugins[plugin.name()] = plugin
plugin.initialize(self.ctx)
def run(self, name: str, input):
return self.plugins[name].execute(input)
# Плагин
class MarkdownRender(Plugin):
def name(self): return 'markdown'
def initialize(self, ctx): ctx['md'] = { 'version': '1.0' }
def execute(self, input):
return input.replace('**','').replace('__','')
kernel = Microkernel()
kernel.register(MarkdownRender())
print(kernel.run('markdown', '**Hello**'))