Created
January 26, 2017 22:34
-
-
Save genderquery/f834c3934bfe64fa764ab2795dbed3cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
// Normally you would put each of these classes in their own file | |
class Point { | |
static final Point ORIGIN = new Point(0, 0); | |
private final double x; | |
private final double y; | |
public Point(final double x, final double y) { | |
// a good example of 'this' | |
this.x = x; | |
this.y = y; | |
} | |
public double getX() { | |
return x; | |
} | |
public double getY() { | |
return y; | |
} | |
} | |
abstract class Asset { | |
private Point mCenter; | |
public Asset() { | |
// 'this' is used to call the other constructor | |
this(Point.ORIGIN); | |
} | |
public Asset(final Point center) { | |
// 'this' is avoided by prefixing members with 'm' | |
mCenter = center; | |
} | |
public Point getCenter() { | |
return mCenter; | |
} | |
public void setCenter(final Point center) { | |
mCenter = center; | |
} | |
public abstract void draw(); | |
} | |
class Rectangle extends Asset { | |
private double mWidth; | |
private double mHeight; | |
public Rectangle() { | |
this(Point.ORIGIN, 0, 0); | |
} | |
public Rectangle(final Point center, final double width, final double height) { | |
// 'super' is like 'this', but refers to the superclass (Asset in this case) | |
super(center); | |
mWidth = width; | |
mHeight = height; | |
} | |
@Override | |
public void draw() { | |
// do the drawing | |
} | |
public double getWidth() { | |
return mWidth; | |
} | |
public void setWidth(final double width) { | |
mWidth = width; | |
} | |
public double getHeight() { | |
return mHeight; | |
} | |
public void setHeight(final double height) { | |
mHeight = height; | |
} | |
} | |
interface IRectangleBuilder { | |
RectangleBuilder setCenter(final Point center); | |
RectangleBuilder setWidth(final double width); | |
RectangleBuilder setHeight(final double height); | |
Rectangle build(); | |
} | |
class RectangleBuilder implements IRectangleBuilder { | |
protected Rectangle mRectangle; | |
public RectangleBuilder() { | |
mRectangle = new Rectangle(); | |
} | |
@Override | |
public RectangleBuilder setCenter(final Point center) { | |
mRectangle.setCenter(center); | |
// return the instance of this rectangle so we can chain methods | |
return this; | |
} | |
@Override | |
public RectangleBuilder setWidth(final double width) { | |
mRectangle.setWidth(width); | |
return this; | |
} | |
@Override | |
public RectangleBuilder setHeight(final double height) { | |
mRectangle.setHeight(height); | |
return this; | |
} | |
@Override | |
public Rectangle build() { | |
return mRectangle; | |
} | |
} | |
class Test { | |
static { | |
Rectangle rectangle = new RectangleBuilder() | |
.setCenter(new Point(10, 20)) | |
.setWidth(100) | |
.setHeight(200) | |
.build(); | |
rectangle.draw(); | |
} | |
} | |
// Don't do this, it's confusing | |
class Outer { | |
Object mObj; | |
class Inner { | |
Object mObj; | |
Object getOuterObj() { | |
return Outer.this.mObj; | |
} | |
Outer getOuter() { | |
return Outer.this; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment