Chapter 34: a contract, two implementations and two applications, built
If you know Spring or Guice you expect annotations and a container scan. EK9 has neither: a component is a contract, an Application says which implementation stands for it, and a program names the Application it runs under. This lesson builds the notifier example of chapter 34 from the contract up - two implementations, a class that asks for the contract, two applications, two programs - and runs one. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-di-notifiers.
What you'll build
The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.
#!ek9 defines module book.di.notifiers defines class OrderService notifier as Notifier! placeOrder() -> customer as String item as String notifier.notify(customer, `Your order for ${item} is confirmed`) default operator ? defines component Notifier as abstract notify() as abstract -> customer as String message as String EmailNotifier is Notifier override notify() -> customer as String message as String Stdout().println(`EMAIL to ${customer}: ${message}`) ConsoleNotifier is Notifier override notify() -> customer as String message as String Stdout().println(`CONSOLE [${customer}] ${message}`) defines application liveApp register EmailNotifier() as Notifier developmentApp register ConsoleNotifier() as Notifier defines program LiveShop() with application of liveApp orders <- OrderService() orders.placeOrder("alice@example.com", "a kettle") orders.placeOrder("bob@example.com", "two mugs") DevelopmentShop() with application of developmentApp orders <- OrderService() orders.placeOrder("alice@example.com", "a kettle") orders.placeOrder("bob@example.com", "two mugs")
Step 1: Notifier — a component
Choose Code > New Construct > Component…
Notifier is the component's name. A component is a contract in EK9, not a Spring bean: the abstract one names what any notifier can do and has nothing behind it. Choose Abstract under Extensibility. Choose Preview.
The preview shows Notifier about to be written. Choose Apply.
Select the Notifier line and, above it, type the blank line the book opens the block with. Saves when typed.
Right-click the Notifier line and choose Add Method…
notify is the method's name. It is the one thing a Notifier does: it takes a customer and a message and answers with nothing. Add the parameters customer of type String and message of type String and tick abstract - a contract with no body. Choose Apply.
Step 2: EmailNotifier — a component
Choose Code > New Construct > Component…
EmailNotifier is the component's name. It is the production implementation, and Extends is all it takes - there is no annotation and no scan. Type Notifier into Extends. Choose Preview.
The preview shows EmailNotifier about to be written - and, if Notifier owes any methods, an override stub for each. Choose Apply.
The override notify() arrived with the component. Select its stub, from require customer? down to its //AI line, and type what an email notifier does. Saves when typed.
Step 3: ConsoleNotifier — a component
Choose Code > New Construct > Component…
ConsoleNotifier is the component's name. It is the second implementation, the one a developer runs without sending real mail. Type Notifier into Extends. Choose Preview.
The preview shows ConsoleNotifier about to be written - and, if Notifier owes any methods, an override stub for each. Choose Apply.
Select the stub, from require customer? down to its //AI line, and type what the console notifier does. Saves when typed.
Step 4: OrderService — a class
Choose Code > New Construct > Class…
OrderService is the class's name. It is an ordinary class that needs a notifier. It will ask for the CONTRACT, never for EmailNotifier or ConsoleNotifier. Choose Preview.
The preview shows OrderService about to be written. Choose Apply.
Select the OrderService line and, above it, type the blank line the book opens the block with. Saves when typed.
Right-click the OrderService line and choose Add Field…
Type notifier as Notifier!. The ! after the type is the injection point: this property is not set by the constructor, the application it runs under fills it in. Choose OK.
The preview shows notifier added - and default operator ? with it, OrderService's first field. Choose Apply.
Right-click the OrderService line and choose Add Method…
placeOrder is the method's name. Add the parameters customer of type String and item of type String. Choose Apply.
Select the stub, from require customer? down to its //AI line, and type the one call placeOrder makes. It calls notify on whatever the application put there. Saves when typed.
Step 5: liveApp — an application
Choose Code > New Construct > Application…
liveApp is the application's name. It is production wiring: wherever a Notifier is asked for, the EmailNotifier answers. The Registrations table holds EmailNotifier as Notifier. Choose Preview.
The preview shows liveApp about to be written, with its register line. Choose Apply.
Select the liveApp line and, above it, type the blank line the book opens the block with. Saves when typed.
Step 6: developmentApp — an application
Choose Code > New Construct > Application…
developmentApp is the application's name. It is the same contract wired to the other implementation - a second application is all a development setup takes. The Registrations table holds ConsoleNotifier as Notifier. Choose Preview.
The preview shows developmentApp about to be written, with its register line. Choose Apply.
Step 7: LiveShop — a program
Choose Code > New Construct > Program…
LiveShop is the program's name. It runs with application of liveApp. Type liveApp into Application. Choose Preview.
The preview shows LiveShop about to be written, with application of liveApp. Choose Apply.
Select the LiveShop line and, above it, type the blank line the book opens the block with. Saves when typed.
Select the stub, from ready <- true down to its //AI line, and type the body: an OrderService and two orders. Nothing here names a notifier. Saves when typed.
Step 8: DevelopmentShop — a program
Choose Code > New Construct > Program…
DevelopmentShop is the program's name. It is the same program under the other application. Type developmentApp into Application. Choose Preview.
The preview shows DevelopmentShop about to be written, with application of developmentApp. Choose Apply.
Select the stub and type the very same body. Saves when typed.
Step 9: Run it
Choose Run > Run Program…
Choose DevelopmentShop from the list, then choose Run.
DevelopmentShop prints the two orders as CONSOLE lines - the same OrderService, a different notifier.
Choose Close.
The result
Notifiers.ek9 now reads exactly as chapter 34 of EK9 in Depth shows it. OrderService never names a notifier; the Application a program runs under decides, and swapping EmailNotifier for ConsoleNotifier was a second Application, not a change to OrderService.
Where this lesson comes from
- For AI assistants - the MCP tool
ek9_new_construct,ek9_add_field,ek9_add_method,ek9_add_application,ek9_add_program.












































