Learn How To Build Decoupled Delphi Applications Quickly With Delphi Event Bus Framework
Modularity is very important in software architecture so that applications built can be easily extendable and maintainable. Consider your building an application with multiple components in it. If we decide to remove a component and replace it with another component it should not affect the application. This can be done by decoupling software architecture. Do you want to build such decoupled applications in Delphi? This post guide you to build using Delphi Event Bus Framework.
EventBus: An event bus allows publish/subscribe-style communication between components without requiring the components to explicitly be aware of each other. Looks like Observer Pattern? No, there is a difference. In the observer pattern, the broadcast is performed directly from the observable to the observers, so they “know” each other. But when using a publish/subscribe pattern, there is a third component, called event bus, which is known by both the publisher and subscriber. It is Decoupled between Publisher and Subscriber.
Delphi Event Bus allows you to decouple components that asynchronously receive and process events and or emit events. Consumers can subscribe to this event bus and declaratively specify which events they wish to consume. The event consumer a publisher is completely decoupled. Simplifies the communication between components.
Delphi Event Bus Features:
- Easy and clean: DelphiEventBus is super easy to learn and use because it respects KISS and “Convention over configuration” design principles. By using default TEventBus instance, you can start immediately to delivery and receive events
- Designed to decouple different parts/layers of your application
- Event-Driven, Thread Safe, Unit Tested
- Attributes based API: Simply put the Subscribe attribute on your subscriber method you are able to receive a specific event
- Support different delivery modes: Specifying the TThreadMode in Subscribe attribute, you can choose to deliver the event in the Main Thread or in a Background one, regardless of where an event was posted. The EventBus will manage Thread synchronization.
How it works :
TEvent = class(TObject) // additional information here end; |
- Prepare subscribers: Declaring your subscribing method:
[Subscribe] procedure OnEvent(AEvent: TAnyTypeOfEvent); begin // manage the event end; |
- Prepare subscribers: Register your subscriber:
GlobalEventBus.RegisterSubscriber(self); |
GlobalEventBus.post(LEvent); |
Check this below video for Demonstration of How the Delphi Event Bus Framework(DEB) works.
Checkout the full source code for Delphi Event framework here.