8000 Hexagonal pattern: Use Guice dependency injection · armdev/java-design-patterns@1b10ddb · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b10ddb

Browse files
committed
Hexagonal pattern: Use Guice dependency injection
1 parent 348e577 commit 1b10ddb

File tree

7 files changed

+152
-25
lines changed

7 files changed

+152
-25
lines changed

hexagonal/src/main/java/com/iluwatar/hexagonal/App.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
import java.util.List;
2727
import java.util.Random;
2828

29+
import com.google.inject.Guice;
30+
import com.google.inject.Injector;
2931
import com.iluwatar.hexagonal.administration.LotteryAdministration;
30-
import com.iluwatar.hexagonal.administration.LotteryAdministrationImpl;
3132
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
3233
import com.iluwatar.hexagonal.domain.LotteryConstants;
3334
import com.iluwatar.hexagonal.domain.LotteryNumbers;
3435
import com.iluwatar.hexagonal.domain.LotteryTicket;
3536
import com.iluwatar.hexagonal.domain.PlayerDetails;
3637
import com.iluwatar.hexagonal.service.LotteryService;
37-
import com.iluwatar.hexagonal.service.LotteryServiceImpl;
3838

3939
/**
4040
*
@@ -124,12 +124,15 @@ public class App {
124124
* Program entry point
125125
*/
126126
public static void main(String[] args) {
127+
128+
Injector injector = Guice.createInjector(new LotteryModule());
129+
127130
// start new lottery round
128-
LotteryAdministration administartion = new LotteryAdministrationImpl();
131+
LotteryAdministration administartion = injector.getInstance(LotteryAdministration.class);
129132
administartion.resetLottery();
130133

131134
// submit some lottery tickets
132-
LotteryServiceImpl service = new LotteryServiceImpl();
135+
LotteryService service = injector.getInstance(LotteryService.class);
133136
submitTickets(service, 20);
134137

135138
// perform lottery
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal;
24+
25+
import com.google.inject.AbstractModule;
26+
import com.iluwatar.hexagonal.administration.LotteryAdministration;
27+
import com.iluwatar.hexagonal.administration.LotteryAdministrationImpl;
28+
import com.iluwatar.hexagonal.banking.WireTransfers;
29+
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
30+
import com.iluwatar.hexagonal.database.LotteryTicketInMemoryRepository;
31+
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
32+
import com.iluwatar.hexagonal.domain.LotterySystem;
33+
import com.iluwatar.hexagonal.domain.LotterySystemImpl;
34+
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
35+
import com.iluwatar.hexagonal.notifications.LotteryNotificationsImpl;
36+
import com.iluwatar.hexagonal.service.LotteryService;
37+
import com.iluwatar.hexagonal.service.LotteryServiceImpl;
38+
39+
/**
40+
* Guice module for binding production dependencies
41+
*/
42+
public class LotteryModule extends AbstractModule {
43+
@Override
44+
protected void configure() {
45+
bind(LotteryTicketRepository.class).to(LotteryTicketInMemoryRepository.class);
46+
bind(LotterySystem.class).to(LotterySystemImpl.class);
47+
bind(LotteryNotifications.class).to(LotteryNotificationsImpl.class);
48+
bind(WireTransfers.class).to(WireTransfersImpl.class);
49+
bind(LotteryAdministration.class).to(LotteryAdministrationImpl.class);
50+
bind(LotteryService.class).to(LotteryServiceImpl.class);
51+
}
52+
}

hexagonal/src/main/java/com/iluwatar/hexagonal/administration/LotteryAdministrationImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323
package com.iluwatar.hexagonal.administration;
2424

25+
import com.google.inject.Inject;
2526
import com.iluwatar.hexagonal.domain.LotteryNumbers;
2627
import com.iluwatar.hexagonal.domain.LotterySystem;
27-
import com.iluwatar.hexagonal.domain.LotterySystemImpl;
2828
import com.iluwatar.hexagonal.domain.LotteryTicket;
2929
import com.iluwatar.hexagonal.domain.LotteryTicketId;
3030

@@ -39,8 +39,9 @@ public class LotteryAdministrationImpl implements LotteryAdministration {
3939

4040
private final LotterySystem lotterySystem;
4141

42-
public LotteryAdministrationImpl() {
43-
lotterySystem = new LotterySystemImpl();
42+
@Inject
43+
public LotteryAdministrationImpl(LotterySystem lotterySystem) {
44+
this.lotterySystem = lotterySystem;
4445
}
4546

4647
@Override

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystemImpl.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
*/
2323
package com.iluwatar.hexagonal.domain;
2424

25+
import com.google.inject.Inject;
2526
import com.iluwatar.hexagonal.banking.WireTransfers;
26-
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
27-
import com.iluwatar.hexagonal.database.LotteryTicketInMemoryRepository;
2827
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
2928
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
30-
import com.iluwatar.hexagonal.notifications.LotteryNotificationsImpl;
3129

3230
import java.util.Map;
3331
import java.util.Optional;
@@ -38,11 +36,18 @@
3836
public class LotterySystemImpl implements LotterySystem {
3937

4038
private final LotteryTicketRepository repository;
41-
private final LotteryNotifications notifications = new LotteryNotificationsImpl();
42-
private final WireTransfers bank = new WireTransfersImpl();
39+
private final LotteryNotifications notifications;
40+
private final WireTransfers wireTransfers;
4341

44-
public LotterySystemImpl() {
45-
repository = new LotteryTicketInMemoryRepository();
42+
/**
43+
* Constructor
44+
*/
45+
@Inject
46+
public LotterySystemImpl(LotteryTicketRepository repository, LotteryNotifications notifications,
47+
WireTransfers wireTransfers) {
48+
this.repository = repository;
49+
this.notifications = notifications;
50+
this.wireTransfers = wireTransfers;
4651
}
4752

4853
@Override
@@ -57,8 +62,8 @@ public LotteryNumbers performLottery() {
5762
for (LotteryTicketId id : tickets.keySet()) {
5863
LotteryTicketCheckResult result = checkTicketForPrize(id, numbers);
5964
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
60-
boolean transferred = bank.transferFunds(LotteryConstants.PRIZE_AMOUNT, LotteryConstants.SERVICE_BANK_ACCOUNT,
61-
tickets.get(id).getPlayerDetails().getBankAccount());
65+
boolean transferred = wireTransfers.transferFunds(LotteryConstants.PRIZE_AMOUNT,
66+
LotteryConstants.SERVICE_BANK_ACCOUNT, tickets.get(id).getPlayerDetails().getBankAccount());
6267
if (transferred) {
6368
notifications.notifyPrize(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
6469
} else {
@@ -78,8 +83,8 @@ public void resetLottery() {
7883

7984
@Override
8085
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
81-
boolean result = bank.transferFunds(LotteryConstants.TICKET_PRIZE, ticket.getPlayerDetails().getBankAccount(),
82-
LotteryConstants.SERVICE_BANK_ACCOUNT);
86+
boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE,
87+
ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT);
8388
if (result == false) {
8489
notifications.notifyTicketSubmitError(ticket.getPlayerDetails());
8590
return Optional.empty();

hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryServiceImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323
package com.iluwatar.hexagonal.service;
2424

25+
import com.google.inject.Inject;
2526
import com.iluwatar.hexagonal.domain.LotteryNumbers;
2627
import com.iluwatar.hexagonal.domain.LotterySystem;
27-
import com.iluwatar.hexagonal.domain.LotterySystemImpl;
2828
import com.iluwatar.hexagonal.domain.LotteryTicket;
2929
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
3030
import com.iluwatar.hexagonal.domain.LotteryTicketId;
@@ -43,8 +43,9 @@ public class LotteryServiceImpl implements LotteryService {
4343
/**
4444
* Constructor
4545
*/
46-
public LotteryServiceImpl() {
47-
lotterySystem = new LotterySystemImpl();
46+
@Inject
47+
public LotteryServiceImpl(LotterySystem lotterySystem) {
48+
this.lotterySystem = lotterySystem;
4849
}
4950

5051
@Override
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal;
24+
25+
import com.google.inject.AbstractModule;
26+
import com.iluwatar.hexagonal.administration.LotteryAdministration;
27+
import com.iluwatar.hexagonal.administration.LotteryAdministrationImpl;
28+
import com.iluwatar.hexagonal.banking.WireTransfers;
29+
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
30+
import com.iluwatar.hexagonal.database.LotteryTicketInMemoryRepository;
31+
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
32+
import com.iluwatar.hexagonal.domain.LotterySystem;
33+
import com.iluwatar.hexagonal.domain.LotterySystemImpl;
34+
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
35+
import com.iluwatar.hexagonal.notifications.LotteryNotificationsImpl;
36+
import com.iluwatar.hexagonal.service.LotteryService;
37+
import com.iluwatar.hexagonal.service.LotteryServiceImpl;
38+
39+
/**
40+
* Guice module for testing dependencies
41+
*/
42+
public class LotteryTestingModule extends AbstractModule {
43+
@Override
44+
protected void configure() {
45+
bind(LotteryTicketRepository.class).to(LotteryTicketInMemoryRepository.class);
46+
bind(LotterySystem.class).to(LotterySystemImpl.class);
47+
bind(LotteryNotifications.class).to(LotteryNotificationsImpl.class);
48+
bind(WireTransfers.class).to(WireTransfersImpl.class);
49+
bind(LotteryAdministration.class).to(LotteryAdministrationImpl.class);
50+
bind(LotteryService.class).to(LotteryServiceImpl.class);
51+
}
52+
}

hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
import java.util.Map;
3131
import java.util.Optional;
3232

33+
import com.google.inject.Guice;
34+
import com.google.inject.Inject;
35+
import com.google.inject.Injector;
36+
import com.iluwatar.hexagonal.LotteryModule;
37+
import com.iluwatar.hexagonal.LotteryTestingModule;
3338
import com.iluwatar.hexagonal.domain.*;
3439
import org.junit.Before;
3540
import org.junit.Test;
@@ -48,11 +53,19 @@
4853
*/
4954
public class LotteryTest {
5055

51-
private final LotterySystem lotterySystem = new LotterySystemImpl();
52-
private final WireTransfers wireTransfers = new WireTransfersImpl();
53-
56+
private Injector injector;
57+
@Inject
58+
private LotterySystem lotterySystem;
59+
@Inject
60+
private WireTransfers wireTransfers;
61+
62+
public LotteryTest() {
63+
this.injector = Guice.createInjector(new LotteryTestingModule());
64+
}
65+
5466
@Before
55-
public void clear() {
67+
public void setup() {
68+
injector.injectMembers(this);
5669
// add funds to the test player's bank account
5770
wireTransfers.setFunds("123-12312", 100);
5871
}

0 commit comments

Comments
 (0)
0