john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

BuilderPattern pizza

public class Pizza
{
    private int size;
    private String cheese;
    private String meat;

    public static class Builder
    {
        private final int size;
        private String cheese;
        private String meat;

        public Builder( int size )
        {
            this.size = size;
        }

        public Builder cheese( String value )
        {
            this.cheese = value;
            return this;
        }

        public Builder meat( String value )
        {
            this.meat = value;
            return this;
        }

        public Pizza build()
        {
            return new Pizza( this );
        }
    }

    private Pizza( Builder builder )
    {
        this.size = builder.size;
        this.cheese = builder.cheese;
        this.meat = builder.meat;
    }

    public static void main( String[] args )
    {
        Pizza pizza = new Pizza.Builder( 14 ).cheese( "mozarella" ).meat( "sausage" ).build();
    }

} // end class

  • « HttpURLConnection checkurl
  • validation constraintviolationexception notnull example »

Published

Jun 19, 2012

Category

java

~85 words

Tags

  • builder 1
  • builderpattern 1
  • java 252
  • patterns 1
  • pizza 1