I'm designing an application that will allow me to draw some functions on a graphic. Each function will be drawn from a set of points that I will pass to this graphic class.
There are different kinds of points, all inheriting from a MyPoint class. For some kind of points it will be just printing them on the screen as they are, others can be ignored, others added, so there is some kind of logic associated to them that can get complex.
How to actually draw the graphic is not the main issue here. What bothers me is how to make the code logic such that this GraphicMaker class doesn't become the so called God-Object.
It would be easy to make something like this:
class GraphicMaker {
ArrayList<Point> points = new ArrayList<Point>();
public void AddPoint(Point point) {
points.add(point);
}
public void DoDrawing() {
foreach (Point point in points) {
if (point is PointA) {
//some logic here
else if (point is PointXYZ) {
//...etc
}
}
}
}
How would you do something like this? I have a feeling the correct way would be to put the drawing logic on each Point object (so each child class from Point would know how to draw itself) but two problems arise:
I would do as you suggested and make each point responsible for drawing itself, passing the array of other points to it:
interface ICanDraw {
void Draw(ArrayList<Point> allPoints);
}
public abstract class Point : ICanDraw {
...
}
public PoniePoint : Point {
public void Draw(ArrayList<Point> allPoints) {
// do drawing logic here
}
}
For your GraphicMaker:
public void DoDrawing() {
foreach (Point point in points) {
point.Draw(points);
}
}
(My Java is a bit rusty, so that might not 100% syntactically correct Java but I think it conveys my suggestion).