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 8016279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:29:43+00:00 2026-06-04T20:29:43+00:00

I am making a small boardgame program which needs to persist the state of

  • 0

I am making a small boardgame program which needs to persist the state of the board to a file, and later read from the file and re-create the board.

I am delegating this functionality to the class shown below. I would like to implement this such that the save format of a square of the board along with it’s co-ordinates are captured in the SQUARE_FORMAT constant, and the regex for reading that same information is captured in the LOAD_REGEX constant. Both should co-relate in code and also be able to visually decipher (by that I mean that a person should be able to clearly see that they co-relate to the same data)

Is there an idiom or pattern for doing this in Java code ?

public class BoardPersistenceUtility {

    private final String SQUARE_SAVE_FORMAT = "";
    private fial String LOAD_REGEX = "";

    public void save(PrintWriter writer, Board board) {

    }

    public Board load(BufferedReader reader) {
        // Implement
        return null;
    }
}

Update 1:

On reading my question again, I guess it might be a bit confusing, about what exactly I am looking for. I am specifically looking for the right way to represent SQUARE_SAVE_FORMAT so that it clearly co-relates with the regex LOAD_REGEX.

SQUARE_SAVE_FORMAT would ideally be a String which uses special characters/variables that will be replaced with actual values and the result will be saved to a file. LOAD_REGEX is the corresponding regex that will be used to read contents from the file. The regex will use capturing groups so I can re-create the original object from the values I get from the capturing groups.

My question is, what are the idioms around creating such pairs of Strings – one of them a format string to be used for saving data, and the other a regex to be used while reading that data.

Update 2:

On thinking a bit more, I think I have been able to clarify my question a bit better.

If you look at both the Strings, SQUARE_SAVE_FORMAT is a format string which will be used in String.format() to create the text for a square on the board, which will be saved in the file. The constant SQUARE_LOAD_REGEX is a regex which will be used to read the line and capture relevant parts into named groups, so I can re-create the original object. (sorry if my regex is slightly incorrect… I quickly wrote something, but I need to refresh some regex principles to ensure that this is indeed what I need)

If you look at both these Strings visually, it is difficult to co-relate them together. Perhaps it is because we do not have any named variables in a Java format String. The best we can do is to specify %i where i is the index of the argument.

I would like to understand if there is any idiom or pattern to represent such pairs of Strings, where one is used for formatting some data to text and the other is used to read the same text and parse it’s parts.

public class BoardPersistenceUtility {

    private final String SQUARE_SAVE_FORMAT = 
                  "%d,%d:%b-%s";
    private final String SQUARE_LOAD_REGEX = 
                  "^(?<row>\d*),(?<col>\d*):(?<mine>true|false)-(?<status>\w)$";

    public void save(PrintWriter writer, Board board) {

    }

    public Board load(BufferedReader reader) {
        // Implement
        return null;
    }
}
  • 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-04T20:29:45+00:00Added an answer on June 4, 2026 at 8:29 pm

    I’m still not sure what you mean, but need formatting, so answer instead of comment.

    First of all, the names are almost completely unrelated–related them somehow.

    SQUARE_DATA_STORE
    SQUARE_DATA_REGEX
    

    Second, there’s no point in differentiating the “style” of the saved data if there’s only a single BoardPersistenceUtility–if there were multiple formats then that information would be captured in a persistence utility subclass, like SquareFormatPersister or something.

    Third, according to your text, one string is where the data will actually be stored. The other is a regular expression. The two will, in this case, never be “visually similar”–regular expressions of any complexity will never (much) look like the strings they can represent. (In this case, we have no clue, because we don’t know what the board data can look like, of course.)

    If your code is so non-self-explanatory that the reader can’t figure out the two fields are related through via your comments and your code, something has gone horribly wrong. I’m having a hard time imagining this code is so overwhelmingly complex that their relationship cannot be trivially communicated.


    Edit after update

    The answer is still no.

    You could use a templating mechanism to provide names for the fields, similar to those used in your regex. This might also make the code a bit more self-explanatory as you’d fill the template context with named values (like “row” or “col”).

    You could use a real parser/generator, but the complexity there is a bit too much.

    You could use a DSL (internal using Groovy, JRuby, JavaScript, etc. or external, which brings us back to parsing) and write chunks of the code that way.

    IMO you’re over-thinking, and over-estimating perceived complexity: except possibly for the templating solution, which IMO is likely over-engineering for the level of difficulty, you’d be far better off writing one or two sentences, which should be more than enough to relate the “fields” of the load and save formats.

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

Sidebar

Related Questions

I am making a small program that reads a text file and stores it
I'm making a small tool for checking the material number from the XML file.
Making a small WCF test program which is based on a Store that has
I am making a small typing test in C# in which the program asks
I am currently making a small test program for simple file checking. The program
I'm making a small media player where I add mediafiles to a listbox which
I'm making a small program to place orders on our supplier's web site. I
I'm making small module/plugin for my future CMS/Framework. I wanted it from begining to
I am making a small program to draw segments in python using wxpython and
i am making a small project which will be incorporated into larger project. basically

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.