added lab12 ex1
This commit is contained in:
parent
8caae17dd4
commit
94df35881e
29
src/lab12_singleton/ex1/CarSensorSingletonLauncher.java
Normal file
29
src/lab12_singleton/ex1/CarSensorSingletonLauncher.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
15
src/lab12_singleton/ex1/Sensor.java
Normal file
15
src/lab12_singleton/ex1/Sensor.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
17
src/lab12_singleton/ex1/SingletonSensorManager.java
Normal file
17
src/lab12_singleton/ex1/SingletonSensorManager.java
Normal 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() + ">");
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
10
src/lab12_singleton/ex1/sensors/RoadConditionSensor.java
Normal file
10
src/lab12_singleton/ex1/sensors/RoadConditionSensor.java
Normal 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";
|
||||||
|
}
|
||||||
|
}
|
10
src/lab12_singleton/ex1/sensors/SpeedSensor.java
Normal file
10
src/lab12_singleton/ex1/sensors/SpeedSensor.java
Normal 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";
|
||||||
|
}
|
||||||
|
}
|
10
src/lab12_singleton/ex1/sensors/TemperatureSensor.java
Normal file
10
src/lab12_singleton/ex1/sensors/TemperatureSensor.java
Normal 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";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user