Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9316973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:45:41+00:00 2026-06-19T02:45:41+00:00

Suppose a Car model object (as case class ), immutable and created using builder

  • 0

Suppose a Car model object (as case class), immutable and created using builder pattern. (Builder pattern by Joshua Bloch).

Its build method calls a CarValidator object in order to allow creation or not of the concerned Car. Otherwise, it throws an IllegalStateException with unexpected fields precised.

Thus, no one could create a stale or invalid Car at any time at Runtime, great!

Suppose now a web form to create a Car. Play’s controller would contain this form mapping:

val carForm = Form(               //this is a conceptual sample
    mapping(
      "brand" -> nonEmptyText,
      "description" -> nonEmptyText,
      "maxSpeed" -> number
      "complexElement" -> number.verifying(........) //redundant validation here
    )(Car.apply)(Car.unapply)
  )   

In this example, there are some basics fields, but imagine more complex fields demanding complex business validations like the complexeElement here.

I really have the feeling that I would easily break the DRY(Don’t Repeat Yourself).

Indeed, whatever the form validation would bring, this would be already provided by my Car builder’s validator, since validation on model is THE most important validation place and shouldn’t depend on anything else.

I imagine a solution with a Helper class near my Controller dealing with the same validator object that my builder uses. However, it forces me to get all validation methods public in order to be called independently at any validation step of my web form (like in the code snippet above).

What would be a good practice to keep this builder principle while avoiding breaking DRY?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:45:42+00:00Added an answer on June 19, 2026 at 2:45 am

    If you want to keep the builder pattern, you should not have Form create instances. The form should just make sure the information that comes in is of the correct type. The form can not create the final Car because it does not know the rules to make a Car, the builder does.

    So I would say you let the form put stuff into an intermediary object (a tuple or PossibleCar case class) and build your Car (using the builder) with that object.

    There is another route available, but that means that you must create (possibly complex) structures that let’s you adapt the different types of validation. Both the builder and form can then use these validations (with the help of adapters) to create valid cars. I don’t know enough about the situation you’re in to give you advise on which route to take.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's supposed I created two models: class Car(models.Model): name = models.CharField(max_length=50) size = models.IntegerField()
Suppose I have a class hierarchy as follow: class Vehicle; class Car extends Vehicle;
Suppose I have a static method of my class that returns an object of
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have this code: Query<Car> q = ofy().load().type(Car.class); for (Car car : q)
if I have a ActiveRecord model: class Car < ActiveRecord::Base belongs_to :store end In,
For example suppose I have a Car object and I want my array to
Suppose you have the following class: class Car : IPainting { ... } Then
suppose i have a class engin and i inherit a class car from engin
Suppose that I have a list of cars : public class Car { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.