Task 1 - Improved Factory pattern

This commit is contained in:
0nlineSam 2025-01-23 13:43:50 +01:00
parent 12d4bb3c4d
commit 2cd827bdab
4 changed files with 20 additions and 9 deletions

View File

@ -1,7 +1,5 @@
package fr.enssat.BoulderDash.bridges; package fr.enssat.BoulderDash.bridges;
public class SoundBridgeFactory { public abstract class SoundBridgeFactory {
public static SoundBridge createSoundBridgeFromFilePath(String filePath) { public abstract SoundBridge createSoundBridgeFromFilePath(String filePath);
return new SoundJLayerBridge(filePath);
}
} }

View File

@ -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);
}
}

View File

@ -3,6 +3,7 @@ package fr.enssat.BoulderDash.controllers;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import fr.enssat.BoulderDash.bridges.SoundJLayerBridgeFactory;
import fr.enssat.BoulderDash.helpers.AudioLoadHelper; import fr.enssat.BoulderDash.helpers.AudioLoadHelper;
import fr.enssat.BoulderDash.models.LevelModel; import fr.enssat.BoulderDash.models.LevelModel;
import fr.enssat.BoulderDash.views.MenuView; import fr.enssat.BoulderDash.views.MenuView;
@ -27,7 +28,7 @@ public class NavigationBetweenViewController implements ActionListener {
* Class constructor * Class constructor
*/ */
public NavigationBetweenViewController() { public NavigationBetweenViewController() {
this.audioLoadHelper = new AudioLoadHelper(); this.audioLoadHelper = new AudioLoadHelper(new SoundJLayerBridgeFactory());
// Play game music // Play game music
this.getAudioLoadHelper().startMusic("game"); this.getAudioLoadHelper().startMusic("game");

View File

@ -18,13 +18,15 @@ import java.util.HashMap;
public class AudioLoadHelper { public class AudioLoadHelper {
private static String pathToAudioStore = "./res/audio"; private static String pathToAudioStore = "./res/audio";
private SoundBridgeFactory soundBridgeFactory;
private SoundBridge musicToPlay; private SoundBridge musicToPlay;
private HashMap<String, SoundBridge> preloadedSounds; private HashMap<String, SoundBridge> preloadedSounds;
/** /**
* Class constructor * Class constructor
*/ */
public AudioLoadHelper() { public AudioLoadHelper(SoundBridgeFactory soundBridgeFactory) {
this.soundBridgeFactory = soundBridgeFactory;
this.preloadSounds(); this.preloadSounds();
} }
@ -49,7 +51,7 @@ public class AudioLoadHelper {
} }
String filePath = this.getMusicPathInAudioStore(musicId); String filePath = this.getMusicPathInAudioStore(musicId);
this.musicToPlay = SoundBridgeFactory.createSoundBridgeFromFilePath(filePath); this.musicToPlay = soundBridgeFactory.createSoundBridgeFromFilePath(filePath);
this.musicToPlay.play(); this.musicToPlay.play();
} }
@ -77,13 +79,13 @@ public class AudioLoadHelper {
} }
}); });
// Cache them all! // Cache them all!^
for (File curSoundId : soundFiles) { for (File curSoundId : soundFiles) {
curSoundIdPrep = curSoundId.getName(); curSoundIdPrep = curSoundId.getName();
curSoundIdPrep = curSoundIdPrep.substring(0, curSoundIdPrep.lastIndexOf('.')); curSoundIdPrep = curSoundIdPrep.substring(0, curSoundIdPrep.lastIndexOf('.'));
this.preloadedSounds.put(curSoundIdPrep, this.preloadedSounds.put(curSoundIdPrep,
SoundBridgeFactory.createSoundBridgeFromFilePath(curSoundId.getPath())); soundBridgeFactory.createSoundBridgeFromFilePath(curSoundId.getPath()));
} }
} }