You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c7d95c2
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: _posts/2016-09-04-build-maintainable-systems-with-hexagonal-architecture.md
+29-30Lines changed: 29 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,83 +34,82 @@ Naked Objects design pattern is considered an implementation of Hexagonal Archit
34
34
35
35
Next we will demonstrate Hexagonal Architecture by building a lottery system. The lottery system will provide two primary ports: One for the users to submit lottery tickets and another for system administrators to perform the draw.
36
36
37
-
Secondary ports consist of lottery ticket database, banking for wire transfers and notifications for pushing the lottery results to the players. The resulting hexagon of the system can be seen in the following diagram.
37
+
The secondary ports consist of lottery ticket database, banking for wire transfers and event log for handling and storing lottery events. The resulting hexagon of the system can be seen in the following diagram.
We start the implementation from the system core. First we need to identify the core concepts of the lottery system. Probably the most important one is the lottery ticket. In lottery ticket you are supposed to pick the numbers and write your contact details to receive email notifications and possibly the prize money directly to your bank account. This leads us to write the following classes.
43
+
We start the implementation from the system core. First we need to identify the core concepts of the lottery system. Probably the most important one is the lottery ticket. In lottery ticket you are supposed to pick the numbers and provide your contact details. This leads us to write the following classes.
`LotteryTicket` contains `LotteryNumbers` and `PlayerDetails`. `LotteryNumbers` contains means to hold given numbers or generate random numbers and test the numbers for equality with another `LotteryNumbers` instance. [PlayerDetails](https://github.com/iluwatar/java-design-patterns/blob/master/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/PlayerDetails.java) is a simple value object containing player's email address, bank account number and phone number.
49
49
50
50
## The core business logic
51
51
52
-
Now that we have the nouns presenting our core concepts we need to implement the core business logic that defines how the system works. In `LotterySystem` interface we write the methods that are needed by the lottery players and system administrators.
52
+
Now that we have the nouns presenting our core concepts we need to implement the core business logic that defines how the system works. In `LotteryAdministration` and `LotteryService` classes we write the methods that are needed by the lottery players and system administrators.
The lottery players use `submitTicket()` to submit tickets for lottery round. After the draw has been performed `checkTicketForPrize()` tells the players whether they have won.
For administrators `LotterySystem` has `resetLottery()` method for starting a new lottery round. At this stage the players submit their lottery tickets into the database and when the time is due the administration calls `performLottery()` to draw the winning numbers and check each of the tickets for winnings.
The lottery players use `submitTicket()` to submit tickets for lottery round. After the draw has been performed `checkTicketForPrize()` tells the players whether they have won.
61
+
62
+
`LotteryAdministration` and `LotteryService` have dependencies to lottery ticket database, banking and event log ports. We use [Guice](https://github.com/google/guice) dependency injection framework to provide the correct implementation classes for each purpose.
61
63
62
-
`LotterySystemImpl` has dependencies to lottery ticket database, banking and notifications ports. This implementation hardwires the adapters it uses. In a more sophisticated application we would use dependency injection to determine the implementation classes. The core logic is tested in [LotteryTest](https://github.com/iluwatar/java-design-patterns/blob/master/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java).
64
+
The core logic is tested in [LotteryTest](https://github.com/iluwatar/java-design-patterns/blob/master/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java).
63
65
64
66
## Primary port for the players
65
67
66
-
Now that the core implementation is ready we need to define the primary port for the players. What we offer is basically a slice from `LotterySystem`.
68
+
Now that the core implementation is ready we need to define the primary port for the players. We introduce `ConsoleLottery` class to provide command line interface that allows players to interact with the lottery system.
The implementation here is trivial since we can just delegate the requests to `LotterySystem`.
80
+
The interface's commands allow us to view submitted tickets, perform the lottery draw and reset the lottery ticket database.
81
81
82
82
## Secondary port for banking
83
83
84
84
Next we implement the secondary ports and adapters. The first one is the banking support that enables us to manipulate bank account funds. To explain the concept, the player can write his bank account number on the lottery ticket and in case the ticket wins the lottery system automatically wire transfers the funds.
The banking port has two adapters for different purposes. The first one `InMemoryBank` is a simple `HashMap` based implementation for testing. The lottery service's bank account is statically initialized to contain enough funds to pay the prizes in case some of the lottery tickets win.
88
89
89
-
The bank back office is a simple `HashMap`based implementation. The lottery service's bank account is statically initialized to contain enough funds to pay the prizes in case some of the lottery tickets win.
90
+
The other adapter `MongoBank` is based on Mongo and is intended for production use. Running either one of the command line interfaces use this adapter.
90
91
91
-
## Secondary port for notifications
92
+
## Secondary port for event log
92
93
93
-
Another secondary port is the notification service. If the player has written his email address in the lottery ticket the system automatically sends notifications of the lottery system events, most importantly whether the ticket has won or not.
94
+
Another secondary port is the lottery event log. Events are sent as the players submit their lottery tickets and when the draw is performed.
The methods in `LotteryNotificationsImpl` adapter are simple `System.out` printers so the implementation is trivial to understand.
98
+
We have two adapters for this port: The first one `StdOutEventLog` is for testing and simply sends the events to standard output. The second `MongoEventLog` is more sophisticated, has persistent storage and is based on Mongo.
99
99
100
100
## Secondary port for database
101
101
102
102
The last secondary port is the database. It contains methods for storing and retrieving lottery tickets.
The `LotteryTicketInMemoryRepository` is a mock database holding its contents in memory only. We use `Optional` to indicate whether the operation was successful or not.
106
+
The port has two adapters. The `LotteryTicketInMemoryRepository` is a mock database holding its contents in memory only and is meant for testing. The `MongoTicketRepository` is used for production runs and provides persistent storage over application restarts.
108
107
109
108
## Lottery application
110
109
111
-
With all the pieces in place we create a command line application to drive the lottery system. The test application initializes the lottery system using the admin interface and starts collecting lottery tickets from the players. Once all the lottery tickets have been submitted the lottery number draw is performed and all the submitted tickets are checked for wins.
110
+
With all the pieces in place we create a command line application to drive the lottery system. The test application initializes the lottery system using the adminstration methods and starts collecting lottery tickets from the players. Once all the lottery tickets have been submitted the lottery number draw is performed and all the submitted tickets are checked for wins.
0 commit comments