Uncertainty creeps in as I ponder a potentially foolish idea (possibly recursive): I find myself tasked with building two objects using the same parameters. One object features a shared method, while the other boasts two private methods (think of them as polygons - one serving as a fixed background, and the other positioned on top as a "progressArc," requiring more complex calculations).
My current approach for creating this combined instance looks like this:
const createExergon = (radius = 100, points = 5, strokeWidth = 0, progress = 50) => {
// Method: calcPoints
let backgr = new PolygonBG(radius, points, strokeWidth);
// Methods: calcPoints (inherited), length, gradient
let progr = new PolygonPG(radius, points, strokeWidth, progress);
return { backgr, progr }
};
let exergon = createExergon();
I am now considering the possibility of running calcPoints() just once to share the values instead of the method...
(apologies, it appears the tags have been tampered with, but rest assured, this is indeed TypeScript)
EDIT: This is the current structure I have (as a newcomer to TypeScript classes, it might not be the most elegant approach...)
class Point {
x: number;
y: number;
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
};
interface Poly {
radius: number;
points: number;
strokeWidth: number;
readonly coords: Point[];
readonly length?: number;
readonly gradient?: number[];
}
//TODO: Find a way to pass default values
let defValues = { radius: 100, points: 5, strokeWidth: 0 };
// Creates a static Polygon (either standalone or serving as background for progressPolygon)
class PolygonBG implements Poly {
radius: number;
points: number;
strokeWidth: number;
readonly coords: Point[];
constructor(radius=100, points=5,strokeWidth=0) {
this.radius = radius;
this.points = points;
this.strokeWidth = strokeWidth;
this.coords = this.calcPoints();
}
/**
*
* @returns points of the regular polygon with the given attributes
*/
calcPoints() {
let p: Point[] = []
...
};
return p;
};
};
// Creates a polygon for progress calculation
class PolygonPG extends PolygonBG {
progress: number;
readonly length: number;
readonly gradient: number[];
constructor(radius = 100, points = 5, strokeWidth = 0, progress = 50) {
super(radius, points, strokeWidth);
this.length = this.len();
this.gradient = this.grad();
this.progress = progress;
};
private len(): number {
// Here, we require this.coords from calcPoints()
...
return l;
};
private grad() {
let g: number[] = [];
for (let i: number = 0; i < this.points; i++) {
// Here, we need this.coords from calcPoints()
...
}
return g;
};
};
// Created here with default values for simplicity
const createExergon = () => {
let backgr = new PolygonBG();
let progr = new PolygonPG();
return { backgr, progr }
};
let exergon = createExergon();
So currently, calcPoints() is executed twice (with the same parameters resulting in the same output but creating two distinct objects), which is functional but not the cleanest solution. It seems there is a fundamental logic issue that eludes me.
(my apologies for the influx of messages. I'm hopeful someone can offer a breakthrough idea.)