Codele / Advanced programming
Build a plugin system, end to end
Put the whole track to work: a small app that knows nothing about its plugins except one promise, yet runs any of them, in order, even when one breaks.
- A plugin system lets you add features without editing the app. The app depends only on a promise: each plugin has a name and run(text). That is an interface, and calling run on any plugin is polymorphism.
- The registry is a plain list of plugin objects (composition). The app picks the switched-on ones by name, like a filter, and passes the text through them in order, like a chain of maps.
- A plugin that breaks must not break the app. A try and catch around each run keeps the failure local, so the others still work. Go has no exceptions: each run returns a result and an error, and the app checks the error.
- To add a plugin you write one class and add it to the list. In a real app the list is handed in from outside (dependency injection) or found in a folder, but the idea is the same.
Loading the playable lesson…