Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Created March 21, 2025 19:02
Show Gist options
  • Save trikitrok/9c8d26c6dd53b8c259388ff27a7a5cd0 to your computer and use it in GitHub Desktop.
Save trikitrok/9c8d26c6dd53b8c259388ff27a7a5cd0 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
export interface PointRenderer {
readonly colorId: number;
drawPoint(x: number, y: number, color: Color): void;
}
// !!! -> the original class implements it
export class GDIBrush implements PointRenderer {
colorId: number;
// A long method
public draw(renderingRoots: Point[], colors: ColorMatrix, selection: Point[]): void {
// !!! -> we wrote this line and used the IDE to generate the new class
new Renderer(this, renderingRoots, colors, selection);
// some more code in the method
for (const point of renderingRoots) {
// a lot more code in the loop
this.drawPoint(point.x, point.y, colors.getColor(this.colorId));
}
// a lot more code in the method
}
drawPoint(x: number, y: number, color: Color): void {
// some code to draw a point...
}
}
export class Renderer {
private readonly pointRenderer: PointRenderer;
private readonly renderingRoots: Point[];
private readonly colors: ColorMatrix;
private readonly selection: Point[];
// !!! -> the new class only see the new interface
constructor(
pointRenderer: PointRenderer,
renderingRoots: Point[],
colors: ColorMatrix,
selection: Point[]
) {
this.pointRenderer = pointRenderer;
this.renderingRoots = renderingRoots;
this.colors = colors;
this.selection = selection;
}
public draw(): void {
// some more code in the method
for (const point of this.renderingRoots) {
// a lot more code in the loop
this.pointRenderer.drawPoint(point.x, point.y, this.colors.getColor(this.pointRenderer.colorId));
}
// 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