From ce9aa06c66b08a50e2b053c4d94c64660a48ea6c Mon Sep 17 00:00:00 2001 From: fdai7736 Date: Tue, 6 Feb 2024 19:43:11 +0100 Subject: [PATCH] refactoring: add PropertyChangeSupport --- .../java/de/hsfulda/onses/models/Game.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/de/hsfulda/onses/models/Game.java b/src/main/java/de/hsfulda/onses/models/Game.java index 6a64133..17bc16d 100644 --- a/src/main/java/de/hsfulda/onses/models/Game.java +++ b/src/main/java/de/hsfulda/onses/models/Game.java @@ -3,10 +3,16 @@ package de.hsfulda.onses.models; import de.hsfulda.onses.services.GameService; import de.hsfulda.onses.services.PlayerService; +import java.beans.PropertyChangeSupport; import java.util.ArrayList; public class Game { + public final static String PROPERTY_LAST_PLAYED_CARD = "lastPlayedCard"; + public final static String PROPERTY_DRAW_CARD_DECK = "drawCardDeck"; + + protected PropertyChangeSupport listeners; + private GameService gameService; private PlayerService playerService; @@ -22,7 +28,9 @@ public class Game { lastPlayedCard.setColor(color); } public Game setLastPlayedCard(Card lastPlayedCard) { + final Card oldLastPlayedCard = this.lastPlayedCard; this.lastPlayedCard = lastPlayedCard; + this.firePropertyChange(PROPERTY_LAST_PLAYED_CARD, oldLastPlayedCard, lastPlayedCard); return this; } @@ -31,7 +39,9 @@ public class Game { } public void addCardToDrawCardDeck(Card card) { + final ArrayList oldCards = new ArrayList<>(this.drawCardDeck); drawCardDeck.add(card); + this.firePropertyChange(PROPERTY_DRAW_CARD_DECK, oldCards, drawCardDeck); } public GameService getGameService() { @@ -56,4 +66,19 @@ public class Game { { this.playerService = new PlayerService().setGame(this); } + + public PropertyChangeSupport listeners() { + if(this.listeners == null) { + this.listeners = new PropertyChangeSupport(this); + } + return this.listeners; + } + + public boolean firePropertyChange(String propertyName, Object oldValue, Object newValue) { + if(this.listeners != null) { + this.listeners.firePropertyChange(propertyName, oldValue, newValue); + return true; + } + return false; + } }