2

short question. Hopefully it gets answered pretty fast. Lets say I have a singleton like this:

package main.library;

public enum LibrarySingleton {
    INSTANCE(new String[][]{});

    final String[][] bookStore;

    private LibrarySingleton(String[][] bookStore){
        this.bookStore = bookStore;
    }
}

and a Book class that holds 3 variables:

package main.library;

public class Book{
    String author;
    String title;
    int pages;

    @Override public String toString(){
        return ("|" + author + "|" + title + "|" + pages + "|");
    }

    public Book(){
        System.out.println("Error: No book information specified");
    }

    public Book(String author, String title, int pages){
        this.author = author;
        this.title = title;
        this.pages = pages;
    }

    public String getAuthor(){
        return author;
    }

    public String getTitle(){
        return title;
    }

    public int getPages(){
        return pages;
    }
}

I'm looking on how to use that singleton as an array holding books. How can I access the books, throw them into the array (singleton), or remove them from the array (singleton)? In case the singleton should be written differently, please correct me, and explain why is it wrong, as I'm not so "premium" with Java yet.

Really hope you guys are going to answer me on that. Just the questions please.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
qubits
  • 1,227
  • 3
  • 20
  • 50
  • 2
    Why are you having `String[][]` instead of `Book[]`? – Rohit Jain Aug 13 '13 at 20:56
  • 4
    This really seems to be an abuse (and mis-use) of the singleton pattern, and you should probably just have a normal `Library` class that holds the books... – assylias Aug 13 '13 at 20:57
  • 3
    If you're going to use enums as singletons, you're going to have a bad time. – Obicere Aug 13 '13 at 20:58
  • 2
    @Obicere it is a good practice to use an enum for singletons. – docDevil Aug 13 '13 at 21:01
  • 2
    @Obicere. Can you elaborate onto that statement? – Rohit Jain Aug 13 '13 at 21:01
  • If you want to add and remove `Books` why not use a [`List`](http://docs.oracle.com/javase/6/docs/api/java/util/List.html) – Java Devil Aug 13 '13 at 21:01
  • @Obicere you're mixing two things there. In this case, a singleton is not the best option. But if it were, an enum would be totally fine to implement that singleton. – Vincent van der Weele Aug 13 '13 at 21:05
  • @Obicere: please read this: http://javarevisited.blogspot.com/2012/07/why-enum-singleton-are-better-in-java.html – docDevil Aug 13 '13 at 21:06
  • 3
    @Obicere. [Have a look at this post](http://stackoverflow.com/q/70689/1679863) – Rohit Jain Aug 13 '13 at 21:06
  • I find it hard to believe enums are **that** much better. If they were, I believe Oracle would see fit that all the instances are changed. It seems like a complete hack-job and I would never condemn myself to such lowly standards to write such code. – Obicere Aug 13 '13 at 21:18
  • @Obicere Engineers involved in writing the JDK itself do recommend using an enum for singletons (e.g. Joshua Bloch). – assylias Aug 17 '13 at 21:45

2 Answers2

1

If you want singleton, you can use following approach:

public class Library {
   private static final Library instance = new Library();
   private List<Book> books = new ArrayList<Book>();

   public static Library getInstance() {
       return instance ;
   }

   public void add(Book book) { 
       books.add(book);
   }
}

Of course, add synchronization if your program has multiple threads. And if your program runs in J2EE environment, where complex classloading occurs, you need a different pattern.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • 1
    You should not use raw type `List`. – Rohit Jain Aug 13 '13 at 21:09
  • Theres no RMA or multithreading so its ok. I do have a question however: how would i reffer to the singleton later on in the main function? Lets say I want to use getAuthor() function for a book ("nice author","nice book","123") which is in the singleton?? – qubits Aug 13 '13 at 21:14
  • Library.getInstance().getAuthor("Gosling") – Leos Literak Aug 13 '13 at 21:18
  • Upvoted I do however get small errors: for: private List books = new ArrayList(); - arrayList cannot be resolved to a type, list cannot be resolved to a type. – qubits Aug 13 '13 at 21:21
  • add import java.util.*; to start of your file – Leos Literak Aug 13 '13 at 21:26
  • Ok, my bad, I forgot to import the right libs, thanks alot for the help. Upvoted ;) – qubits Aug 13 '13 at 21:26
0

If you initialize an empty array and make it final it will stay empty and you cannot reinitialize it. I guess what you need is an arraylist (list in general). That one you can initialize and add element to it.

docDevil
  • 72
  • 4