Merge pull request #13534 from etrandafir93/features/BAEL-6212-anemic_vs_rich_domain_model

BAEL-6212: rich domain model support
This commit is contained in:
davidmartinezbarua 2023-03-08 13:05:01 -03:00 committed by GitHub
commit 7be831c312
4 changed files with 168 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package com.baeldung.pattern.richdomainmodel;
public class Player {
private int points ;
final String name;
public Player(String name) {
this(name, 0);
}
private Player(String name, int points) {
this.name = name;
this.points = 0;
}
public void gainPoint() {
points++;
}
public boolean hasScoreBiggerThan(Score score) {
return this.points > score.points();
}
public int pointsDifference(Player other) {
return points - other.points;
}
public String name() {
return name;
}
public String score() {
return Score.from(points).label();
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.pattern.richdomainmodel;
import java.util.Arrays;
public enum Score {
LOVE(0, "Love"), FIFTEEN(1, "Fifteen"), THIRTY(2, "Thirty"), FORTY(3, "Forty");
private final int points;
private final String label;
Score(int points, String label) {
this.points = points;
this.label = label;
}
public static Score from(int value) {
return Arrays.stream(values())
.filter(v -> v.points == value)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("no such element: " + value));
}
public int points() {
return points;
}
public String label() {
return label;
}
}

View File

@ -0,0 +1,71 @@
package com.baeldung.pattern.richdomainmodel;
public class TennisGame {
private final Player server;
private final Player receiver;
public TennisGame(String server, String receiver) {
this.server = new Player(server);
this.receiver = new Player(receiver);
}
public void wonPoint(String playerName) {
if(server.name().equals(playerName)) {
server.gainPoint();
} else {
receiver.gainPoint();
}
}
public String getScore() {
if (gameContinues()) {
return getGameScore();
}
return "Win for " + leadingPlayer().name();
}
private String getGameScore() {
if (isScoreEqual()) {
return getEqualScore();
}
if (isAdvantage()) {
return "Advantage " + leadingPlayer().name();
}
return getSimpleScore();
}
private boolean isScoreEqual() {
return server.pointsDifference(receiver) == 0;
}
private boolean isAdvantage() {
return leadingPlayer().hasScoreBiggerThan(Score.FORTY)
&& Math.abs(server.pointsDifference(receiver)) == 1;
}
private boolean isGameFinished() {
return leadingPlayer().hasScoreBiggerThan(Score.FORTY)
&& Math.abs(server.pointsDifference(receiver)) >= 2;
}
private Player leadingPlayer() {
if (server.pointsDifference(receiver) > 0) {
return server;
}
return receiver;
}
private boolean gameContinues() {
return !isGameFinished();
}
private String getSimpleScore() {
return String.format("%s-%s", server.score(), receiver.score());
}
private String getEqualScore() {
if (server.hasScoreBiggerThan(Score.THIRTY)) {
return "Deuce";
}
return String.format("%s-All", server.score());
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.pattern.richdomainmodel;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class RichDomainModelUnitTest {
@Test
public void givenATennisGame_whenReceiverWinsThreePoints_thenScoreIsFortyLove() {
TennisGame game = new TennisGame("server", "receiver");
game.wonPoint("server");
game.wonPoint("server");
game.wonPoint("server");
assertThat(game.getScore())
.isEqualTo("Forty-Love");
}
@Test
public void givenATennisGame_whenEachPlayerWonTwoPoints_thenScoreIsThirtyAll() {
TennisGame game = new TennisGame("server", "receiver");
game.wonPoint("server");
game.wonPoint("server");
game.wonPoint("receiver");
game.wonPoint("receiver");
assertThat(game.getScore())
.isEqualTo("Thirty-All");
}
}