Spring: Using builder pattern to create a bean

You may try to implement FactoryBean interface: public class HttpFactoryBean implements FactoryBean<HttpClient>{ private String host; private int port; public HttpClient getObject() throws Exception { return new StdHttpClient.Builder() .host(host) .port(port) .build(); } public Class<? extends HttpClient> getObjectType() { return StdHttpClient.class; } public boolean isSingleton() { return true; } public void setHost(String host) { this.host = host; … Read more

Java generics + Builder pattern

You were close: Foo.Builder.<Bar> start().setCount(1).setKey(bar).build(); Cheers! 🙂 P.S. If the compiler can’t infer the type parameter of the method on its own, you can force it by calling obj.<Type> method(…) . P.P.S you might want to use: public Foo<K2> build() { return new Foo<K2>(this); } Avoid using raw types.

Builder Vs Decorator pattern [closed]

From wikipedia’s decorator pattern article: In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically. There’s no need to add toppings to a Pizza after it has been fully constructed. You don’t eat half a pizza and then add another topping to it. … Read more

Builder design pattern: Why do we need a Director?

The core portion of the Builder pattern concerns the Abstract Builder and its subclasses (concrete builders). According to GoF’s Design Patterns, director simply “notifies the builder whenever a part of the product should be built”, which can be perfectly done by the client. The StringBuilder class in the Java API is an example of a … Read more

Conditional Builder Method Chaining Fluent Interface

What I’d do is have NinjaBuilder keep the operations as a list of delegates, rather than applying them, and only apply them when .Build is called. This would allow you to make them conditional: public class NinjaBuilder { List<Action<Ninja>> builderActions = new List<Action<Ninja>>(); public Ninja Build() { var ninja = new Ninja(); builderActions.ForEach(ba => ba(ninja)); … Read more

How to access attributes using Nokogiri

Using Nokogiri::XML::Reader works for your example, but probably isn’t the full answer you are looking for (Note that there is no attributes method for Builder). reader = Nokogiri::XML::Reader(builder.to_xml) reader.read #Moves to next node in document reader.attribute(“messageId”) Note that if you issued reader.read again and then tried reader.attribute(“messageId”) the result will be nil since the current … Read more

Builder Pattern and Inheritance

This is certainly possible with the recursive bound, but the subtype builders need to also be generic, and you need a few interim abstract classes. It’s a little bit cumbersome, but it’s still easier than the non-generic version. /** * Extend this for Mammal subtype builders. */ abstract class GenericMammalBuilder<B extends GenericMammalBuilder<B>> { String sex; … Read more