简体   繁体   中英

How do I make an array of data accessible by the entire system (JAVA)

In short:

On startup of a Java/Android application, consider an array of data being initialised with hard coded values. Then, during runtime, a class can read from that data. The exact elements from the array that it will want to access are dependent on User input at runtime.

In detail:

Consider a crossword application. From a main screen, you select the crossword that you want to play. A Crossword (class), creates multiple Questions (class). To create a Question, it is initialised with two strings: 'question' and 'answer'. In order for the Crossword to create all of it's Questions, it needs to know which Questions belong to it. When a User selects a Crossword from a list, it gets passed an ID which will map to (perhaps) an element index of a multidimensional array, whereby each outer array represents a set of crossword questions, and each inner array represents the questions within that crossword. Similar to below:

Array(
// crossword 1
[0] => Array(
    // question 1
    [0] => Array(
        [0] => 'question'
        [1] => 'answer'
    )
    // question 2
    [1] => Array(
        [0] => 'question'
        [1] => 'answer'
    )
)
// crossword 2
[1] => Array(
    // question 1
    [0] => Array(
        [0] => 'question'
        [1] => 'answer'
    )
    // question 2
    [1] => Array(
        [0] => 'question'
        [1] => 'answer'
    )       
)

)

OK, so User selects Crossword 1, I now create a Crossword object, create multiple Question objects by accessing index 1 of the global array. Something like:

Q1 = new Question(globalArr[1][0][0], globalArr[1][0][1]);
Q2 = new Question(globalArr[1][1][0], globalArr[1][1][1]);

My question:

I imagine (hope) there is a more elegant OOP solution to this. Assuming that there is, I would appreciate someone sharing their knowledge. Would a Singleton design come into play here?

The main goal here is making hard coded information about each question accessible to the entire application (or at least a class)

The generic (and mostly dirty) way of providing hard coded values in Java is to declare static fields or data structures in a class which are then made available by the classloader. This is how a singleton is implemented in most of the cases. But since you delegate the management of creating the single instance to your classloader(s) (there can be multiple classloaders in one VM) there is no guarantee that you will always be using the exact same instance of the static data. So this is not a good recipe to pass around data between multiple threads.

The right way to handle data/content in Android is probably to map your domain model (crosswords) to an implementation using the Android Content Provider API. You can also provide hardcoded values with that if you absolutely need to.

In general: model your domain with apropriate abstract types and not using arrays. That is definitely not the OO way of doing it.

Read this book: http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=sr_1_1?ie=UTF8&qid=1383562013&sr=8-1&keywords=domain+driven+design

How about:

  • define your main item crossWords as a Singleton Collection of CrossWord object
  • your CrossWord object contains a Collection of Question object
  • your Question object contains 2 fields: question and answer

You could use a CrossWord var to point the user selected CrossWord:

CrossWord selectedCW = crossWords.get(index);

To add Q&A could use:

Question q = new Question();
q.setQuestion("question");
q.setAnswer("answer");
selectedCW.add(q);

or by an overloaded constructor:

selectedCW.add(new Question("question","answer"));

Take a look to the Java Collection Framework to choose your best collection implementation.

You can use the Singleton Pattern and put this array in a class. http://en.wikipedia.org/wiki/Singleton_pattern

like

public class Question {
private Question() {}; // no one can create a instance
public final static Question INSTANCE = new Question();
private final Array[] array = ...

public getArray() {
return array;
}

For thread saveness read http://javarevisited.blogspot.de/2012/12/how-to-create-thread-safe-singleton-in-java-example.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM