Poging tot het flexibel laten werken van devices met features. Zou moeten werken via een enum, maar lukt nog niet.

This commit is contained in:
2011-02-02 13:00:33 +00:00
parent 35defa2593
commit 1e53c39bc2
8 changed files with 380 additions and 1 deletions

30
java/src/pm/Main.java Normal file
View File

@@ -0,0 +1,30 @@
package pm;
import java.util.ArrayList;
import pm.device.Device;
import pm.device.Features;
import pm.device.exampledevice.ExampleDevice;
public class Main {
ArrayList<Device> deviceList;
public Main() {
deviceList = new ArrayList<Device>();
}
public void addDevice(Device device) {
deviceList.add(device);
if (device.hasFeature(Features.RESTART)) {
device.start();
}
}
public void start() {
addDevice(new ExampleDevice());
}
public static void main(String[] args) {
new Main().start();
}
}

View File

@@ -0,0 +1,31 @@
package pm.device;
import java.util.ArrayList;
import pm.device.feature.Feature;
public abstract class Device {
protected ArrayList<Feature> featureList;
protected Device() {
featureList = new ArrayList<Feature>();
}
public void addFeature(Feature feature) {
if (!hasFeature(feature)) {
if (this instanceof feature.getClass()) {
}
featureList.add(feature);
}
}
public void removeFeature(Feature feature) {
featureList.remove(feature);
}
public boolean hasFeature(Feature feature) {
return featureList.contains(feature);
}
}

View File

@@ -0,0 +1,11 @@
package pm.device;
import pm.device.feature.Feature;
import pm.device.feature.Restart;
public enum Features {
Restart ((Class<Restart>)Restart.class);
Features(Class<Feature> feature) {
}
}

View File

@@ -0,0 +1,22 @@
package pm.device.exampledevice;
import pm.device.Device;
import pm.device.Features;
public class ExampleDevice extends Device {
public ExampleDevice() {
addFeature(Features.Restart);
}
public void start() {
}
public void stop() {
}
public void restart() {
}
}

View File

@@ -0,0 +1,5 @@
package pm.device.feature;
public interface Feature {
}

View File

@@ -0,0 +1,8 @@
package pm.device.feature;
public interface Restart extends Feature {
public static final String feature = "RESTART";
public void stop();
public void restart();
}