Simplify ComponentLocator

This commit is contained in:
2025-04-27 22:52:25 +02:00
parent ac854ed4f1
commit c38835fd7b
22 changed files with 57 additions and 71 deletions

2
.idea/misc.xml generated
View File

@ -13,7 +13,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@ -588,7 +588,7 @@ public class Commands extends PICComponent implements CommandInterface {
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
} }
} }

View File

@ -3,7 +3,6 @@ package fabrik.simulator.pic16f84;
import com.gluonhq.charm.glisten.control.ToggleButtonGroup; import com.gluonhq.charm.glisten.control.ToggleButtonGroup;
import fabrik.simulator.pic16f84.interfaces.*; import fabrik.simulator.pic16f84.interfaces.*;
import fabrik.simulator.pic16f84.interfaces.WatchdogTimerInterface;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
@ -42,7 +41,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
private int [] prog; private int [] prog;
private int [][] read; private int [][] read;
private int [] ind; private int [] ind;
private PICComponents picComponents; private PICComponentLocator picComponents;
private boolean isBreakpointReached = false; private boolean isBreakpointReached = false;
@ -553,9 +552,9 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
} }
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
this.picComponents = picComponents; this.picComponents = locator;
System.out.println("Frontend"); System.out.println("Frontend");
} }
} }

View File

@ -4,11 +4,9 @@ import fabrik.simulator.pic16f84.interfaces.*;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.Stage; import javafx.stage.Stage;
@ -17,7 +15,7 @@ import java.io.IOException;
import java.util.Objects; import java.util.Objects;
public class CreateWindow extends Application { public class CreateWindow extends Application {
private PICComponents picComponents = new PICComponents(); // Subjekt private PICComponentLocator picComponents = new PICComponentLocator(); // Subjekt
public static GridPane grid = new GridPane(); public static GridPane grid = new GridPane();
private static VBox table; private static VBox table;
@ -40,6 +38,7 @@ public class CreateWindow extends Application {
FXMLLoader codewindow = new FXMLLoader(CreateWindow.class.getResource("frontend.fxml")); FXMLLoader codewindow = new FXMLLoader(CreateWindow.class.getResource("frontend.fxml"));
codewindow.setController(picComponents.getComponent(FrontendControllerInterface.class)); codewindow.setController(picComponents.getComponent(FrontendControllerInterface.class));
Parent code = codewindow.load(); Parent code = codewindow.load();
table = Table.init(picComponents); table = Table.init(picComponents);
grid.add(table, 1, 1); grid.add(table, 1, 1);
grid.add(code, 0, 1); grid.add(code, 0, 1);

View File

@ -39,8 +39,8 @@ public class DataRegister extends PICComponent implements DataRegisterInterface
} }
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
System.out.println("DataRegister.\n"); System.out.println("DataRegister.\n");
initOrReset(); initOrReset();
} }

View File

@ -1,7 +1,6 @@
package fabrik.simulator.pic16f84; package fabrik.simulator.pic16f84;
import eu.hansolo.tilesfx.Command;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
@ -173,7 +172,7 @@ public class EEPROM extends PICComponent implements EEPROMInterface {
} }
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
} }
} }

View File

@ -145,7 +145,7 @@ public class EmptyRegister implements DataRegisterInterface {
} }
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator picComponents) {
} }
} }

View File

@ -227,8 +227,8 @@ public class IOPorts extends PICComponent implements IOPortInterface {
} }
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
System.out.println("IOPorts.\n"); System.out.println("IOPorts.\n");
} }
} }

View File

@ -1,9 +1,5 @@
package fabrik.simulator.pic16f84; package fabrik.simulator.pic16f84;
import eu.hansolo.tilesfx.Command;
import fabrik.simulator.pic16f84.interfaces.CommandInterface;
import fabrik.simulator.pic16f84.interfaces.DataRegisterInterface;
import fabrik.simulator.pic16f84.interfaces.FrontendControllerInterface;
import fabrik.simulator.pic16f84.interfaces.InterruptInterface; import fabrik.simulator.pic16f84.interfaces.InterruptInterface;
public class Interrupts extends PICComponent implements InterruptInterface { public class Interrupts extends PICComponent implements InterruptInterface {
@ -66,7 +62,7 @@ public class Interrupts extends PICComponent implements InterruptInterface {
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
} }
} }

View File

@ -9,27 +9,23 @@ public abstract class PICComponent {
IOPortInterface ioPorts; IOPortInterface ioPorts;
TimerInterface timer; TimerInterface timer;
InterruptInterface interrupts; InterruptInterface interrupts;
//TableInterface table;
//WindowManagement createWindow;
FrontendControllerInterface frontendController; FrontendControllerInterface frontendController;
WatchdogTimerInterface watchdogTimer; WatchdogTimerInterface watchdogTimer;
ProgramStackInterface programStack; ProgramStackInterface programStack;
CommandInterface commands; CommandInterface commands;
ToggleButtonInterface toggleButtonExt; ToggleButtonInterface toggleButtonExt;
void initialize(PICComponents picComponents) { void initialize(PICComponentLocator locator) {
toggleButtonExt = (ToggleButtonInterface) picComponents.getComponent(ToggleButtonInterface.class); toggleButtonExt = locator.getComponent(ToggleButtonInterface.class);
dataRegister = (DataRegisterInterface) picComponents.getComponent(DataRegisterInterface.class); dataRegister = locator.getComponent(DataRegisterInterface.class);
eeprom = (EEPROMInterface) picComponents.getComponent(EEPROMInterface.class); eeprom = locator.getComponent(EEPROMInterface.class);
ioPorts = (IOPortInterface) picComponents.getComponent(IOPortInterface.class); ioPorts = locator.getComponent(IOPortInterface.class);
preScaler = (PreScalerInterface) picComponents.getComponent(PreScalerInterface.class); preScaler = locator.getComponent(PreScalerInterface.class);
timer = (TimerInterface) picComponents.getComponent(TimerInterface.class); timer = locator.getComponent(TimerInterface.class);
interrupts = (InterruptInterface) picComponents.getComponent(InterruptInterface.class); interrupts = locator.getComponent(InterruptInterface.class);
//table = (TableInterface) picComponents.getComponent(TableInterface.class); frontendController = locator.getComponent(FrontendControllerInterface.class);
//createWindow = (WindowManagement) picComponents.getComponent(WindowManagement.class); watchdogTimer = locator.getComponent(WatchdogTimerInterface.class);
frontendController = (FrontendControllerInterface) picComponents.getComponent(FrontendControllerInterface.class); programStack = locator.getComponent(ProgramStackInterface.class);
watchdogTimer = (WatchdogTimerInterface) picComponents.getComponent(WatchdogTimerInterface.class); commands = locator.getComponent(CommandInterface.class);
programStack = (ProgramStackInterface) picComponents.getComponent(ProgramStackInterface.class);
commands = (CommandInterface) picComponents.getComponent(CommandInterface.class);
} }
} }

View File

@ -4,10 +4,11 @@ import fabrik.simulator.pic16f84.interfaces.*;
import java.util.HashMap; import java.util.HashMap;
public class PICComponents { public class PICComponentLocator {
// https://en.wikipedia.org/wiki/Service_locator_pattern
private final HashMap<Class<? extends PICComponentInterface>, PICComponentInterface> componentCatalogue; private final HashMap<Class<? extends PICComponentInterface>, PICComponentInterface> componentCatalogue;
public PICComponents() { public PICComponentLocator() {
super(); super();
this.componentCatalogue = new HashMap<>(); this.componentCatalogue = new HashMap<>();
} }
@ -16,8 +17,9 @@ public class PICComponents {
this.componentCatalogue.put(componentClass, component); this.componentCatalogue.put(componentClass, component);
} }
public PICComponentInterface getComponent(Class<? extends PICComponentInterface> componentClass) { public <T extends PICComponentInterface> T getComponent(Class<T> componentClass) {
return this.componentCatalogue.get(componentClass); T component = (T) this.componentCatalogue.get(componentClass);
return component;
} }
public void initAll() { public void initAll() {

View File

@ -54,7 +54,7 @@ public class PreScaler extends PICComponent implements PreScalerInterface {
} }
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
} }
} }

View File

@ -48,7 +48,7 @@ public class ProgramStack extends PICComponent implements ProgramStackInterface
} }
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
} }
} }

View File

@ -4,7 +4,6 @@ import fabrik.simulator.pic16f84.interfaces.*;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn;
@ -46,7 +45,7 @@ public class Table {
return s; return s;
} }
public static VBox init(PICComponents picComponents){ public static VBox init(PICComponentLocator picComponents){
dataRegister = (DataRegisterInterface) picComponents.getComponent(DataRegisterInterface.class); dataRegister = (DataRegisterInterface) picComponents.getComponent(DataRegisterInterface.class);
commands = (CommandInterface) picComponents.getComponent(CommandInterface.class); commands = (CommandInterface) picComponents.getComponent(CommandInterface.class);
ioPorts = (IOPortInterface) picComponents.getComponent(IOPortInterface.class); ioPorts = (IOPortInterface) picComponents.getComponent(IOPortInterface.class);

View File

@ -1,8 +1,5 @@
package fabrik.simulator.pic16f84; package fabrik.simulator.pic16f84;
import fabrik.simulator.pic16f84.interfaces.DataRegisterInterface;
import fabrik.simulator.pic16f84.interfaces.FrontendControllerInterface;
import fabrik.simulator.pic16f84.interfaces.InterruptInterface;
import fabrik.simulator.pic16f84.interfaces.TimerInterface; import fabrik.simulator.pic16f84.interfaces.TimerInterface;
public class Timer extends PICComponent implements TimerInterface { public class Timer extends PICComponent implements TimerInterface {
@ -67,7 +64,7 @@ public class Timer extends PICComponent implements TimerInterface {
} }
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
} }
} }

View File

@ -1,7 +1,6 @@
package fabrik.simulator.pic16f84; package fabrik.simulator.pic16f84;
import com.gluonhq.charm.glisten.control.ToggleButtonGroup; import com.gluonhq.charm.glisten.control.ToggleButtonGroup;
import fabrik.simulator.pic16f84.interfaces.IOPortInterface;
import fabrik.simulator.pic16f84.interfaces.ToggleButtonInterface; import fabrik.simulator.pic16f84.interfaces.ToggleButtonInterface;
import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener;
import javafx.event.EventHandler; import javafx.event.EventHandler;
@ -59,8 +58,8 @@ public class ToggleButtonGroupExt extends PICComponent implements ToggleButtonIn
}; };
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
System.out.println("ToggleButtonGroupExt"); System.out.println("ToggleButtonGroupExt");
super.initialize(picComponents); super.initialize(locator);
} }
} }

View File

@ -53,7 +53,7 @@ public class WatchdogTimer extends PICComponent implements WatchdogTimerInterfac
} }
@Override @Override
public void initialize(PICComponents picComponents) { public void initialize(PICComponentLocator locator) {
super.initialize(picComponents); super.initialize(locator);
} }
} }

View File

@ -1,7 +1,7 @@
package fabrik.simulator.pic16f84.interfaces; package fabrik.simulator.pic16f84.interfaces;
import fabrik.simulator.pic16f84.PICComponents; import fabrik.simulator.pic16f84.PICComponentLocator;
public interface PICComponentInterface { public interface PICComponentInterface {
void initialize(PICComponents picComponents); void initialize(PICComponentLocator picComponents);
} }

View File

@ -9,13 +9,13 @@ import org.junit.jupiter.api.Test;
class CommandsTests { class CommandsTests {
PICComponents picComponents; PICComponentLocator picComponents;
public CommandsTests() {} public CommandsTests() {}
@BeforeEach @BeforeEach
void resetComponents() { void resetComponents() {
picComponents = new PICComponents(); picComponents = new PICComponentLocator();
} }
@Test @Test

View File

@ -9,13 +9,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
class DataRegisterTests { class DataRegisterTests {
PICComponents picComponents; PICComponentLocator picComponents;
public DataRegisterTests() {} public DataRegisterTests() {}
@BeforeEach @BeforeEach
void resetComponents() { void resetComponents() {
picComponents = new PICComponents(); picComponents = new PICComponentLocator();
} }

View File

@ -20,14 +20,14 @@ import static org.mockito.Mockito.mock;
class EEPROMTests { class EEPROMTests {
PICComponents picComponents; PICComponentLocator picComponents;
String savedEEPROM; String savedEEPROM;
public EEPROMTests() {} public EEPROMTests() {}
@BeforeEach @BeforeEach
void resetComponentsAndReadEEPROM() throws IOException { void resetComponentsAndReadEEPROM() throws IOException {
picComponents = new PICComponents(); picComponents = new PICComponentLocator();
savedEEPROM = Files.readString(Path.of("eeprom.json")); savedEEPROM = Files.readString(Path.of("eeprom.json"));
} }

View File

@ -10,11 +10,11 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
class WatchDogTimerTests { class WatchDogTimerTests {
private PICComponents picComponents; private PICComponentLocator picComponents;
@BeforeEach @BeforeEach
void resetComponents() { void resetComponents() {
picComponents = new PICComponents(); picComponents = new PICComponentLocator();
} }
@Test @Test