arrays - Java, singleton elements modification, software patterns -
short question. gets answered pretty fast. lets have singleton this:
package main.library; public enum librarysingleton { instance(new string[][]{}); final string[][] bookstore; private librarysingleton(string[][] bookstore){ this.bookstore = bookstore; } }
and book class 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 use singleton array holding books. how can access books, throw them array (singleton), or remove them array (singleton)? in case singleton should written differently, please correct me, , explain why wrong, i'm not "premium" java yet.
really hope guys going answer me on that. questions please.
if want singleton, 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 program has multiple threads. , if program runs in j2ee environment, complex classloading occurs, need different pattern.
Comments
Post a Comment