0

I've got this code:

int width = 1280;
int height = 720;

List<ElectronicDevice> devices = new ArrayList<ElectronicDevice>();

class ElectronicDevice {
  int x;
  int y;
  // ...
}

class EClock extends ElectronicDevice {
  int hz;

  EClock(int x, int y, int clock) {
    this.x = x;
    this.y = y;
    this.hz = hz;
  }
  // ...
}

class EBulb extends ElectronicDevice {
  EClock(int x, int y) {
    this.x = x;
    this.y = y;
  }
  // ...
}

class ToolbarElement {
  ElectronicDevice dev;

  ToolbarElement(ElectronicDevice dev) {
    this.dev = dev;
  }

  public void addDevice() {
    // somehow add a copy of `dev` to the `devices` list
  }
}

List<ToolbarElement> elements = new ArrayList<ToolbarElement>();

elements.add(new EClock(width/2, height/2, 5));
elements.add(new EBulb(width/2, height/2));

elements.get(0).addDevice();

I need to add a copy of ToolbarElement dev to devices, but I don't know how. It needs to be a deep copy. In the program I don't know the constructors of all devices, I'd like to just deep clone dev into devices.

I tried adding implements Cloneable to the ElectronicDevice class, but then I would need to add that and a clone() method to every class that extents ElectronicDevice.

After adding the implements Cloneable to ElectronicDevice it mostly worked, but all the clocks had desynchronized after adding more than 1 of them (this is a graphical application in Processing, every class that extends ElectronicDevice can have a behavior() method which specifies how it behaves and the clock oscillates between 0 and 1 with the frequency of hz Hz.)

This was my addDevice() implementation:

// In ElectronicDevice
class ElectronicDevice implements Cloneable {
  // ...
  protected Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
  // end of class code
}

// In ToolbarElement
class ToolbarElement {
  // ...
  public void addDevice() {
    try { devices.add((ElectronicDevice)object.clone()); }
    catch (CloneNotSupportedException e) { e.printStackTrace(); }
  }
  // end of class code
}

Is there any solution that wouldn't require modifying every single class that extends ElectronicDevice?

lqdev
  • 403
  • 3
  • 12
  • *Is there any solution that wouldn't require modifying every single class that extends ElectronicDevice?* - Yes, by using reflection, but I would prefer keeping my code clean and writing a `clone()` method inside each subclass instead. – BackSlash Dec 17 '17 at 15:05
  • @BackSlash this might be a good solution, but when you add more and more classes it becomes increasingly more painful to add this. What I might do though is create an `ETemplate` class so I can easily copy + paste it to create a new type of an `ElectronicDevice`. My point is that I want to add new devices as easily as it is possible, I am even making a custom modding API + scripting language to use in my program. – lqdev Dec 17 '17 at 15:19
  • each device has it's own properties, thus you have to override clone() at each subclass and return clone of "this". – matoni Dec 17 '17 at 15:21
  • @lqPGM Investing a bit more time to make clean and clear code is way better when it comes to maintainance. Don't be lazy, write good code. – BackSlash Dec 17 '17 at 15:23
  • @BackSlash I've tried adding the `clone()` method to every subclass and it still has the same issue (clock desync). Also I want to make it easier for maintainance by including the clone method in a single class, so instead of having to change it in 15 different classes I would only have to implement the changes into a single class. – lqdev Dec 17 '17 at 17:18

0 Answers0