From 2cd827bdaba4bc96a7a8c696c732e58fef24fe5b Mon Sep 17 00:00:00 2001 From: 0nlineSam Date: Thu, 23 Jan 2025 13:43:50 +0100 Subject: [PATCH] Task 1 - Improved Factory pattern --- .../enssat/BoulderDash/bridges/SoundBridgeFactory.java | 6 ++---- .../BoulderDash/bridges/SoundJLayerBridgeFactory.java | 10 ++++++++++ .../controllers/NavigationBetweenViewController.java | 3 ++- .../fr/enssat/BoulderDash/helpers/AudioLoadHelper.java | 10 ++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 boulder-dash/src/fr/enssat/BoulderDash/bridges/SoundJLayerBridgeFactory.java diff --git a/boulder-dash/src/fr/enssat/BoulderDash/bridges/SoundBridgeFactory.java b/boulder-dash/src/fr/enssat/BoulderDash/bridges/SoundBridgeFactory.java index 163f6770..695661c1 100644 --- a/boulder-dash/src/fr/enssat/BoulderDash/bridges/SoundBridgeFactory.java +++ b/boulder-dash/src/fr/enssat/BoulderDash/bridges/SoundBridgeFactory.java @@ -1,7 +1,5 @@ package fr.enssat.BoulderDash.bridges; -public class SoundBridgeFactory { - public static SoundBridge createSoundBridgeFromFilePath(String filePath) { - return new SoundJLayerBridge(filePath); - } +public abstract class SoundBridgeFactory { + public abstract SoundBridge createSoundBridgeFromFilePath(String filePath); } diff --git a/boulder-dash/src/fr/enssat/BoulderDash/bridges/SoundJLayerBridgeFactory.java b/boulder-dash/src/fr/enssat/BoulderDash/bridges/SoundJLayerBridgeFactory.java new file mode 100644 index 00000000..39cf0780 --- /dev/null +++ b/boulder-dash/src/fr/enssat/BoulderDash/bridges/SoundJLayerBridgeFactory.java @@ -0,0 +1,10 @@ +package fr.enssat.BoulderDash.bridges; + +public class SoundJLayerBridgeFactory extends SoundBridgeFactory { + public SoundJLayerBridgeFactory() {} + + @Override + public SoundBridge createSoundBridgeFromFilePath(String filePath) { + return new SoundJLayerBridge(filePath); + } +} diff --git a/boulder-dash/src/fr/enssat/BoulderDash/controllers/NavigationBetweenViewController.java b/boulder-dash/src/fr/enssat/BoulderDash/controllers/NavigationBetweenViewController.java index db190f48..e818dea1 100644 --- a/boulder-dash/src/fr/enssat/BoulderDash/controllers/NavigationBetweenViewController.java +++ b/boulder-dash/src/fr/enssat/BoulderDash/controllers/NavigationBetweenViewController.java @@ -3,6 +3,7 @@ package fr.enssat.BoulderDash.controllers; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import fr.enssat.BoulderDash.bridges.SoundJLayerBridgeFactory; import fr.enssat.BoulderDash.helpers.AudioLoadHelper; import fr.enssat.BoulderDash.models.LevelModel; import fr.enssat.BoulderDash.views.MenuView; @@ -27,7 +28,7 @@ public class NavigationBetweenViewController implements ActionListener { * Class constructor */ public NavigationBetweenViewController() { - this.audioLoadHelper = new AudioLoadHelper(); + this.audioLoadHelper = new AudioLoadHelper(new SoundJLayerBridgeFactory()); // Play game music this.getAudioLoadHelper().startMusic("game"); diff --git a/boulder-dash/src/fr/enssat/BoulderDash/helpers/AudioLoadHelper.java b/boulder-dash/src/fr/enssat/BoulderDash/helpers/AudioLoadHelper.java index 11d0a7cf..707bc7e1 100644 --- a/boulder-dash/src/fr/enssat/BoulderDash/helpers/AudioLoadHelper.java +++ b/boulder-dash/src/fr/enssat/BoulderDash/helpers/AudioLoadHelper.java @@ -18,13 +18,15 @@ import java.util.HashMap; public class AudioLoadHelper { private static String pathToAudioStore = "./res/audio"; + private SoundBridgeFactory soundBridgeFactory; private SoundBridge musicToPlay; private HashMap preloadedSounds; /** * Class constructor */ - public AudioLoadHelper() { + public AudioLoadHelper(SoundBridgeFactory soundBridgeFactory) { + this.soundBridgeFactory = soundBridgeFactory; this.preloadSounds(); } @@ -49,7 +51,7 @@ public class AudioLoadHelper { } String filePath = this.getMusicPathInAudioStore(musicId); - this.musicToPlay = SoundBridgeFactory.createSoundBridgeFromFilePath(filePath); + this.musicToPlay = soundBridgeFactory.createSoundBridgeFromFilePath(filePath); this.musicToPlay.play(); } @@ -77,13 +79,13 @@ public class AudioLoadHelper { } }); - // Cache them all! + // Cache them all!^ for (File curSoundId : soundFiles) { curSoundIdPrep = curSoundId.getName(); curSoundIdPrep = curSoundIdPrep.substring(0, curSoundIdPrep.lastIndexOf('.')); this.preloadedSounds.put(curSoundIdPrep, - SoundBridgeFactory.createSoundBridgeFromFilePath(curSoundId.getPath())); + soundBridgeFactory.createSoundBridgeFromFilePath(curSoundId.getPath())); } }