8000 Hexagonal pattern: move business logic to core · armdev/java-design-patterns@ab68129 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab68129

Browse files
committed
Hexagonal pattern: move business logic to core
1 parent e57a094 commit ab68129

File tree

7 files changed

+202
-101
lines changed

7 files changed

+202
-101
lines changed

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

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

25-
import java.util.Map;
26-
2725
import com.iluwatar.hexagonal.domain.LotteryNumbers;
2826
import com.iluwatar.hexagonal.domain.LotteryTicket;
2927
import com.iluwatar.hexagonal.domain.LotteryTicketId;
3028

29+
import java.util.Map;
30+
3131
/**
3232
*
3333
* Administrator interface for lottery service.

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

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

25-
import java.util.Map;
26-
27-
import com.iluwatar.hexagonal.banking.WireTransfers;
28-
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
29-
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
30-
import com.iluwatar.hexagonal.database.LotteryTicketInMemoryRepository;
31-
import com.iluwatar.hexagonal.domain.LotteryConstants;
3225
import com.iluwatar.hexagonal.domain.LotteryNumbers;
26+
import com.iluwatar.hexagonal.domain.LotterySystem;
27+
import com.iluwatar.hexagonal.domain.LotterySystemImpl;
3328
import com.iluwatar.hexagonal.domain.LotteryTicket;
34-
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
35-
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
3629
import com.iluwatar.hexagonal.domain.LotteryTicketId;
37-
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
38-
import com.iluwatar.hexagonal.notifications.LotteryNotificationsImpl;
39-
import com.iluwatar.hexagonal.service.LotteryService;
40-
import com.iluwatar.hexagonal.service.LotteryServiceImpl;
30+
31+
import java.util.Map;
4132

4233
/**
4334
*
@@ -46,42 +37,24 @@
4637
*/
4738
public class LotteryAdministrationImpl implements LotteryAdministration {
4839

49-
private final LotteryTicketRepository repository;
50-
private final LotteryService service = new LotteryServiceImpl();
51-
private final LotteryNotifications notifications = new LotteryNotificationsImpl();
52-
private final WireTransfers bank = new WireTransfersImpl();
40+
private final LotterySystem lotterySystem;
41+
5342
public LotteryAdministrationImpl() {
54-
repository = new LotteryTicketInMemoryRepository();
43+
lotterySystem = new LotterySystemImpl();
5544
}
5645

5746
@Override
5847
public Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets() {
59-
return repository.findAll();
48+
return lotterySystem.getAllSubmittedTickets();
6049
}
6150

6251
@Override
6352
public LotteryNumbers performLottery() {
64-
LotteryNumbers numbers = LotteryNumbers.createRandom();
65-
Map<LotteryTicketId, LotteryTicket> tickets = getAllSubmittedTickets();
66-
for (LotteryTicketId id: tickets.keySet()) {
67-
LotteryTicketCheckResult result = service.checkTicketForPrize(id, numbers);
68-
if (result.getResult().equals(CheckResult.WIN_PRIZE)) {
69-
boolean transferred = bank.transferFunds(LotteryConstants.PRIZE_AMOUNT, LotteryConstants.SERVICE_BANK_ACCOUNT,
70-
tickets.get(id).getPlayerDetails().getBankAccount());
71-
if (transferred) {
72-
notifications.notifyPrize(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
73-
} else {
74-
notifications.notifyPrizeError(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
75-
}
76-
} else if (result.getResult().equals(CheckResult.NO_PRIZE)) {
77-
notifications.notifyNoWin(tickets.get(id).getPlayerDetails());
78-
}
79-
}
80-
return numbers;
53+
return lotterySystem.performLottery();
8154
}
8255

8356
@Override
8457
public void resetLottery() {
85-
repository.deleteAll();
58+
lotterySystem.resetLottery();
8659
}
8760
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.domain;
24+
25+
import java.util.Map;
26+
import java.util.Optional;
27+
28+
/**
29+
* Lottery system interface
30+
*/
31+
public interface LotterySystem {
32+
33+
/**
34+
* Get all the lottery tickets submitted for lottery
35+
*/
36+
Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets();
37+
38+
/**
39+
* Draw lottery numbers
40+
*/
41+
LotteryNumbers performLottery();
42+
43+
/**
44+
* Begin new lottery round
45+
*/
46+
void resetLottery();
47+
48+
/**
49+
* Submit lottery ticket to participate in the lottery
50+
*/
51+
Optional<LotteryTicketId> submitTicket(LotteryTicket ticket);
52+
53+
/**
54+
* Check if lottery ticket has won
55+
*/
56+
LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers);
57+
58+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.domain;
24+
25+
import com.iluwatar.hexagonal.banking.WireTransfers;
26+
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
27+
import com.iluwatar.hexagonal.database.LotteryTicketInMemoryRepository;
28+
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
29+
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
30+
import com.iluwatar.hexagonal.notifications.LotteryNotificationsImpl;
31+
32+
import java.util.Map;
33+
import java.util.Optional;
34+
35+
/**
36+
* Lottery system implementation
37+
*/
38+
public class LotterySystemImpl implements LotterySystem {
39+
40+
private final LotteryTicketRepository repository;
41+
private final LotteryNotifications notifications = new LotteryNotificationsImpl();
42+
private final WireTransfers bank = new WireTransfersImpl();
43+
44+
public LotterySystemImpl() {
45+
repository = new LotteryTicketInMemoryRepository();
46+
}
47+
48+
@Override
49+
public Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets() {
50+
return repository.findAll();
51+
}
52+
53+
@Override
54+
public LotteryNumbers performLottery() {
55+
LotteryNumbers numbers = LotteryNumbers.createRandom();
56+
Map<LotteryTicketId, LotteryTicket> tickets = getAllSubmittedTickets();
57+
for (Lottery 1241 TicketId id : tickets.keySet()) {
58+
LotteryTicketCheckResult result = checkTicketForPrize(id, numbers);
59+
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());
62+
if (transferred) {
63+
notifications.notifyPrize(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
64+
} else {
65+
notifications.notifyPrizeError(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
66+
}
67+
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
68+
notifications.notifyNoWin(tickets.get(id).getPlayerDetails());
69+
}
70+
}
71+
return numbers;
72+
}
73+
74+
@Override
75+
public void resetLottery() {
76+
repository.deleteAll();
77+
}
78+
79+
@Override
80+
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
81+
boolean result = bank.transferFunds(LotteryConstants.TICKET_PRIZE, ticket.getPlayerDetails().getBankAccount(),
82+
LotteryConstants.SERVICE_BANK_ACCOUNT);
83+
if (result == false) {
84+
notifications.notifyTicketSubmitError(ticket.getPlayerDetails());
85+
return Optional.empty();
86+
}
87+
Optional<LotteryTicketId> optional = repository.save(ticket);
88+
if (optional.isPresent()) {
89+
notifications.notifyTicketSubmitted(ticket.getPlayerDetails());
90+
}
91+
return optional;
92+
}
93+
94+
@Override
95+
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
96+
Optional<LotteryTicket> optional = repository.findById(id);
97+
if (optional.isPresent()) {
98+
if (optional.get().getNumbers().equals(winningNumbers)) {
99+
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.WIN_PRIZE, 1000);
100+
} else {
101+
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.NO_PRIZE);
102+
}
103+
} else {
104+
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.TICKET_NOT_SUBMITTED);
105+
}
106+
}
107+
}

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

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

25-
import java.util.Optional;
26-
2725
import com.iluwatar.hexagonal.domain.LotteryNumbers;
2826
import com.iluwatar.hexagonal.domain.LotteryTicket;
2927
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
3028
import com.iluwatar.hexagonal.domain.LotteryTicketId;
3129

30+
import java.util.Optional;
31+
3232
/**
3333
*
3434
* Interface for submitting and checking lottery tickets.

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

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

25-
import java.util.Optional;
26-
27-
import com.iluwatar.hexagonal.banking.WireTransfers;
28-
import com.iluwatar.hexagonal.banking.WireTransfersImpl;
29-
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
30-
import com.iluwatar.hexagonal.database.LotteryTicketInMemoryRepository;
31-
import com.iluwatar.hexagonal.domain.LotteryConstants;
3225
import com.iluwatar.hexagonal.domain.LotteryNumbers;
26+
import com.iluwatar.hexagonal.domain.LotterySystem;
27+
import com.iluwatar.hexagonal.domain.LotterySystemImpl;
3328
import com.iluwatar.hexagonal.domain.LotteryTicket;
3429
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
3530
import com.iluwatar.hexagonal.domain.LotteryTicketId;
36-
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
37-
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
38-
import com.iluwatar.hexagonal.notifications.LotteryNotificationsImpl;
31+
32+
import java.util.Optional;
3933

4034
/**
4135
*
@@ -44,45 +38,22 @@
4438
*/
4539
public class LotteryServiceImpl implements LotteryService {
4640

47-
private final LotteryTicketRepository repository;
48-
49-
private final WireTransfers bank = new WireTransfersImpl();
50-
51-
private final LotteryNotifications notifications = new LotteryNotificationsImpl();
41+
private final LotterySystem lotterySystem;
5242

5343
/**
5444
* Constructor
5545
*/
5646
public LotteryServiceImpl() {
57-
repository = new LotteryTicketInMemoryRepository();
47+
lotterySystem = new LotterySystemImpl();
5848
}
5949

6050
@Override
6151
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
62-
boolean result = bank.transferFunds(LotteryConstants.TICKET_PRIZE, ticket.getPlayerDetails().getBankAccount(),
63-
LotteryConstants.SERVICE_BANK_ACCOUNT);
64-
if (result == false) {
65-
notifications.notifyTicketSubmitError(ticket.getPlayerDetails());
66-
return Optional.empty();
67-
}
68-
Optional<LotteryTicketId> optional = repository.save(ticket);
69-
if (optional.isPresent()) {
70-
notifications.notifyTicketSubmitted(ticket.getPlayerDetails());
71-
}
72-
return optional;
52+
return lotterySystem.submitTicket(ticket);
7353
}
7454

7555
@Override
7656
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
77-
Optional<LotteryTicket> optional = repository.findById(id);
78-
if (optional.isPresent()) {
79-
if (optional.get().getNumbers().equals(winningNumbers)) {
80-
return new LotteryTicketCheckResult(CheckResult.WIN_PRIZE, 1000);
81-
} else {
82-
return new LotteryTicketCheckResult(CheckResult.NO_PRIZE);
83-
}
84-
} else {
85-
return new LotteryTicketCheckResult(CheckResult.TICKET_NOT_SUBMITTED);
86-
}
57+
return lotterySystem.checkTicketForPrize(id, winningNumbers);
8758
}
8859
}

0 commit comments

Comments
 (0)
0