EDIT: I am not worried about being called in the wrong order since this is enforced through using multiple interfaces, I am just worried about the terminal method getting called at all.
I am using a builder pattern to create permissions in our system. I chose a builder pattern because security is so important in our product (it involves minors so COPPA et al), I felt it was imperative that permissions be readable, and felt that the readability was of the utmost importance (i.e. use a fluent-style builder pattern rather than a single function with 6 values).
The code looks like this:
permissionManager.grantUser( userId ).permissionTo( Right.READ ).item( docId ).asOf( new Date() );
The methods populate a private backing bean, that upon having the terminal method (i.e. asOf ) commit the permission to the database; if that method does not get called nothing happens. Occasionally developers will forget to call the terminal method, which does not cause a compiler error and is easy to miss on a quick reading/skimming of the code.
What could I do to prevent this problem? I would not like to return a Permission object that needs to get saved since that introduces more noise and makes permission code harder to read, follow, track, and understand.
I have thought about putting a flag on the backing which gets marked by the terminal command. Then, check the flag in the finalize
method and write to the log if the object was created without persisting. (I know that finalize
is not guaranteed to run, but it's the best I can think of.)
build()
. – Baxyfinalize
approach (or the equivalentPhantomReference
approach) should be a best-effort error detection mechanism only, if you implement it. As you said: it does not usually guarantee anything, but it can help you debug the problem. Also: you could keep grab a stack trace every time a non-terminal method is called and print that when the finalizer finds an un-applied builder. This way you'll know where the problem occured. – Osrock