Builder

From APIDesign

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
-
[[APIDesignPatterns:Builder|Builder pattern]] is composed from inital configuration [[APIDesignPatterns:CumulativeFactory|cumulative factory]] pattern phase followed by final call to a [[APIDesignPatterns:Factory|factory method]]. It is very useful, especially in cases where the intermediate states created by the use of [[APIDesignPatterns:CumulativeFactory|cumulative factory]] are incomplete and cannot be used by itself. If an additional action (write to disk, establish connnection, etc.) is needed, it makes sense to finish the API calls by final use of [[APIDesignPatterns:Factory|factory method]].
+
[[APIDesignPatterns:Builder|Builder pattern]] is an alternative to [[APIDesignPatterns:CumulativeFactory|cumulative factory]] pattern. While the [[APIDesignPatterns:CumulativeFactory|cumulative factory]] relies fully on immutability, the initial phase of a [[APIDesignPatterns:Builder|Builder]] empowers mutability. It is then followed by final call to a [[APIDesignPatterns:Factory|factory method]] to convert the temporarily mutable object into immutable one. It is very useful, especially in cases where the intermediate states created by the use of [[APIDesignPatterns:CumulativeFactory|cumulative factory]] are incomplete and cannot be used by itself, or when mutability offers some obvious benefits. For example avoiding additional temporary actions (write to disk, establish connnection, etc.). Then the additional step of ''finishing'' the [[API]] calls by final use of [[APIDesignPatterns:Factory|factory method]] makes a lot of sense.
Imagine that we have an API representing a server...
Imagine that we have an API representing a server...

Revision as of 09:25, 25 August 2009

Builder pattern is an alternative to cumulative factory pattern. While the cumulative factory relies fully on immutability, the initial phase of a Builder empowers mutability. It is then followed by final call to a factory method to convert the temporarily mutable object into immutable one. It is very useful, especially in cases where the intermediate states created by the use of cumulative factory are incomplete and cannot be used by itself, or when mutability offers some obvious benefits. For example avoiding additional temporary actions (write to disk, establish connnection, etc.). Then the additional step of finishing the API calls by final use of factory method makes a lot of sense.

Imagine that we have an API representing a server...

does not exists: aserverinfo.builder.api

...and we need a way to establish connection to it. For that purpose we can define additional class that has no getters, no way to obtain its state by external API clients. Instead it offers users of its API only setters (or in this case cumulative factory methods) allowing the clients to change enough properties before final creation of the connector is done:

does not exists: aserverinfo.builder.factory

The API use is not complicated over the case of cumulative factory. Moreover the writer of the API can simplify internals of his ServerInfo class, as the class is know to never connect directly to any server. The class is just a recipe for creation of the connection:

does not exists: ServerConnector.builder.creation.verbose

The previous code snippet shows the verbose way to use the API. The shorter one-liner is also possible:

does not exists: ServerConnector.builder.creation

Builder pattern evolution characteristics are similar to the ones of the cumulative factory pattern. Yet the clear separation between configuration and functional part of the API may, in some situations, simplify internal implementation and prevent misuses (calling a setter after making the connection) of the class.

<comments/>

Personal tools
buy