Hexagonal pattern: Remove interfaces with only one implementation

This commit is contained in:
Ilkka Seppälä 2016-09-10 07:14:24 +03:00
parent 0f2807b9cf
commit adc6019c7e
9 changed files with 115 additions and 274 deletions

View File

@ -22,7 +22,7 @@
<operations public="true" package="true" protected="true" private="true" static="true"/> <operations public="true" package="true" protected="true" private="true" static="true"/>
</display> </display>
</class> </class>
<class id="3" language="java" name="com.iluwatar.hexagonal.domain.LotterySystemImpl" project="hexagonal" <class id="3" language="java" name="com.iluwatar.hexagonal.domain.LotterySystem" project="hexagonal"
file="/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystemImpl.java" binary="false" file="/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotterySystemImpl.java" binary="false"
corner="BOTTOM_RIGHT"> corner="BOTTOM_RIGHT">
<position height="167" width="421" x="1263" y="122"/> <position height="167" width="421" x="1263" y="122"/>
@ -62,7 +62,7 @@
<operations public="true" package="true" protected="true" private="true" static="true"/> <operations public="true" package="true" protected="true" private="true" static="true"/>
</display> </display>
</interface> </interface>
<class id="7" language="java" name="com.iluwatar.hexagonal.service.ConsoleService" project="hexagonal" <class id="7" language="java" name="com.iluwatar.hexagonal.service.LotteryService" project="hexagonal"
file="/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleService.java" binary="false" file="/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleService.java" binary="false"
corner="BOTTOM_RIGHT"> corner="BOTTOM_RIGHT">
<position height="113" width="421" x="2029" y="122"/> <position height="113" width="421" x="2029" y="122"/>
@ -122,7 +122,7 @@
<operations public="true" package="true" protected="true" private="true" static="true"/> <operations public="true" package="true" protected="true" private="true" static="true"/>
</display> </display>
</class> </class>
<class id="13" language="java" name="com.iluwatar.hexagonal.administration.ConsoleAdministration" project="hexagonal" <class id="13" language="java" name="com.iluwatar.hexagonal.administration.LotteryAdministration" project="hexagonal"
file="/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java" binary="false" file="/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java" binary="false"
corner="BOTTOM_RIGHT"> corner="BOTTOM_RIGHT">
<position height="131" width="320" x="2490" y="122"/> <position height="131" width="320" x="2490" y="122"/>

View File

@ -23,17 +23,12 @@
package com.iluwatar.hexagonal; package com.iluwatar.hexagonal;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.iluwatar.hexagonal.administration.LotteryAdministration;
import com.iluwatar.hexagonal.administration.ConsoleAdministration;
import com.iluwatar.hexagonal.banking.InMemoryBank; import com.iluwatar.hexagonal.banking.InMemoryBank;
import com.iluwatar.hexagonal.banking.WireTransfers; import com.iluwatar.hexagonal.banking.WireTransfers;
import com.iluwatar.hexagonal.database.InMemoryTicketRepository; import com.iluwatar.hexagonal.database.InMemoryTicketRepository;
import com.iluwatar.hexagonal.database.LotteryTicketRepository; import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import com.iluwatar.hexagonal.domain.LotterySystem;
import com.iluwatar.hexagonal.domain.LotterySystemImpl;
import com.iluwatar.hexagonal.notifications.LotteryNotifications; import com.iluwatar.hexagonal.notifications.LotteryNotifications;
import com.iluwatar.hexagonal.notifications.StdOutNotifications; import com.iluwatar.hexagonal.notifications.StdOutNotifications;
import com.iluwatar.hexagonal.service.ConsoleService;
import com.iluwatar.hexagonal.service.LotteryService; import com.iluwatar.hexagonal.service.LotteryService;
/** /**
@ -43,10 +38,7 @@ public class LotteryModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
bind(LotteryTicketRepository.class).to(InMemoryTicketRepository.class); bind(LotteryTicketRepository.class).to(InMemoryTicketRepository.class);
bind(LotterySystem.class).to(LotterySystemImpl.class);
bind(LotteryNotifications.class).to(StdOutNotifications.class); bind(LotteryNotifications.class).to(StdOutNotifications.class);
bind(WireTransfers.class).to(InMemoryBank.class); bind(WireTransfers.class).to(InMemoryBank.class);
bind(LotteryAdministration.class).to(ConsoleAdministration.class);
bind(LotteryService.class).to(ConsoleService.class);
} }
} }

View File

@ -1,61 +0,0 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.hexagonal.administration;
import com.google.inject.Inject;
import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotterySystem;
import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketId;
import java.util.Map;
/**
*
* Lottery administration implementation
*
*/
public class ConsoleAdministration implements LotteryAdministration {
private final LotterySystem lotterySystem;
@Inject
public ConsoleAdministration(LotterySystem lotterySystem) {
this.lotterySystem = lotterySystem;
}
@Override
public Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets() {
return lotterySystem.getAllSubmittedTickets();
}
@Override
public LotteryNumbers performLottery() {
return lotterySystem.performLottery();
}
@Override
public void resetLottery() {
lotterySystem.resetLottery();
}
}

View File

@ -22,7 +22,9 @@
*/ */
package com.iluwatar.hexagonal.administration; package com.iluwatar.hexagonal.administration;
import com.google.inject.Inject;
import com.iluwatar.hexagonal.domain.LotteryNumbers; import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotterySystem;
import com.iluwatar.hexagonal.domain.LotteryTicket; import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketId; import com.iluwatar.hexagonal.domain.LotteryTicketId;
@ -30,24 +32,36 @@ import java.util.Map;
/** /**
* *
* Administrator interface for lottery service. * Lottery administration implementation
* *
*/ */
public interface LotteryAdministration { public class LotteryAdministration {
private final LotterySystem lotterySystem;
@Inject
public LotteryAdministration(LotterySystem lotterySystem) {
this.lotterySystem = lotterySystem;
}
/** /**
* Get all the lottery tickets submitted for lottery * Get all the lottery tickets submitted for lottery
*/ */
Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets(); public Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets() {
return lotterySystem.getAllSubmittedTickets();
}
/** /**
* Draw lottery numbers * Draw lottery numbers
*/ */
LotteryNumbers performLottery(); public LotteryNumbers performLottery() {
return lotterySystem.performLottery();
}
/** /**
* Begin new lottery round * Begin new lottery round
*/ */
void resetLottery(); public void resetLottery() {
lotterySystem.resetLottery();
}
} }

View File

@ -22,37 +22,101 @@
*/ */
package com.iluwatar.hexagonal.domain; package com.iluwatar.hexagonal.domain;
import com.google.inject.Inject;
import com.iluwatar.hexagonal.banking.WireTransfers;
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
/** /**
* Lottery system interface * Lottery system
*/ */
public interface LotterySystem { public class LotterySystem {
private final LotteryTicketRepository repository;
private final LotteryNotifications notifications;
private final WireTransfers wireTransfers;
/**
* Constructor
*/
@Inject
public LotterySystem(LotteryTicketRepository repository, LotteryNotifications notifications,
WireTransfers wireTransfers) {
this.repository = repository;
this.notifications = notifications;
this.wireTransfers = wireTransfers;
}
/** /**
* Get all the lottery tickets submitted for lottery * Get all the lottery tickets submitted for lottery
*/ */
Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets(); public Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets() {
return repository.findAll();
}
/** /**
* Draw lottery numbers * Draw lottery numbers
*/ */
LotteryNumbers performLottery(); public LotteryNumbers performLottery() {
LotteryNumbers numbers = LotteryNumbers.createRandom();
Map<LotteryTicketId, LotteryTicket> tickets = getAllSubmittedTickets();
for (LotteryTicketId id : tickets.keySet()) {
LotteryTicketCheckResult result = checkTicketForPrize(id, numbers);
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
boolean transferred = wireTransfers.transferFunds(LotteryConstants.PRIZE_AMOUNT,
LotteryConstants.SERVICE_BANK_ACCOUNT, tickets.get(id).getPlayerDetails().getBankAccount());
if (transferred) {
notifications.notifyPrize(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
} else {
notifications.notifyPrizeError(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
}
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
notifications.notifyNoWin(tickets.get(id).getPlayerDetails());
}
}
return numbers;
}
/** /**
* Begin new lottery round * Begin new lottery round
*/ */
void resetLottery(); public void resetLottery() {
repository.deleteAll();
}
/** /**
* Submit lottery ticket to participate in the lottery * Submit lottery ticket to participate in the lottery
*/ */
Optional<LotteryTicketId> submitTicket(LotteryTicket ticket); public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE,
ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT);
if (result == false) {
notifications.notifyTicketSubmitError(ticket.getPlayerDetails());
return Optional.empty();
}
Optional<LotteryTicketId> optional = repository.save(ticket);
if (optional.isPresent()) {
notifications.notifyTicketSubmitted(ticket.getPlayerDetails());
}
return optional;
}
/** /**
* Check if lottery ticket has won * Check if lottery ticket has won
*/ */
LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers); public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
Optional<LotteryTicket> optional = repository.findById(id);
if (optional.isPresent()) {
if (optional.get().getNumbers().equals(winningNumbers)) {
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.WIN_PRIZE, 1000);
} else {
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.NO_PRIZE);
}
} else {
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.TICKET_NOT_SUBMITTED);
}
}
} }

View File

@ -1,112 +0,0 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.hexagonal.domain;
import com.google.inject.Inject;
import com.iluwatar.hexagonal.banking.WireTransfers;
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
import java.util.Map;
import java.util.Optional;
/**
* Lottery system implementation
*/
public class LotterySystemImpl implements LotterySystem {
private final LotteryTicketRepository repository;
private final LotteryNotifications notifications;
private final WireTransfers wireTransfers;
/**
* Constructor
*/
@Inject
public LotterySystemImpl(LotteryTicketRepository repository, LotteryNotifications notifications,
WireTransfers wireTransfers) {
this.repository = repository;
this.notifications = notifications;
this.wireTransfers = wireTransfers;
}
@Override
public Map<LotteryTicketId, LotteryTicket> getAllSubmittedTickets() {
return repository.findAll();
}
@Override
public LotteryNumbers performLottery() {
LotteryNumbers numbers = LotteryNumbers.createRandom();
Map<LotteryTicketId, LotteryTicket> tickets = getAllSubmittedTickets();
for (LotteryTicketId id : tickets.keySet()) {
LotteryTicketCheckResult result = checkTicketForPrize(id, numbers);
if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.WIN_PRIZE)) {
boolean transferred = wireTransfers.transferFunds(LotteryConstants.PRIZE_AMOUNT,
LotteryConstants.SERVICE_BANK_ACCOUNT, tickets.get(id).getPlayerDetails().getBankAccount());
if (transferred) {
notifications.notifyPrize(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
} else {
notifications.notifyPrizeError(tickets.get(id).getPlayerDetails(), LotteryConstants.PRIZE_AMOUNT);
}
} else if (result.getResult().equals(LotteryTicketCheckResult.CheckResult.NO_PRIZE)) {
notifications.notifyNoWin(tickets.get(id).getPlayerDetails());
}
}
return numbers;
}
@Override
public void resetLottery() {
repository.deleteAll();
}
@Override
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
boolean result = wireTransfers.transferFunds(LotteryConstants.TICKET_PRIZE,
ticket.getPlayerDetails().getBankAccount(), LotteryConstants.SERVICE_BANK_ACCOUNT);
if (result == false) {
notifications.notifyTicketSubmitError(ticket.getPlayerDetails());
return Optional.empty();
}
Optional<LotteryTicketId> optional = repository.save(ticket);
if (optional.isPresent()) {
notifications.notifyTicketSubmitted(ticket.getPlayerDetails());
}
return optional;
}
@Override
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
Optional<LotteryTicket> optional = repository.findById(id);
if (optional.isPresent()) {
if (optional.get().getNumbers().equals(winningNumbers)) {
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.WIN_PRIZE, 1000);
} else {
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.NO_PRIZE);
}
} else {
return new LotteryTicketCheckResult(LotteryTicketCheckResult.CheckResult.TICKET_NOT_SUBMITTED);
}
}
}

View File

@ -1,60 +0,0 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.hexagonal.service;
import com.google.inject.Inject;
import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotterySystem;
import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
import com.iluwatar.hexagonal.domain.LotteryTicketId;
import java.util.Optional;
/**
*
* Implementation for lottery service
*
*/
public class ConsoleService implements LotteryService {
private final LotterySystem lotterySystem;
/**
* Constructor
*/
@Inject
public ConsoleService(LotterySystem lotterySystem) {
this.lotterySystem = lotterySystem;
}
@Override
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
return lotterySystem.submitTicket(ticket);
}
@Override
public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
return lotterySystem.checkTicketForPrize(id, winningNumbers);
}
}

View File

@ -22,27 +22,39 @@
*/ */
package com.iluwatar.hexagonal.service; package com.iluwatar.hexagonal.service;
import com.iluwatar.hexagonal.domain.LotteryNumbers; import com.google.inject.Inject;
import com.iluwatar.hexagonal.domain.LotteryTicket; import com.iluwatar.hexagonal.domain.*;
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult;
import com.iluwatar.hexagonal.domain.LotteryTicketId;
import java.util.Optional; import java.util.Optional;
/** /**
* *
* Interface for submitting and checking lottery tickets. * Implementation for lottery service
* *
*/ */
public interface LotteryService { public class LotteryService {
private final LotterySystem lotterySystem;
/**
* Constructor
*/
@Inject
public LotteryService(LotterySystem lotterySystem) {
this.lotterySystem = lotterySystem;
}
/** /**
* Submit lottery ticket to participate in the lottery * Submit lottery ticket to participate in the lottery
*/ */
Optional<LotteryTicketId> submitTicket(LotteryTicket ticket); public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
return lotterySystem.submitTicket(ticket);
}
/** /**
* Check if lottery ticket has won * Check if lottery ticket has won
*/ */
LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers); public LotteryTicketCheckResult checkTicketForPrize(LotteryTicketId id, LotteryNumbers winningNumbers) {
return lotterySystem.checkTicketForPrize(id, winningNumbers);
}
} }

View File

@ -23,17 +23,12 @@
package com.iluwatar.hexagonal; package com.iluwatar.hexagonal;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.iluwatar.hexagonal.administration.ConsoleAdministration;
import com.iluwatar.hexagonal.administration.LotteryAdministration;
import com.iluwatar.hexagonal.banking.InMemoryBank; import com.iluwatar.hexagonal.banking.InMemoryBank;
import com.iluwatar.hexagonal.banking.WireTransfers; import com.iluwatar.hexagonal.banking.WireTransfers;
import com.iluwatar.hexagonal.database.InMemoryTicketRepository; import com.iluwatar.hexagonal.database.InMemoryTicketRepository;
import com.iluwatar.hexagonal.database.LotteryTicketRepository; import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import com.iluwatar.hexagonal.domain.LotterySystem;
import com.iluwatar.hexagonal.domain.LotterySystemImpl;
import com.iluwatar.hexagonal.notifications.LotteryNotifications; import com.iluwatar.hexagonal.notifications.LotteryNotifications;
import com.iluwatar.hexagonal.notifications.StdOutNotifications; import com.iluwatar.hexagonal.notifications.StdOutNotifications;
import com.iluwatar.hexagonal.service.ConsoleService;
import com.iluwatar.hexagonal.service.LotteryService; import com.iluwatar.hexagonal.service.LotteryService;
/** /**
@ -43,10 +38,7 @@ public class LotteryTestingModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
bind(LotteryTicketRepository.class).to(InMemoryTicketRepository.class); bind(LotteryTicketRepository.class).to(InMemoryTicketRepository.class);
bind(LotterySystem.class).to(LotterySystemImpl.class);
bind(LotteryNotifications.class).to(StdOutNotifications.class); bind(LotteryNotifications.class).to(StdOutNotifications.class);
bind(WireTransfers.class).to(InMemoryBank.class); bind(WireTransfers.class).to(InMemoryBank.class);
bind(LotteryAdministration.class).to(ConsoleAdministration.class);
bind(LotteryService.class).to(ConsoleService.class);
} }
} }