I am working on a tool that scrubs Do-Not-Call (DNC) phone records from a skip-traced CSV file.
I have this PhoneModel
for child phone entries for each row. It consist of the following fields:
isDNC
score
(How accurate is the phone number for that record)phoneType
(Landline, Mobile, …)phoneNumber
date
(last updated date)
I have code for creating the child phone models from a MultiValuedMap
of rawPhoneData
:
public static List<PhoneModel> ExtractFromRawPhoneData(MultiValuedMap<String,Object> rawPhoneData) {
List<PhoneModel> phoneModels = [];
PhoneModel model = new PhoneModel();
List<String> sortedMapKeys = rawPhoneData.keySet()
// SOURCE: Phind AI
.sort { String a, String b ->
int aNum = NumberUtils.ExtractNumber(a);
int bNum = NumberUtils.ExtractNumber(b);
return aNum <=> bNum ?: a <=> b
}
sortedMapKeys.eachWithIndex({ String key, int idx ->
model = this.BuildPhoneModel(model, rawPhoneData, key);
if ((idx < sortedMapKeys.size() - 1) &&
(NumberUtils.ExtractNumber(sortedMapKeys[idx + 1]) == NumberUtils.ExtractNumber(sortedMapKeys[idx])))
return;
if (!StringUtils.IsNullOrEmpty(model.phoneNumber))
phoneModels.add(model);
model = new PhoneModel();
})
return phoneModels;
}
private static PhoneModel BuildPhoneModel(PhoneModel originalModel, MultiValuedMap rawPhoneData, String key) {
String rawValue = rawPhoneData.get(key)[0];
if (key.contains(Constants.DncKeyPart)) {
originalModel.isDNC = new YesNoBooleanConverter().convertStringToBoolean(rawValue);
return originalModel;
}
if (key.contains(Constants.ScoreKeyPart)) {
originalModel.score = NumberUtils.ParseInt(rawValue);
return originalModel;
}
if (key.contains(Constants.TypeKeyPart)) {
if (!StringUtils.IsNullOrEmpty(rawValue))
originalModel.phoneType = PhoneTypes.FromTextValue(rawValue);
return originalModel;
}
if (key.contains(Constants.NumberKeyPart)) {
originalModel.phoneNumber = rawValue;
return originalModel;
}
if (key.contains(Constants.DateKeyPart)) {
if (!StringUtils.IsNullOrEmpty(rawValue))
originalModel.date = DateUtils.ParseDateTime(rawValue);
return originalModel;
}
throw new IllegalArgumentException("Unrecognized key '${key}'");
}
My question here is thus:
Is my approach to build the PhoneModel
part-by-part, using dictionary, an implementation of the builder design pattern?
4
The builder pattern uses a builder class, which with one final call gives you a fully built object. The implication of this is that the type system prevents un- or partially constructed (built) objects — at any and every point or place you have a built object, it is 100% complete and ready to use. (Partially built objects are represented by the instance of the builder class.)
Whereas your approach builds the object in an incremental manner, meaning that you are forgoing the type system support for definitely constructed objects that the builder pattern offers.
As an aside it is curios to use string.contains
for key matching, though maybe I misunderstand the Groovy method.
It is also an odd choice to take a dictionary as parameter, and then also a key so as to handle only one item at a time therein. Why not leave the “dictionary.get()” to the caller and pass both key and value? That way the function wouldn’t rely on the key-value store.
1
Your approach to constructing the PhoneModel
from the rawPhoneData
dictionary does share some similarities with the builder design pattern, but there are some key differences.
Builder Design Pattern:
The builder design pattern is used to construct a complex object step by step, and the final step returns the resulting object. The pattern is recognized by:
- A separate
Builder
class, which provides methods to configure the
product. - A method for retrieving the resulting product.
- The pattern is particularly useful when an object needs to be created with many optional components or configurations.
Your Implementation:
- You’re using static methods within the same class to build the
PhoneModel
. - You’re building the
PhoneModel
part-by-part based on the keys present in therawPhoneData
dictionary. - The construction is tightly coupled with the structure and naming conventions of the
rawPhoneData
dictionary.
While your approach does involve constructing an object piece-by-piece based on input data (which is reminiscent of the builder pattern), it doesn’t fully encapsulate the builder pattern’s separation of concerns and flexibility. In a classic builder pattern, the construction process is abstracted away from the representation of the data. Here, your construction process is directly tied to the structure of the rawPhoneData
.
So, to answer your question: Your approach has some elements of the builder design pattern, but it’s not a strict implementation of it. If you wanted to implement the builder pattern more closely, you might consider creating a separate PhoneModelBuilder
class that provides methods for setting each attribute of the PhoneModel
and a final method to return the constructed PhoneModel
.