how to new an unknown object in abstract class - java

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?

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...

Related

I'm trying to understand the Building Pattern and I have trouble with the Director

I try to code a builder pattern for my better understanding. Mostly I relied on GOF and wikipedia.
So my Object is a house with required attribute area and some optional attributes (like windows, doors, rooms etc.)
I will show you the code. Now, I'm not really sure if its correct and I think I don't have a director? I don't get in which cases you need one and how it works.
This is my class house and the innerclass HouseBuilder
public class House {
//required
private final String area;
//optional
private int windows;
private int doors;
private int rooms;
//constructor with HouseBuilder
private House(HouseBuilder builder) {
this.windows = builder.windows;
this.doors = builder.doors;
this.rooms = builder.rooms;
}
public static class HouseBuilder {
//required
private String area;
//optional
private int windows;
private int doors;
private int rooms;
//constructor with required attributes
HouseBuilder(String area) {
this.area = area;
}
//optional attributes
public HouseBuilder windows(int windows) {
this.windows = windows;
return this;
}
public HouseBuilder doors (int doors) {
this.doors = doors;
return this;
}
//function for building
public Housebuild() {
return new House(this);
}
}
Now, I just got a class demo where I can build a house like that:
House house = new House.HouseBuilder("Downtown")
.doors(3).windows(2).build();
But this is not a director like in the books. Is my idea even correct? And why is that better than just using setters?
Thanks!
Your example illustrates classic builder. Director is something like an abstract builder, and in practise it is rarely used because the client class can handle that perfectly well. Example of a director in your case would be:
public class House
{
public final String area;
public windows;
public int doors;
public int rooms;
}
interface HouseBuilder
{
public HouseBuilder area();
public HouseBuilder windows();
public HouseBuilder doorsors();
public HouseBuilder rooms();
public House build();
}
public static class DowntownHouseBuilder implements HouseBuilder
{
House downtownHouse = new House();
public HouseBuilder area()
{
downtownHouse.area = "Downtown";
}
public HouseBuilder windows()
{
downtownHouse.windows = 3;
return this;
}
public HouseBuilder doors()
{
downtownHouse.doors = 2;
return this;
}
public HouseBuilder rooms()
{
downtownHouse.rooms = 2;
return this;
}
public House build()
{
return downtownHouse;
}
}
public static class VilaBuilder implements HouseBuilder
{
House vila new House();
public HouseBuilder area()
{
vila.area = "Downtown";
}
public HouseBuilder windows()
{
vila.windows = 24;
return this;
}
public HouseBuilder doors()
{
vila.doors = 5;
return this;
}
public HouseBuilder rooms()
{
downtownHouse.rooms = 10;
return this;
}
public House build()
{
return vila;
}
}
class Driector
{
private HouseBuilder houseBuilder;
public Driector(HouseBuilder houseBuilder)
{
this.houseBuilder = houseBuilder;
}
public House buildHouse()
{
return this.houseBuilder.area()
.windows()
.doors()
.rooms()
.buid();
}
}
class HouseConstruction
{
public static void main(String[] args)
{
Director director = new Director(new VilaBuilder());
House house = director.buildHouse();
System.out.println("Builder constructed: "+ house);
}
}
Hope this helps clarify what is a Director in Builder pattern.

overloaded constructor InnerClass inside a method

I'm reading the book "Thinking in Java" by Bruce Eckel. I came across this assertion in the inner class chapter, which says: "the only justification for using a local inner class instead of an anonymous inner class is if you need a named constructor and/or an overloaded constructor"
I don't now if i understood well but:
Is this the way of overloading constructors of Inner(local classes) inside method?
abstract class ForInner {
abstract String getName();
abstract void setName(String newName);
abstract int getNumber();
abstract void setNumber(int newNumber);
}
class Outer{
public ForInner getSomeInner(String name) {
class LocalInner extends ForInner{
private String myName;
private int myNumber;
public LocalInner(String myName) {
this.myName = myName;
}
public String getName() {
return myName;
}
public void setName(String newName) {
myName = newName;
}
public int getNumber() {
return myNumber;
}
public void setNumber(int newNumber) {
myNumber = newNumber;
}
}
return new LocalInner(name);
}
public ForInner getSomeInner(int number) {
class LocalInner extends ForInner{
private String myName;
private int myNumber;
public LocalInner(int myNumber) {
this.myNumber = myNumber;
}
public String getName() {
return myName;
}
public void setName(String newName) {
myName = newName;
}
public int getNumber() {
return myNumber;
}
public void setNumber(int newNumber) {
myNumber = newNumber;
}
}
return new LocalInner(number);
}
}
I'm not sure if the assertion referring to this. But might have a guess that is not the case because How different it would be of using in this way
abstract class ForInner {
abstract String getName();
abstract void setName(String newName);
abstract int getNumber();
abstract void setNumber(int newNumber);
}
lass Outer{
public ForInner inner (String name) {
return new ForInner() {
private String myName;
private int myNumber;
{
myName = name;
}
public String getName() {
return myName;
}
public void setName(String newName) {
myName = newName;
}
public int getNumber() {
return myNumber;
}
public void setNumber(int newNumber) {
myNumber = newNumber;
}
};
}
public ForInner inner (int number) {
return new ForInner() {
private String myName;
private int myNumber;
{
myNumber = number;
}
public String getName() {
return myName;
}
public void setName(String newName) {
myName = newName;
}
public int getNumber() {
return myNumber;
}
public void setNumber(int newNumber) {
myNumber = newNumber;
}
};
}
}
thank in advance?
public class OuterClass {
Runnable printA = new Runnable() {
#Override
public void run() {
System.out.println("Print A");
}
};
Runnable printB = new Runnable() {
#Override
public void run() {
System.out.println("MESSAGE:" + " " + "Print B");
}
};
class PrintMessage implements Runnable {
private String msg;
public PrintMessage(String msg) {
this.msg = msg;
}
// overloaded constructor
public PrintMessage(String prefix, String msg) {
this.msg = prefix + " " + msg;
}
#Override
public void run() {
System.out.println(msg);
}
}
Runnable printC = new PrintMessage("Print C");
Runnable printD = new PrintMessage("Print D");
Runnable printE = new PrintMessage("MESSAGE:", "Print E");
public static void main(String[] args) {
OuterClass sample = new OuterClass();
sample.printA.run();
sample.printB.run();
sample.printC.run();
sample.printD.run();
sample.printE.run();
}
}
There are two instances of Runnable implemented as anonymous classes. While printA is created you cannot use it to create printB. You should create anonymous class from the beginning (i.e. override all abstract methods).
If an inner class created based on Runnable, you can use it in form new PrintMessage() to create new instances. Besides that it's possible to use non-default constructors.
Ah ok so when have this code
class OuterClass {
public Runnable printA() {
return new Runnable() {
#Override
public void run() {
System.out.println("Print A");
}
};
}
public static void main(String[] args) {
OuterClass outer = new OuterClass();
Runnable printA = outer.printA();
Runnable printB = outer.printA();
}
}
In this case I'm not creating multiply instances of a single anonymous inner class. Instead I'm creating multiple anonymous classes that use the same source code. Is that Rigth?!
Thanks

Java builder pattern with non-trivial subclass tree

I'm familiar with using the builder pattern with generics and subclassing, but I can't see how to make it work with a non-trivial tree of subclasses (i.e. C extends B extends A). Here's a simple example of what I'm trying to do:
class A {
private final int value;
protected A(ABuilder builder) {
this.value = builder.value;
}
public int getValue() { return value; }
public static class ABuilder<T extends ABuilder<T>> {
private int value;
public T withValue(int value) {
this.value = value;
return (T) this;
}
public A build() {
return new A(this);
}
}
}
class B extends A {
private final String name;
public static BBuilder builder() {
return new BBuilder();
}
protected B(BBuilder builder) {
super(builder);
this.name = builder.name;
}
public String getName() { return name; }
public static class BBuilder<U extends BBuilder<U>> extends ABuilder<BBuilder<U>> {
private String name;
public U withName(String name) {
this.name = name;
return (U) this;
}
public B build() {
return new B(this);
}
}
}
Everything is fine if I declare BBuilder without the generic type:
public static class BBuilder extends ABuilder<BBuilder>
Since I want BBuilder to be extended by a CBuilder, I'm trying to use the same sort of Curiously Recurring Template Pattern as ABuilder. But like this, the compiler sees BBuilder.withValue() as returning an ABuilder, not a BBuilder as I want. This:
B b = builder.withValue(1)
.withName("X")
.build();
doesn't compile. Can anyone see what I'm doing wrong here, I've been going round trying different patterns of generics but can't get it to work.
Thanks to anyone who has any advice.
It seems that your mistake only with declaring correct parameter:
class A {
private final int value;
public static <T extends Builder<T>> T builderA() {
return (T)new Builder<>();
}
protected A(Builder<? extends Builder<?>> builder) {
value = builder.value;
}
public static class Builder<T extends Builder<T>> {
private int value;
public T withValue(int value) {
this.value = value;
return (T)this;
}
public A build() {
return new A(this);
}
}
}
class B extends A {
private final String name;
public static <T extends Builder<T>> T builderB() {
return (T)new Builder<>();
}
protected B(Builder<? extends Builder<?>> builder) {
super(builder);
name = builder.name;
}
public static class Builder<T extends Builder<T>> extends A.Builder<T> {
private String name;
public Builder<T> withName(String name) {
this.name = name;
return this;
}
public B build() {
return new B(this);
}
}
}
Client code:
A a = A.builder().withValue(1).build();
B b = B.builder().withValue(2).withName("xx").build();
Are you certain you need generics? This hierarchy seems to work fine without generics.
static class A {
protected final int value;
protected A(ABuilder builder) {
this.value = builder.value;
}
public int getValue() {
return value;
}
#Override
public String toString() {
return "A{" +
"value=" + value +
'}';
}
public static ABuilder builder() {
return new ABuilder();
}
public static class ABuilder {
protected int value;
public ABuilder withValue(int value) {
this.value = value;
return this;
}
public A build() {
return new A(this);
}
}
}
static class B extends A {
protected final String name;
protected B(BBuilder builder) {
super(builder);
this.name = builder.name;
}
public String getName() {
return name;
}
#Override
public String toString() {
return "B{" +
"value=" + value +
", name='" + name + '\'' +
'}';
}
public static BBuilder builder() {
return new BBuilder();
}
public static class BBuilder extends ABuilder {
private String name;
public BBuilder withName(String name) {
this.name = name;
return this;
}
#Override
public BBuilder withValue(int value) {
this.value = value * 2;
return this;
}
public B build() {
return new B(this);
}
}
}
static class C extends B {
private final String otherName;
protected C(CBuilder builder) {
super(builder);
this.otherName = builder.otherName;
}
public String getName() {
return otherName;
}
#Override
public String toString() {
return "C{" +
"value=" + value +
", name='" + name + '\'' +
", otherName='" + otherName + '\'' +
'}';
}
public static CBuilder builder() {
return new CBuilder();
}
public static class CBuilder extends BBuilder {
private String otherName;
public CBuilder withName(String name) {
this.otherName = name;
return this;
}
public C build() {
return new C(this);
}
}
}
public void test() {
A a = A.builder().withValue(10).build();
B b = B.builder().withValue(10).withName("B").build();
C c = C.builder().withName("C").build();
System.out.println("a = "+a);
System.out.println("b = "+b);
System.out.println("c = "+c);
}

Builder pattern with inheritance support generics issue

I implemented pattern based on this answer
I have the following asbtract config:
public abstract class AbstractConfig {
public static abstract class Builder<B extends Builder<B>> {
private int calories = 0;
public Builder() {
}
public B setCalories(int calories) {
this.calories = calories;
return (B) this;
}
public abstract AbstractConfig build();
}
private int calories = 0;
protected AbstractConfig(final Builder builder) {
calories = builder.calories;
}
}
And I have the following concrete config:
public class DialogConfig extends AbstractConfig {
public static class DialogConfigBuilder<B extends DialogConfigBuilder<B>> extends Builder<B> {
private double width;
private double height;
public DialogConfigBuilder() {
//does nothing.
}
public B setWidth(final double value) {
width = value;
return (B) this;
}
public B setHeight(final double value) {
height = value;
return (B) this;
}
public DialogConfig build() {
return new DialogConfig(this);
}
}
private final double width;
private final double height;
protected DialogConfig(final DialogConfigBuilder builder) {
super(builder);
width = builder.width;
height = builder.height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
}
And this is how I use it
DialogConfig config = new DialogConfig.DialogConfigBuilder()
.setWidth(0)
.setCalories(0)
.setHeight(0) //X LINE
.build();
At X line I get - Can't find symbol method setHeight. What is my mistake?
EDIT - I will have and a ExtendedDialogConfig that must extend DialogConfig and etc. I mean there will be other subclasses.
You would first change setCalories() to:
public Builder<B> setCalories(int calories) {
this.calories = calories;
return this;
}
to get rid of that cast and the warning. And now look at this closely. You return a Builder. This code doesn't know about future subclasses. It only returns an instance of that base builder.
As a consequence, when you have that chained call:
.setHeight(0) .build();
that would return that base builder. To then call build() - which would build an abstract configuration. But you want to assign that to a more specific DialogConfig. Thus the error.
A (ugly) workaround:
DialogConfig.DialogConfigBuilder<?> builder = new DialogConfig.DialogConfigBuilder<>().setHeight(0);
builder.setCalories(0);
...config = builder.build();
And a solution - by again reworking setCalories():
#SuppressWarnings("unchecked")
public <T extends B> T setCalories(int calories) {
this.calories = calories;
return (T) this;
}
Fixes the compile error; and allows chaining the setCalories() call as well. Final exercise of getting rid of the cast/suppress is left as exercise to the reader.
And for the record - the "complete" solution, including all adaptions to get rid of raw types and other warnings:
abstract class AbstractConfig {
public static abstract class Builder<B extends Builder<B>> {
private int calories = 0;
#SuppressWarnings("unchecked")
public <T extends B> T setCalories(int calories) {
this.calories = calories;
return (T) this;
}
public abstract AbstractConfig build();
}
private int calories = 0;
public int getCalories() { return calories; }
protected <B extends Builder<B>> AbstractConfig(final Builder<B> builder) {
calories = builder.calories;
}
}
final class DialogConfig extends AbstractConfig {
public static class DialogConfigBuilder<B extends DialogConfigBuilder<B>> extends Builder<B> {
private double width;
private double height;
public DialogConfigBuilder<B> setWidth(final double value) {
width = value;
return this;
}
public DialogConfigBuilder<B> setHeight(final double value) {
height = value;
return this;
}
public DialogConfig build() {
return new DialogConfig(this);
}
}
private final double width;
private final double height;
protected <B extends DialogConfigBuilder<B>> DialogConfig(final DialogConfigBuilder<B> builder) {
super(builder);
width = builder.width;
height = builder.height;
}
public double getWidth() { return width; }
public double getHeight() { return height; }
}
public class Builders {
public static void main(String[] args) {
DialogConfig config = new DialogConfig.DialogConfigBuilder<>().setHeight(0).setCalories(0).build();
System.out.println(config);
}
}
I found my mistake. This is how I used DialogConfigBuilder
DialogConfig config = new DialogConfig.DialogConfigBuilder()
.setWidth(0)
.setCalories(0)
.setHeight(0) //X LINE
.build();
This is how I should use DialogConfigBuilder
DialogConfig config = new DialogConfig.DialogConfigBuilder<>()
.setWidth(0)
.setCalories(0)
.setHeight(0) //X LINE
.build();
Pay attention to <> in the second case.

Why is my java EXTENDS not working?

I hava a Super Class Ball with extender OvalBall. With following being my code I am getting a error message telling me I can't use the child class OvalBall. can anyone help explain please?
public class Ball
{
private double diameter;
private String colour;
public Ball(double d, String c)
{
this.diameter = d;
this.colour = c;
}
public void setDiameter(double d)
{
this.diameter = d;
}
public double getDiameter()
{
return this.diameter;
}
public void setColour(String c)
{
this.colour = c;
}
public String getColour()
{
return colour;
}
public double bounce()
{
double height = diameter * 2;
return height;
}
public void roll()
{
System.out.println("wheeee");
}
public class OvalBall extends Ball
{
private double secondDiameter;
public void setSecondDiameter(double sd)
{
this.secondDiameter = sd;
}
public double getSecondDiameter()
{
return this.secondDiameter;
}
}
}
public class Main
{
public static void main(String[] args)
{
OvalBall na = new OvalBall(4,"blue",4);
na.setSDiameter(10);
System.out.println(na.bounce());
}
}
Feel free to change anything.
Thanks
You need to define OvalBall either as static or in its own file.
Maybe OvalBall din't implement the constructor class of hi base class
That's why you can't create a object of OvalBall.
class Ball {//This is you base class contructor
//Child class contructor
public class OvalBall extends Ball
{
public OvalBall(double d, String c)
{
base(d,c);
}
}
And when you make nested classes in java there is a different. way to create an object instance instead of other languages Like C#.
Try in this way. or use my code as example
public class Main
{
public static void main(String[] args)
{
Ball.OvalBall na = na.new OvalBall(4,"blue",4);
na.setSDiameter(10);
System.out.println(na.bounce());
}
}

Categories