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 7399575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:04:41+00:00 2026-05-29T04:04:41+00:00

Suppose you create a class names Person using the builder pattern, and suppose the

  • 0

Suppose you create a class names Person using the builder pattern, and suppose the Builder class contains methods body(), head(), arms() and of course build() and you consider methods head() and build() obligatory for the user of this class.

We would like to somehow mark these methods obligatory, if possible using annotations. If a user of this class tries to build a Person instance but forgot to call either of these methods, we would like to get some kind of warning – either from the java compiler, or maybe from Eclipse or Maven, which we use to build our projects – any of them would do.

Is it possible to do? Which way would you suggest?

  • 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-05-29T04:04:41+00:00Added an answer on May 29, 2026 at 4:04 am

    Here is an example with using different types to make some parts mandatory (it also makes the order you call the methods mandatory):

    package test;
    
    import test.StepOne.StepThree;
    import test.StepOne.StepTwo;
    import test.StepOne.LastStep;
    
    public class TestBuilder {
    
        public static void main(String[] args) {
    
            String person1 = PersonBuilder.newInstance().head("head").body("body").arm("arm").leg("leg").build();
    
            String person2 = PersonBuilder.newInstance().head("head").body("body").arm("arm").build();
    
        }
    
    }
    
    interface StepOne {
    
        // mandatory
        StepTwo head(String head);
    
        interface StepTwo {
            // mandatory
            StepThree body(String body);
        }
    
        interface StepThree {
            // mandatory
            LastStep arm(String arm);
        }
    
        // all methods in this interface are not mandatory
        interface LastStep {
            LastStep leg(String leg);
            String build();
        }
    
    }
    
    class PersonBuilder implements StepOne, StepTwo, StepThree, LastStep {
    
        String head;
        String body;
        String arm;
        String leg;
    
        static StepOne newInstance() {
            return new PersonBuilder();
        }
    
        private PersonBuilder() {
        }
    
    
    
        public StepTwo head(String head) {
            this.head = head;
            return this;
        }
    
        public LastStep arm(String arm) {
            this.arm = arm;
            return this;
        }
    
        public StepThree body(String body) {
            this.body = body;
            return this;
        }
    
        public LastStep leg(String leg) {
            this.leg = leg;
            return this;
        }
    
        public String build() {
            return head + body + arm + leg;
        }
    }
    

    Edit

    The OP was so impressed with this answer that he wrote it up fully in a blog. It’s such a clever take on the builder pattern that a full treatment deserves to be referenced here.

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

Sidebar

Related Questions

I'm trying to create a test class which organizes its test methods using inner
Suppose I am designing a class that can handle any database technology to create
Suppose we have a class. We create an object from the class and when
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Suppose I my project contains a Form and a Class. I would like to
Suppose i have a base class person. and i publically inherits a class student
Let's suppose I want to create a javascript class/object/function which have a method that
Let's suppose I define a domain class named Client in several datasources and create
Suppose you create a generic Object variable and assign it to a specific instance.
Suppose I create a very simple socket connection, how can one programatically: Find out

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.