0

I would like to use a builder pattern to create an unknown object

how to do that?

my code like this:

public abstract class abstractA<T extends GameApplication<T>>{

public static class Builder<T> {

    private JPanel panel = new JPanel();
    private JFrame frame = new JFrame();

    private int height = 0, width = 0;
    private int x = 0,y = 0;
    private Color backgroundColor = Color.BLUE;

    public Builder setFrameHeightWidth(int height, int weight) {
        this.height = height;
        this.width = weight;
        return this;
    }

    public Builder setLocation(int x, int y) {
        this.x = x;
        this.y = y;
        return this;
    }

    public Builder setbackground(Color color) {
        this.backgroundColor = color;
        return this;
    }

    public T Build(){
        //error here
        return new T ;
    }

}

I want to use it like this:

class RealA extends abstractA{


public static void main(String[] argv){
    RealA a = abstractA.builder
                .setLocation(100,200)
                .setFrameHeightWidth(500,600)
                .build();
}

}

and I can't create a generics object, but I need this. How to do that?

Shawn Plus
  • 457
  • 1
  • 6
  • 15
  • 4
    You can't. Because of type-erasure, the actual type of `T` is unknown at runtime. – Andreas Apr 18 '19 at 15:58
  • How would you even know that `T` has the 5 properties your builder has gathered. Re-think what you're trying to do. – Andreas Apr 18 '19 at 15:59

1 Answers1

0

You can sort of do (something like) this if you let the builder know the kind of thing it is building (by passing it a class) and then get it to create the instance using reflection (e.g. the classes newInstance method).

This assumes all sub classes have a zero-argument constructor. (It can be modified to use a constructor with arguments, but each sub-class would need a constructor with the same signature)

For example...

public class Stack {

    static abstract class Common {
        protected String name;

        public void setName(String name) {
            this.name = name;
        }

        public abstract void doSomething();
    }

    static class Solid1 extends Common {
        @Override
        public void doSomething() {
            System.out.println("Solid1's implementation: name=" + name);
        }
    }

    static class Solid2 extends Common {
        @Override
        public void doSomething() {
            System.out.println("Solid2's implementation: name=" + name);
        }
    }

    static class Builder<T extends Common> {
        private final Class<T> clazz;
        private String name;

        public Builder(Class<T> clazz) {
            this.clazz = clazz;
        }

        public Builder<T> setName(String name) {
            this.name = name;
            return this;
        }

        public T build() {
            T t;
            try {
                t = clazz.newInstance();
            } catch (Exception e) {
                throw new RuntimeException("Bad things have happened!");
            }
            t.setName(name);
            return t;
        }
    }

    public static void main(String[] args) {
        Solid1 solid1 = new Builder<>(Solid1.class).setName("[NAME]").build();
        Solid2 solid2 = new Builder<>(Solid2.class).setName("[NAME]").build();

        solid1.doSomething();
        solid2.doSomething();
    }
}

Output...

Solid1's implementation: name=[NAME]
Solid2's implementation: name=[NAME]

Not sure how useful this is...

BretC
  • 4,141
  • 13
  • 22