开发者

"Variable Assignment Request" Object Design

I have multiple classes that are not allowed to modify each other's fields, but instead must request to modify by adding a request object to the Main class's queue. The Main class, at the end of each loop, will perform the requested modifications.

public class Main {

    public static ClassA a = new ClassA();
    public static ClassB b = new ClassB();

    private static List<Request> requestQueue = new ArrayList<Request>(); // List holds all requests

    public static void main(String args[]) {
        while (true) {
            a.tick();
            b.tick();
            for (Request r : requestQueue) {
                r.field = r.value; // All requests are fulfilled 
            }
            requestQueue.clear();
        }
    }

    public static <ValueType> void addModificationRequest(ValueType field,
            ValueType value) {
        requestQueue.add(new Request<ValueType>(field, value));
    }
}

// Request Object
public class Request<ValueType> {

    ValueType field;
    ValueType value;

    public Request(ValueType field, ValueType value) {
        this.field = field;
        this.value = value;
    }
}

// Classes
public class ClassA {
    String name = "A";

    public void tick() {
        Main.addModificationRequest(Main.b.name, "NewNameB"); // Try to change b name
    }
}

public class ClassB {
    String name = "B";

    public void tick() {
        // Nothing
    }
}

I know the开发者_如何学Go code is messy, but its the simplest way I have thought of so far. Does anyone know any good design patterns that can accomplish this in a simpler and more robust way? (Note: I must have it this way, there is no possibility of allowing the classes to directly modify each other.)

Edit: The classes will have more than one modifiable variable. I also need to determine if any requests try to modify the same variable, so I can ignore one or both.

Edit: Thanks for all the help, I really appreciate it! I have decided to let the classes keep track of their own modifications. I am also going to use the same Request object I have been using because it is simple and straightforward. Again, thank you.


Maybe this doesn't apply for your use case, but you could have the objects themself keep track of pending modifications, and then have a call, made from your main class, that modifies the actual fields themself.

If you only need the set operation, keeping track of modifications is as easy as keeping an extra field for each field in the class. If you need a series of, say, arithmetic operations, you can use a list of modifications.

public class DelayedModificationField<T> {
    private T field;
    private T newValue;
    public T get() { return field;}
    public delayedSet(T value) {
        if (newValue != null) throw new ConcurrentModificationException("error");
        newValue = value;
    }
    public void performSet() {
        field = newValue;
        newValue = null;
    }

public ExampleClass {
    private DelayedModificationField<String> myField = new DelayedModificationField<String>();
    public void setMyField(String s) { myField.delayedSet(s);}
    public String getMyField() { return myField.get();}
    public void performSet() { myField.performSet();}
}

Your main class must keep track of all objects supporting delayed modification and call performSet on them each tick. Make them implement an interface and keep them in a list.


This isn't going to work. I don't know the exact parameters of your assignment, but I would define an interface Modifiable with a single method void setField(ValueType newValue). Then I would declare ClassA and ClassB to implement Modifiable. Finally, redefine your Request class to be a Modifiable and a ValueType instead of two ValueType fields. Your queue loop would then be:

for (Request r : requestQueue) {
    r.modifiable.setField(r.value);
}

EDIT:

Here's a modification of this approach that is extendable to multiple fields:

interface Modifiable {
    void setName(String newName);
    void setAge(Integer newAge); // I wish
}

abstract class Request<ValueType> {
    Modifiable object;
    ValueType value;

    public Request(Modifiable object, ValueType value) {
        this.object = object;
        this.value = value;
    }

    abstract void execute();
}

class RequestNameChange extends Request<String> {
    public RequestNameChange(Modifiable object, String name) {
        super(object, name);
    }

    void execute() {
        object.setName(name);
    }
}

// etc.

Another approach would be to use reflection, but I suspect that isn't what your assignment is about.


Try the "hollywood" pattern - it employs a "don't call us, we'll call you" style, aka using "callback" code.

Like this:

public static class Main {

    public static ClassA a = new ClassA();
    public static ClassB b = new ClassB();

    private static List<Runnable> requestQueue = new ArrayList<Runnable>(); // List holds all requests

    public static void main(String args[]) {
        while (true) {
            a.tick();
            b.tick();
            for (Runnable r : requestQueue) {
                r.run(); // All requests are fulfilled 
            }
            requestQueue.clear();
        }
    }

    public static void addModificationRequest(Runnable runnable) {
        requestQueue.add(runnable);
    }
}

Call addModificationRequest using an anonymous class:

Main.addModificationRequest(new Runnable() {
    public void run() {
        Main.b.name = "NewNameB";
    }
});

Note that this code will throw a ConcurrentModificationException if addModificationRequest is called while the foreach loop is executing. A cheap fix would be to add the keyword synchronized to addModificationRequest and execute the loop + clear within a synchronized block using the class object as the lock.

Edit:

To pass in the field too, so you can check it, try this:

interface FieldUpdater extends Runnable
{
    Field getField();
}

static class Main
{

    public static ClassA a = new ClassA();
    public static ClassB b = new ClassB();

    private static List<FieldUpdater> requestQueue = new ArrayList<FieldUpdater>(); // List holds all requests

    private static Set<Field> seenBefore = new HashSet<Field>();

    public void main(String args[])
    {
        while (true)
        {
            a.tick();
            b.tick();
            for (FieldUpdater r : requestQueue)
            {
                if (seenBefore.contains(r.getField()))
                {
                    continue; // Or do something else specific
                }
                seenBefore.add(r.getField());
                r.run();
            }
            requestQueue.clear();
        }
    }

    public synchronized static void addModificationRequest(FieldUpdater fieldUpdater)
    {
        requestQueue.add(fieldUpdater);
    }
}

Then call your method like this:

    Main.addModificationRequest(new FieldUpdater()
    {
        public void run()
        {
            Main.b.name = "NewNameB";
        }

        public Field getField()
        {
            return Main.class.getField("name");
        }

    });

Note that for clarity I have deliberately omitted dealing with the Exceptions that can be thrown from getField(). Also, this is not good code design, but it does address fixing your code as stated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜