سؤال

So I have an ArrayList of objects of my own class:

public class A
{...}

Now say I have a variable that holds one of these objects from the list. Now I know if I change that object (e.g. set a field on that object), that this changes the memory that holds the object itsself, so both references (the variable and the internal array variable that holds the object in the list) are still identical and both still show to the same, now modified, object. Normal behavior!

But I also want that to be the case when I replace the object in the list that my variable points to. That is when I do

myList.set(theObjectPosition, newInstanceOfA);

...in this case I also want my variable to automagically have the reference to this new instance. What would be the easiest way to implement such behavior?

This is why I need this:

In short, I have a set of classes that handles undos/redos of changes made to a ArrayList (which I have not extended, that would not solve the problem anyway?). The classes are called

UndoAction, Add, Remove, Modify (the latter 3 inherit from UndoAction)

All classes have methods undo() and redo().

Example by describing (code is at the end if needed):

Say I want to add an item to a List: I do new Add(myObject), then I want to modify the item in the list so I do new Modify(myObject), after which it creates a DeepCopy of the object state as backup, after which I THEN change the object itsself (after having done the call to new Modify(..)). Then I do undo() on the Modify object so it does list.set(somePos, previousDeepCopy) (hence this post, the deepcopy made, by nature, is a new instance which messes things up!!!)....

So you can imagine that this list.set poses some problems. Any reference to the replaced object will be gone. So I can't work effectively with the list like that if it always replaces references to objects like that, hence my undo manager is doomed to failure this way.

So how do I combat these design flaws? Any suggestions are welcome.


Some code:

 protected abstract class UndoAction {
    protected HashSet<Integer> ids = new HashSet<Integer>();
    protected Marker marker;

    public UndoAction(List<E> l) {
      for (E e : l) {
        if (!entities.contains(e)) {
          entities.add(e);
          trace.append(entities.indexOf(e), new Stack<UndoAction>());
        }
        ids.add(entities.indexOf(e));
      }
    }

    public boolean sameAffectedTargets(UndoAction undoAction) {
      if (this.ids.containsAll(undoAction.ids) && undoAction.ids.containsAll(this.ids))
        return true;
      return false;
    }

    public Marker getMarker() {
      return new Marker(this);
    }

    public void firstRun() {
    }

    public void undo() {
    }

    public void redo() {
    }

    protected List<Data> getCopyEntities() {
      List<Data> l = new ArrayList<Data>();
      for (Integer id : ids) {
        E e = entities.get(id);
        int pos = adapterList.indexOf(e);
        l.add(new Data(id, pos, e));
      }
      return l;
    }

    protected List<Data> getDeepCopyEntities() {
      List<Data> l = new ArrayList<Data>();
      for (Integer id : ids) {
        E e = DeepCopy.copy(entities.get(id));
        int pos = adapterList.indexOf(entities.get(id));
        l.add(new Data(id, pos, e));
      }
      return l;
    }

    public void addEntities(List<Data> datas) {
      for (Data d : datas)
        d.addEntity();
    }

    public void setEntities(List<Data> datas) {
      for (Data d : datas)
        d.setEntity();
    }

    public void removeEntities(List<Data> datas) {
      for (Data d : datas)
        d.removeEntity();
    }

    protected class Data {
      public int id;
      public int pos;
      public E entity;

      public void addEntity() {
        entities.set(this.id, this.entity);
        adapterList.add(this.pos, this.entity);
      }

      public void setEntity() {
        entities.set(this.id, this.entity);
        E oldEntity = adapterList.get(this.pos);
        adapterList.set(this.pos, this.entity);
        notifyEntityReplaced(oldEntity, adapterList.get(this.pos), this.pos);
      }

      public void removeEntity() {
        entities.set(this.id, null);
        adapterList.remove(this.entity);
      }

      public Data(int id, int pos, E entity) {
        this.id = id;
        this.pos = pos;
        this.entity = entity;
      }
    }
  }

  protected class Add extends UndoAction {
    protected List<Data> addBackup;

    public Add(List<E> l) {
      super(l);
    }

    @Override
    public void undo() {
      super.undo();
      addBackup = getCopyEntities();
      removeEntities(addBackup);
    }

    @Override
    public void firstRun() {
      super.firstRun();
      adapterList.addAll(entities);
    }

    @Override
    public void redo() {
      super.redo();
      addEntities(addBackup);
    }
  }

  // call before modifying
  protected class Modify extends UndoAction {
    protected List<Data> beforeDeepCopies;
    protected List<Data> afterDeepCopies;

    public Modify(List<E> l) {
      super(l);
    }

    @Override
    public void undo() {
      super.undo();
      if (!skipModifying) {
        if (afterDeepCopies == null)
          afterDeepCopies = getDeepCopyEntities();
        setEntities(beforeDeepCopies);
      }
    }

    @Override
    public void firstRun() {
      super.firstRun();
      if (!skipModifying) // TODO
        beforeDeepCopies = getDeepCopyEntities();
    }

    @Override
    public void redo() {
      super.redo();
      if (!skipModifying)
        setEntities(afterDeepCopies);
    }
  }

  protected class Remove extends UndoAction {
    protected List<Data> removeBackup;

    public Remove(List<E> l) {
      super(l);
    }

    public List<E> getRemoved() {
      List<E> l = new ArrayList<E>();
      for (Data data : removeBackup)
        l.add(data.entity);
      return l;
    }

    @Override
    public void undo() {
      super.undo();
      addEntities(removeBackup);
    }

    @Override
    public void firstRun() {
      super.firstRun();
      removeBackup = getCopyEntities();
    }

    @Override
    public void redo() {
      super.redo();
      removeEntities(removeBackup);
    }
  }
هل كانت مفيدة؟

المحلول 2

I think you need an extra layer of indirection. Rather than having the other code reference an "A", you probably want it to reference a "Stack<A>", and use "peek()" to reference the current value (when you change the value in the array, you will probably want to "push()" the new value).

For the sake of code cleanliness, however, you can make this a little neater by making "A" into an interface and creating an implementation of "A" (let's call it "DelegatingStackAImpl") that will provide the same interface as "A" except with support for "push()" and "pop()" and forwarding invocations to whichever instance is currently on the top of the stack. Thus you won't break existing uses of "A" while still supporting this extra layer of indirection.

نصائح أخرى

If Object references in Java(ClassName) are like C pointers(ClassName*), You seem to want behaviour like C pointers to pointers(ClassName**).

One obvious way is to make Holder class whose only purpose is to hold a reference to your class. That way you can change the reference to the object itself, and as long as all are holding references to the Holder, they will also now point to the new replaced object.

public class AHolder{
    public A aRef; // or private with getter setter.
}

The simplest way would be just ensuring that your class is "mutable", that is, that you can change the internal state with the one of the new object.

myOldObject.replaceValues(myNewObject);

If the object itself, by some reason, cannot be done mutable, just build a wrapper around it.

If the object you want to replace is only referred to via its interface, then you can use a JDK dynamic proxy in the first place, which delegates to an actual implementation. To change the actual implementation, you need to modify the proxies invocation handler.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top