I am not sure about you guys, but at least for Java, there are certainly block of codes I like to keep around for later use. I have come across some useful ones (with the concept) which I will cover here, mainly for the reference.
1. DAO - Data Access Object
DAO is an abstraction where it provides an interface to data objects such as SQL/NoSQL Database, CSV, Spreadsheet, etc. It could literally be anything to store values. If you have typical structures to store some item. That could also be a DAO.
Simple codes that show how it could handle various DAO Object in maybe a day factory which constructs and provide you objects needed, Example of using Factory pattern:
I would make the DAO implements a DatabaseAccessObject interface which provides basic operations such as OpenConnection, CloseConnection, getRows, writeRows, getColumns, writeColumns, checkConnection, etc. That would be common in each DAO object.
2. POJO - Plain Old Java Object
There are many ways to create a Java Object from the different set of documents. You can read/write from/to XML and JSON are the 2 great examples. However, you use the term for making anything into Java Objects such as Databases-POJO (Hibernate). I will just cover the 2 representations, XML, and JSON here.
There are many different ways to make a POJO out of XML. Using @XStream is one of the ways. Here is a very simple usage. Say your XML look like,
Then you could convert this to a POJO by:
Another way would be using a sax and parse the raw XML. You can do something like getXMLDeclCharset, parseThisAsSuch using a DefaultHandler. Here is an example:
As you can see, using SAXParser would add granularity to how the document is being parsed. This granularity could save you a bit of coding and time. For a simple task, I found myself easier to use just XStream as it is much flexible going back and forth with XML to POJO in annotated way. However, the SAX way is much more flexible in terms of what you wish to do with the raw XML.
There are also numerous ways to make a POJO out of JSON. A simple way is to use Jackson 2 (com.fasterxml.jackson.databind). Here is to read from JSON.
However, I personally use Gson from Google most of the time. It’s similar logic as above. Let’s take a look at a Staff JSON:
Now in order to make a POJO, all you need is below:
3. Exception Template
I think it is good to divide Exceptions into different sub-exceptions. We looked at DAO above, here would be an exception caused by DAO.
Templates are similar for any other exceptions, however, it gives names to what kind of exception it was and what it was for.
4. Copying a file to another location
Here is a way to copy a file to another file.
5. New list using static factories
Unless you are using a Guava libraries, I suggest you make your own static factory that handles these data structures with a name like below example:
6. Way to initialize common data structures
Initializing a List:
Or since you are defining statically, you can simply use the static block to initialize.
There are so many useful convenient functions in Java out there such as using annotations, interfaces, or libraries. However, those items deserve a separate blog on their own as there are many details to talk about those. Above items are really just referenced to me and people who might also find them useful. They often times come handy as they did for me. There are so many other useful design patterns I included as part of Algorithms Playground Project. Go to the linked URL and you will see a bunch of design patterns on all categorized with its own unit tests to show you a better understanding on each. Please step through the examples to understand how each of them works step by step and sees differences from another.
Share this post
I am a passionate software developer working in Downtown, Vancouver. I strongly believe in art of algorithms and together with it to write clean and efficient software to build awesome products. If you would like to connect with me, choose one from below options :) You can also send me an email at