'. '

ChameleonBuilder

From APIDesign

(Difference between revisions)
Jump to: navigation, search
(New page: Very nice attribute of a Builder pattern is the fact that it allows the users to associate the attributes one by one and doesn't force them to do so in a predefined order. Possibly it ...)
(Factory Method)
Line 3: Line 3:
=== Factory Method ===
=== Factory Method ===
-
[[TBD]]
+
Of course, one can use a [[factory]] method - e.g. let the builder be created only with necessary attributes. However this has the typical drawbacks to evolution of [[factory]] methods - the order of parameters is important, needs to be remembered and potentially number of overloaded methods grows. We used to have '''fromText''' method in {{truffle|com/oracle/truffle/api/source|Source}} class. One would use it as:
 +
 
 +
<source lang="java">
 +
Source src = Source.fromText("my.js", "my.js");
 +
</source>
 +
 
 +
Now, can you guess what of the parameters is name for the {{truffle|com/oracle/truffle/api/source|Source}} and which of them is the content? Hardly, I don't remember the order either. Now imagine we add also a MIME type:
 +
 
 +
<source lang="java">
 +
Source src = Source.fromText("my.js", "my.js", "text/javascript");
 +
</source>
 +
 
 +
Three subsequent {{JDK|java/lang|String}} arguments are really hard to use properly. Let's try to use the [[builder]] pattern, but make it ''unfinished'' first.
=== Unspecified Return Type ===
=== Unspecified Return Type ===

Revision as of 08:24, 16 June 2016

Very nice attribute of a Builder pattern is the fact that it allows the users to associate the attributes one by one and doesn't force them to do so in a predefined order. Possibly it allows the users to skip definition of some of the attributes and use defaults. However what can one do if certain attribute is essential - e.g. needs to be specified?

Factory Method

Of course, one can use a factory method - e.g. let the builder be created only with necessary attributes. However this has the typical drawbacks to evolution of factory methods - the order of parameters is important, needs to be remembered and potentially number of overloaded methods grows. We used to have fromText method in Source class. One would use it as:

Source src = Source.fromText("my.js", "my.js");

Now, can you guess what of the parameters is name for the Source and which of them is the content? Hardly, I don't remember the order either. Now imagine we add also a MIME type:

Source src = Source.fromText("my.js", "my.js", "text/javascript");

Three subsequent String arguments are really hard to use properly. Let's try to use the builder pattern, but make it unfinished first.

Unspecified Return Type

TBD

Personal tools
buy