diff --git a/src/lab12_singleton/ex1/CarSensorSingletonLauncher.java b/src/lab12_singleton/ex1/CarSensorSingletonLauncher.java new file mode 100644 index 0000000..711bf06 --- /dev/null +++ b/src/lab12_singleton/ex1/CarSensorSingletonLauncher.java @@ -0,0 +1,29 @@ +package lab12_singleton.ex1; + +import lab12_singleton.ex1.sensors.*; +import lab12_singleton.ex1.managers.*; + +public class CarSensorSingletonLauncher { + public CarSensorSingletonLauncher() { + + } + + public static void main(String[] args) { + CarSensorSingletonLauncher launcher = new CarSensorSingletonLauncher(); + launcher.launch(); + } + + public void launch() { + this.test(SimpleSingletonSensorsManager.getInstance()); + this.test(SynchSingletonSensorsManager.getInstance()); + this.test(EagerSingletonSensorsManager.getInstance()); + this.test(DbleCheckedSingletonSensorsManager.getInstance()); + } + + private void test(SingletonSensorManager manager) { + manager.addSensor(new TemperatureSensor()); + manager.addSensor(new SpeedSensor()); + manager.addSensor(new RoadConditionSensor()); + manager.printState(); + } +} diff --git a/src/lab12_singleton/ex1/Sensor.java b/src/lab12_singleton/ex1/Sensor.java new file mode 100644 index 0000000..32119c6 --- /dev/null +++ b/src/lab12_singleton/ex1/Sensor.java @@ -0,0 +1,15 @@ +package lab12_singleton.ex1; + +public abstract class Sensor { + private boolean ok = true; + + public abstract String getName(); + public void setOk(boolean ok) { + this.ok = ok; + } + + @Override + public String toString() { + return getName() + ": " + (ok ? "Ok" : "Warning"); + } +} diff --git a/src/lab12_singleton/ex1/SingletonSensorManager.java b/src/lab12_singleton/ex1/SingletonSensorManager.java new file mode 100644 index 0000000..ea4b57f --- /dev/null +++ b/src/lab12_singleton/ex1/SingletonSensorManager.java @@ -0,0 +1,17 @@ +package lab12_singleton.ex1; + +import java.util.ArrayList; + +public abstract class SingletonSensorManager { + private final ArrayList sensors = new ArrayList<>(); + + protected SingletonSensorManager() {} + + public void addSensor(Sensor sensor) { + sensors.add(sensor); + } + + public void printState() { + System.out.println("<" + this.getClass().getSimpleName() + ": " + sensors.toString() + ">"); + } +} diff --git a/src/lab12_singleton/ex1/managers/DbleCheckedSingletonSensorsManager.java b/src/lab12_singleton/ex1/managers/DbleCheckedSingletonSensorsManager.java new file mode 100644 index 0000000..78adf79 --- /dev/null +++ b/src/lab12_singleton/ex1/managers/DbleCheckedSingletonSensorsManager.java @@ -0,0 +1,18 @@ +package lab12_singleton.ex1.managers; + +import lab12_singleton.ex1.SingletonSensorManager; + +public class DbleCheckedSingletonSensorsManager extends SingletonSensorManager { + private static DbleCheckedSingletonSensorsManager instance; + + public static DbleCheckedSingletonSensorsManager getInstance() { + if (instance == null) { + synchronized (DbleCheckedSingletonSensorsManager.class) { + if (instance == null) { + instance = new DbleCheckedSingletonSensorsManager(); + } + } + } + return instance; + } +} diff --git a/src/lab12_singleton/ex1/managers/EagerSingletonSensorsManager.java b/src/lab12_singleton/ex1/managers/EagerSingletonSensorsManager.java new file mode 100644 index 0000000..98a76dd --- /dev/null +++ b/src/lab12_singleton/ex1/managers/EagerSingletonSensorsManager.java @@ -0,0 +1,11 @@ +package lab12_singleton.ex1.managers; + +import lab12_singleton.ex1.SingletonSensorManager; + +public class EagerSingletonSensorsManager extends SingletonSensorManager { + private static final EagerSingletonSensorsManager instance = new EagerSingletonSensorsManager(); + + public static EagerSingletonSensorsManager getInstance() { + return instance; + } +} diff --git a/src/lab12_singleton/ex1/managers/SimpleSingletonSensorsManager.java b/src/lab12_singleton/ex1/managers/SimpleSingletonSensorsManager.java new file mode 100644 index 0000000..aed4c3f --- /dev/null +++ b/src/lab12_singleton/ex1/managers/SimpleSingletonSensorsManager.java @@ -0,0 +1,14 @@ +package lab12_singleton.ex1.managers; + +import lab12_singleton.ex1.SingletonSensorManager; + +public class SimpleSingletonSensorsManager extends SingletonSensorManager { + private static SimpleSingletonSensorsManager instance; + + public static SingletonSensorManager getInstance() { + if (instance == null) { + instance = new SimpleSingletonSensorsManager(); + } + return instance; + } +} diff --git a/src/lab12_singleton/ex1/managers/SynchSingletonSensorsManager.java b/src/lab12_singleton/ex1/managers/SynchSingletonSensorsManager.java new file mode 100644 index 0000000..be62331 --- /dev/null +++ b/src/lab12_singleton/ex1/managers/SynchSingletonSensorsManager.java @@ -0,0 +1,14 @@ +package lab12_singleton.ex1.managers; + +import lab12_singleton.ex1.SingletonSensorManager; + +public class SynchSingletonSensorsManager extends SingletonSensorManager { + private static SynchSingletonSensorsManager instance; + + public static synchronized SynchSingletonSensorsManager getInstance() { + if (instance == null) { + instance = new SynchSingletonSensorsManager(); + } + return instance; + } +} diff --git a/src/lab12_singleton/ex1/sensors/RoadConditionSensor.java b/src/lab12_singleton/ex1/sensors/RoadConditionSensor.java new file mode 100644 index 0000000..5f36b97 --- /dev/null +++ b/src/lab12_singleton/ex1/sensors/RoadConditionSensor.java @@ -0,0 +1,10 @@ +package lab12_singleton.ex1.sensors; + +import lab12_singleton.ex1.Sensor; + +public class RoadConditionSensor extends Sensor { + @Override + public String getName() { + return "Road Condition"; + } +} diff --git a/src/lab12_singleton/ex1/sensors/SpeedSensor.java b/src/lab12_singleton/ex1/sensors/SpeedSensor.java new file mode 100644 index 0000000..de070f2 --- /dev/null +++ b/src/lab12_singleton/ex1/sensors/SpeedSensor.java @@ -0,0 +1,10 @@ +package lab12_singleton.ex1.sensors; + +import lab12_singleton.ex1.Sensor; + +public class SpeedSensor extends Sensor { + @Override + public String getName() { + return "Speed"; + } +} diff --git a/src/lab12_singleton/ex1/sensors/TemperatureSensor.java b/src/lab12_singleton/ex1/sensors/TemperatureSensor.java new file mode 100644 index 0000000..b6ce892 --- /dev/null +++ b/src/lab12_singleton/ex1/sensors/TemperatureSensor.java @@ -0,0 +1,10 @@ +package lab12_singleton.ex1.sensors; + +import lab12_singleton.ex1.Sensor; + +public class TemperatureSensor extends Sensor { + @Override + public String getName() { + return "Temperature"; + } +}