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

  • SEARCH
  • Home
  • 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 8812781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:40:58+00:00 2026-06-14T03:40:58+00:00

I am writing a java simulation application which has a lot of entities to

  • 0

I am writing a java simulation application which has a lot of entities to simulate. Each of these entities has a certain state at any time in the system. A possible and natural approach to model such an entity would be using the state (or state machine) pattern. The problem is that it creates a lot of objects during the runtime if there are a lot of state switches, what might cause bad system performance. What design alternatives do I have? I want performance to be the main criteria after maintainability.

Thanks

  • 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-06-14T03:41:00+00:00Added an answer on June 14, 2026 at 3:41 am

    My suggestion:
    Have you “transitions managment” be configurable (i.e – via XML).

    Load the XML to a repository holding the states.
    The internal data structure will be a Map:

    Map<String,Map<String,Pair<String,StateChangeHandler>>> transitions;
    

    The reason for my selection is that this will be a map from a state name
    To a map of “inputs” and new states:
    Each map defines a map between possible input and the new state it leads to which is defined by the state name and a StateChangeHandler I will elaborate on later
    change state method at the repository would have a signature of:

    void changeState(StateOwner owner, String input)

    This way the repository is stateless in the sense of the state owner using it, you can copy one copy,
    and not worry about thread safety issues.
    StateOwner will be an interface your Classes that need state changing should implement.
    I think the interface should look like this:

    public interace StateOwner {
       String getState();
       void String setState(String newState);
    }
    

    In addition, you will have a ChangeStateHandler interface:

    public interface StateChangeHandler {
        void onChangeState(StateOwner, String newState) {
        }
    }
    

    When the repository’s changeState method is called, it will
    check at the data structure that the current state of the stateOwner has a map of “inputs”.
    If it has such a map, it will check if the input has a new State to change to, and invoke the onChangeState method.
    I will suggest you have a default implementation of the StateChangeHandler, and of course sub classes that will define the state change behavior more explicitly.

    As I previously mentioned, all this can be loaded from an XML configuration, and using reflection you can instantitate StateChangeHandler objects based on their name (as mentioned at the XML) and that will be held in the repository.

    Efficiency and good performance rely and obtained using the following points:
    a. The repository itself is stateless – no internal references of StateOwner should be kept.
    b. You load the XML once , when the system starts, after that you should work with in memory data structure.
    c. You will provide specific StateChangeHandler implementation only when needed, the default implementation should do basicaly nothing.
    d. No need to instantiate new objects of Handlers (as they should be stateless)

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

Sidebar

Related Questions

I am developing a Java Desktop Application which uses MySQL database. The DB has
I am writing a simple application in Java that does some particle simulation on
I am writing a simulation in Java whereby objects act under Newtonian physics. An
I am writing java code. which will work on windows and linux. some code
I usually C++ applications. The only web-application development I've done is writing Java Applets.
I am writing a java smart card application in windows MFC. How do i
I am writing a Java program which automatically plugs information into a website, which
I'm writing a java application. Currently this produces a file called trace.txt in the
I've just started writing Java and I'm trying to make a simulation of a
I am currently writing a java-class that wraps an SQLite database. This class has

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.