Is the process of building a model from a dictionary necessarily the builder pattern?

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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}'");
}
</code>
<code> 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}'"); } </code>
    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:

  1. A separate Builder class, which provides methods to configure the
    product.
  2. A method for retrieving the resulting product.
  3. The pattern is particularly useful when an object needs to be created with many optional components or configurations.

Your Implementation:

  1. You’re using static methods within the same class to build the PhoneModel.
  2. You’re building the PhoneModel part-by-part based on the keys present in the rawPhoneData dictionary.
  3. 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật