CHAPTER 4 CACHE CONTROLLER PATTERN 103 public (Web hosting resellers)
Wednesday, August 22nd, 2007CHAPTER 4 CACHE CONTROLLER PATTERN 103 public class LibrarianBuilder { public static Librarian create(String rootPath) throws InstantiationException { LibrarianSaveToFile.setRootPath(rootPath); return new LibrarianSaveToFile(new LibrarianSaveToStorage()); } } The Builder pattern is an extension of the Factory pattern. It is used to instantiate multiple instances of different types that are arranged in a specific configuration. In the case of our example class LibrarianBuilder, that would mean assigning the root directory by using the method setRootPath, instantiating LibrarianSaveToFile, instantiating LibrarianSaveToStorage, and wiring the two Librarian instances together. The returned Librarian instances would appear to the caller to be a single Librarian instance. However, when a Librarian method is called, two different instances are called. Implementing Static HTTP Validation The last step to implementing static HTTP validation is to put the entire solution together to build a web application. The following example uses Java servlets, but other implementations such as ASP.NET could have easily been used: public class LibrarianServlet extends HttpServlet { protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException { if(req.getContentType().compareTo( . “application/x-www-form-urlencoded”) == 0) { String operation = req.getParameter(”operation”); if(operation != null && . operation.compareTo(”addBook”) == 0) { Librarian librarian = LibrarianBuilder.create( . getServletContext().getInitParameter(”generatepath”)); try { Book book = new Book(); String isbn = req.getParameter(”isbn”); if(isbn != null) { try { book = librarian.checkOutBook(isbn); } catch(Exception ex) { book.setISBN(isbn); } } String author = req.getParameter(”author”); if(author != null) { book.setAuthor(author); }
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.