Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5959915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:45:07+00:00 2026-05-22T18:45:07+00:00

I have multiple classes that are not allowed to modify each other’s fields, but

  • 0

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 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:45:08+00:00Added an answer on May 22, 2026 at 6:45 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple classes that are quite different in their behavior, but at the
I have multiple classes that all derive from a base class, now some of
I have a C# singleton class that multiple classes use. Is access through Instance
I have an enum which is used by multiple classes. What is the best
I have multiple threads (C# application running on IIS) running that all need to
I have multiple branches of a project checked out, each under their own directory
I have multiple Linq2Sql Classes such as Article NewsItem Product. They all have a
I have to tables I want to map to each other. I want to
Possible Duplicates: Why does Java allow multiple inheritance from interfaces but not from abstract/concrete
I have an AIR/Flex app that can have multiple windows (s:Window instances) open at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.