erma-core team mailing list archive
-
erma-core team
-
Mailing list archive
-
Message #00019
ERMA and Builders...
I've never been a fan of having to use the "new" keyword with a
short-lived object that I won't be holding a reference to. I realize
this isn't the intended use case for EventMonitors (folks should
subclass it), but I don't think many people do that.
I believe a lot of folks will just do...
new EventMonitor("blah").fire();
Which is fine really.
One idea I came up with while day dreaming yesterday was to have a
builder for EventMonitors...
public class EventMonitorBuilder {
private final EventMonitor eventMonitor;
public static EventMonitorBuilder newEventMonitor(String name) {
return new EventMonitorBuilder(new EventMonitor(name));
}
public static void fireEventMonitor(String name) {
newEventMonitor(name).fire();
}
private EventMonitorBuilder(EventMonitor eventMonitor) {
this.eventMonitor = eventMonitor;
}
public EventMonitorBuilder addAttribute(String name, Object value) {
eventMonitor.set(name, value);
return this;
}
public EventMonitor fire() {
eventMonitor.fire();
return eventMonitor;
}
}
This would allow us to write sexy code like this in Java...
(Note that my examples rely on Java 5 for boxing and static imports)
newEventMonitor("blah")
.addAttribute("test", 1)
.addAttribute("good", "stuff")
.fire();
Or even the one-line alternative...
fireEventMonitor("blah");
Another thing I'd like to do is add support for Groovy. Where we can
create a Builder Groovy style.
http://groovy.codehaus.org/Builders
(I'm sure Doug is thinking of doing sexy stuff like this in ERMArb)
This is pseudo code to show how the builder pattern would work...
def montior = new GroovyEventMonitorBuilder()
monitor.MonitorNameGoesHere {
AttributeName(AttributeValue)
}
No need for a "fire()" method as it would be implied.
So for example...
def montior = new GroovyEventMonitorBuilder()
monitor.authenticationFailure {
userName("rkrueger")
}
Just wanted to get these thoughts out of my head :)
Follow ups