patternjavaMinor
Builder pattern for users in document database
Viewed 0 times
builderdatabasefordocumentuserspattern
Problem
I'm attempting to make a builder pattern that makes sense and is practical for storing user data in a flexible way in a document database. The DB I chose is MongoDB, and I'm using its Java API throughout. I plan on replicating this kind of pattern once I have something that is decent.
This works, but feels a bit clunky. I would like to hear from others who have implemented builder patterns before and see what their thoughts are. Also, all other advice is welcome on all parts of the code. Here is a link to the primary class from the API that I'm using: BasicDBObjectBuilder
My tests explain fairly well how the class works and how it is used. I have also added documentation in the main class to explain as good as possible. This is Groovy, but a lot of it is actually from Java, aside from the more "sugary" syntax. I'm not using much Groovy "magic" as it is. I added a small example usage to illustrate.
Example usage:
Prints this:
UserDocumentBuilderTest.groovy
```
import com.mongodb.BasicDBObjectBuilder
import com.mongodb.DBObject
import org.junit.Test
import org.junit.Before
import java.security.MessageDigest
/**
* These tests are focused on the UserDocumentBuilder class.
*/
class UserDocumentBuilderTest {
This works, but feels a bit clunky. I would like to hear from others who have implemented builder patterns before and see what their thoughts are. Also, all other advice is welcome on all parts of the code. Here is a link to the primary class from the API that I'm using: BasicDBObjectBuilder
My tests explain fairly well how the class works and how it is used. I have also added documentation in the main class to explain as good as possible. This is Groovy, but a lot of it is actually from Java, aside from the more "sugary" syntax. I'm not using much Groovy "magic" as it is. I added a small example usage to illustrate.
Example usage:
String username = "foo"
String password = "bar"
Map details = [
"email": "hello@world.com",
"favorite_number": 42,
"favorite_colors": ["green", "blue"]
]
UserDocumentBuilder builder = new UserDocumentBuilder(username, password)
BasicDBObjectBuilder builderWithDetails = builder.begin()
builder.addDetails(builderWithDetails, details)
// get a DBObject which can then be inserted into MongoDB:
DBObject userDocument = builderWithDetails.get()
println userDocument.toString()Prints this:
{ "user_name" : "foo" , "password_hash" : "37b51d194a7513e45b56f6524f2d51f2" , "date_created" : { "$date" : "2015-11-01T16:55:56.978Z"} , "details" : { "email" : "hello@world.com" , "favorite_number" : 42 , "favorite_colors" : [ "green" , "blue"]}}UserDocumentBuilderTest.groovy
```
import com.mongodb.BasicDBObjectBuilder
import com.mongodb.DBObject
import org.junit.Test
import org.junit.Before
import java.security.MessageDigest
/**
* These tests are focused on the UserDocumentBuilder class.
*/
class UserDocumentBuilderTest {
Solution
-
Neither MongoDB's
-
Your name
It represents a builder for a user, not a builder for a document of a user, doesn't it? Hence, I'd rather call it
-
AFAICS the only added value to
Why don't you simply:
and just overload
-
You supply a
and you apply another hash algorithm to this already-hash then? Why?
-
MongoDB's
Neither MongoDB's
BasicDBObjectBuilder nor your UserDocumentBuilder is a Builder according to the pattern of the GoF.-
Your name
UserDocumentBuilder is somehow confusing. Especially when you then define:BasicDBObjectBuilder userDocumentBuilder = new BasicDBObjectBuilder()It represents a builder for a user, not a builder for a document of a user, doesn't it? Hence, I'd rather call it
UserDBObjectBuilder.-
AFAICS the only added value to
BasicDBObjectBuilder is the addDetails(...) method to add keys/values from a Map rather than single key/value pairs only.Why don't you simply:
public UserDBObjectBuilder extends BasicDBObjectBuilderand just overload
add(...) with add(Map details) therein? -
You supply a
passwordHash to:public UserDocumentBuilder(..., String passwordHash, ...)and you apply another hash algorithm to this already-hash then? Why?
-
MongoDB's
BasicDBObjectBuilder implementation, apart from the missing method descriptions, is questionable on its own:- What is the difference between
add(...)andappend(...)?
- What does
push(String key)do? Adding akeywith anullvalue associated to it?
- What if I invoke
BasicDBObjectBuilder.start("key", "value").start()? Is the building process started over again by eliminating the values given at the firststart(...)? → Thesestart()methods should better be different constructors.
Code Snippets
BasicDBObjectBuilder userDocumentBuilder = new BasicDBObjectBuilder()public UserDBObjectBuilder extends BasicDBObjectBuilderpublic UserDocumentBuilder(..., String passwordHash, ...)Context
StackExchange Code Review Q#109460, answer score: 6
Revisions (0)
No revisions yet.