Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Created September 5, 2024 19:11
Show Gist options
  • Save trikitrok/8d827d8c6ac49bb173b19675256d1c43 to your computer and use it in GitHub Desktop.
Save trikitrok/8d827d8c6ac49bb173b19675256d1c43 to your computer and use it in GitHub Desktop.
Step 3
// Step 3
// !!! -> extracted interface with the things the
// object method uses from the original class
interface PointRenderer {
int getColorId();
void drawPoint(int x, int y, Color color);
}
class GDIBrush implements PointRenderer {
private int colorId;
// A long method
public void draw(List<Point> renderingRoots,
ColorMatrix colors,
List<Point> selection) {
new Renderer(this, renderingRoots, colors, selection);
// some more code in the method
for (Point point : renderingRoots) {
// a lot more code in the loop
drawPoint(point.x(), point.y(), colors.getColor(colorId));
}
// a lot more code in the method
}
@Override
public void drawPoint(int x, int y, Color color) {
// some code to draw a point...
}
@Override
public int getColorId() {
return colorId;
}
}
// !!! -> the new class only see the new interface
class Renderer {
private final PointRenderer pointRenderer;
private final List<Point> renderingRoots;
private final ColorMatrix colors;
private final List<Point> selection;
public Renderer(PointRenderer pointRenderer, List<Point> renderingRoots, ColorMatrix colors, List<Point> selection) {
this.pointRenderer = pointRenderer;
this.renderingRoots = renderingRoots;
this.colors = colors;
this.selection = selection;
}
public void draw() {
// some more code in the method
for (Point point : renderingRoots) {
// a lot more code in the loop
pointRenderer.drawPoint(point.x(), point.y(), colors.getColor(pointRenderer.getColorId()));
}
// a lot more code in the method
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment