added lab12 ex1

This commit is contained in:
Louis Heredero 2024-11-11 09:05:04 +01:00
parent 8caae17dd4
commit 94df35881e
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
10 changed files with 148 additions and 0 deletions

View File

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

View File

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

View File

@ -0,0 +1,17 @@
package lab12_singleton.ex1;
import java.util.ArrayList;
public abstract class SingletonSensorManager {
private final ArrayList<Sensor> sensors = new ArrayList<>();
protected SingletonSensorManager() {}
public void addSensor(Sensor sensor) {
sensors.add(sensor);
}
public void printState() {
System.out.println("<" + this.getClass().getSimpleName() + ": " + sensors.toString() + ">");
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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