concept builder in category griffon

appears as: builders, Builder, builder, builders, The builder
Griffon in Action

This is an excerpt from Manning's book Griffon in Action.

Java is a good language to develop applications, but it requires a steeper learning curve than other languages that can run on the JVM—languages that provide the same behavior in many respects, but in a more expressive manner. Griffon addresses this issue through the use of the Groovy language, builders, and plugins.

  • newInstance()—Perhaps the most important method that must be implemented, newInstance() is responsible for creating instances. You have access to the current context via the builder parameter, the node’s name via the name parameter, the node’s value (if any) via the value parameter, and any property that was set using Map syntax via the attributes parameter. When implementing this method, we recommend that you remove attribute keys for each value in the attributes map that was used to create the target instance; otherwise they’ll be set again by the method we’ll discuss next.
  • onHandleNodeAttributes()—This method is responsible for setting any remaining properties that may not have been handled when newInstance() was called, and sets them on the target instance. Note that it returns a Boolean value. If the return value is false, the builder will ignore any remaining properties that the factory did not process explicitly. If the return value is true, the builder will attempt to set any properties that are still left on the Map. Overriding this method can be useful in various circumstances, such as when a type conversion must be executed before setting a property’s value or when there are multiple setters for a particular property name.
  • This is basically what FactoryBuilderSupport does. Whenever a method is invoked and it isn’t found by regular means, methodMissing() kicks in. The builder will attempt to resolve the node name to a factory, and if a match is found it will then kick start the node build cycle. But if no match is found, you’ll get a MissingMethod-Exception as a result of the method call.

    Once a factory is resolved, the build cycle continues; the builder will call out specific methods defined internally. Some of those methods expose extension points via the closure delegates. Figure 12.2 shows the sequence of method calls exchanged between builder and factory in order to build a node.

    Figure 12.2. Builder and factory engaged in the node build cycle. Delegates are called on each phase that matches their name.

    Without further ado, these are the types of delegates you may add to a builder:

    12.1.3. Creating a builder

    This is perhaps the most thorough option you have at your disposal. Creating a builder means you have full control over which node factories get registered and how. You also have the option to register as many delegates as needed. This sounds similar to addons, doesn’t it? The main advantage of creating a builder is that you can distribute your builder to be used outside of a Griffon application. Why would you want to do that? Think of IDE integration for one, or the ability to use your builder with a simple Groovy script. As a matter of fact, this is how SwingXBuilder (and some of the official extensions you’ll see later in the chapter) came to be in the first place.

    Builders should extend from FactoryBuilderSupport. This ensures that the builder has all it needs to process factories and delegates, leaving you with the responsibility of adding as many factories as you like.

    Say you’d like to create a builder for a MyCustomComponent and other cool components you may have lying around. The following listing shows what such a builder could look like.

    Listing 12.3. Basic builder implementation
    class CoolBuilder extends FactoryBuilderSupport { CoolBuilder(boolean init = true) { super(init) } void registerCoolWidgets() { registerFactory('myCustomComp', new MyCustomComponentFactory()) registerFactory('pushButton', new PushButtonFactory()) } }

    Take note of the builder’s constructor. Every builder created in this way will automatically search for methods that follow a naming convention; the assumption is that these methods will register factories on the builder. We can appreciate the convention being applied to the registerCoolWidgets() method. This naming convention is as follows:

    void register<groupName>() { ... }

    This means that every method that starts with the word register, doesn’t return a value, and takes no arguments will be automatically called after the builder instance is created. If that method happens to make a call to registerFactory(), those node factories will be added to the builder. You can use this approach to call anything you’d like during node registration; for example, you can make calls to register any delegates needed by the builder.