Java: nice example for the Builder pattern

/*
 * Copyright 2014 Patrick Ott .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.professional_webworkx.blog.innerclassbuilder;

import java.awt.Color;

/**
 *
 * @author Patrick Ott 
 */
public class App {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        Shape build = Shape.builder().opacity(0.7).build();
        Rectangle rectangle = Rectangle.builder().opacity(0.4).color(Color.yellow).height(3.5).build();
    }
    
}

Shape class

package de.professional_webworkx.blog.innerclassbuilder;

/**
 *
 * @author Patrick Ott 
 * @version 1.0
 */
public class Shape {

    protected final double opacity;
    
    public static abstract class Builder> {
        
        private double opacity;
        
        protected abstract T self();
        
        public T opacity(final double opacity) {
            this.opacity = opacity;
            return self();
        }
        
        public Shape build() {
            return new Shape(this);
        }
    }
    
    private static class Builder2 extends Builder {

        @Override
        protected Builder2 self() {
            return this;
        }
    
        
    }
    
    public static Builder builder() {
        return new Builder2();
    }
    
    protected Shape(final Builder builder) {
        this.opacity = builder.opacity;
    }
}

Rectangle class

/*
 * Copyright 2014 Patrick Ott .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.professional_webworkx.blog.innerclassbuilder;

import java.awt.Color;

/**
 *
 * @author Patrick Ott 
 * @version 1.0
 */
public class Rectangle extends Shape {

    protected final double height;
    protected final Color color;
    
    public static abstract class Builder> extends Shape.Builder {
        
        private double height;
        private Color color;
        
        protected abstract T self();
        
        public T height(final double height) {
            this.height = height;
            return self();
        }
        
        public T color(final Color color) {
            this.color = color;
            return self();
        }
        
        public Rectangle build() {
            return new Rectangle(this);
        }
    }
    
    private static class Builder2 extends Builder {

        @Override
        protected Builder2 self() {
            return this;
        }
        
    }
    public static Builder builder() {
        return new Builder2();
    }
    
    protected Rectangle(final Builder builder) {
        super(builder);
        this.height = builder.height;
        this.color  = builder.color;
    }
    
}