-2

I have a class Point, that has a static type variable Visual. I made it static because: The type Visual contains method to draw points and lines between points, etc. The type Visual also creates a blank canvas each time a Visual object is instantiated. Since I need a group of points to be visualized on a single canvas, Visual is made a static singleton. My problem is I have a client class PointOp, that performs some operation with Point objects. I want all Point objects from PointOp instance 1 on a single canvas, and all Point objects from PointOp instance 2 on a single, different canvas. I can not achieve this by simply instantiate two PointOp objects. All Point objects from two instances are drawn on the same canvas. How may I solve this? Is there a way I can maintain different versions of the Visual object in different PointOp objects?

public class Point
{
    static Visual visualize = null;

    public void draw()     // draw() method of Point
    { 
        if (visualize == null)
           visualize = new Visual();
        visualize.draw();    // draw() method of type Visual evoked.
    }
}

public class PointOp()
{  
    Point[] point;
    public PointOp()
    {
        // Instantiate an array of Point objects for operation.
    }
    PointOp instance1 = new PointOp();
    instance1.point[0].draw();         
    PointOp instance2 = new PointOp();
    instance2.point[0].draw();    // Problem: all Points mixed on single  canvas.
}
Muye
  • 175
  • 1
  • 7

2 Answers2

0

Visual doesn't need to be static at all, for example you could create Canvas class, instantiate one canvas, add this canvas to one or more visual objects and then create points with the same visual objects. That's how it can be solved with your logic.

But I think what you want to achieve is the same as the Swing's mvc model, it's worth to have a look on this pattern.

gabor.harsanyi
  • 599
  • 2
  • 14
0

You should store the Visual in the PointOp class, and then pass it as an argument to the draw() method:

public class Point
{
    static Visual visualize = null;

    public void draw(Visual visualize)
    { 
        visualize.draw();
    }
}

public class PointOp()
{  
    Point[] point;
    private Visual visual;

    public PointOp()
    {
        // Instantiate an array of Point objects for operation.
        visual = new Visual()
    }
    PointOp instance1 = new PointOp();
    instance1.point[0].draw(visual );         
    PointOp instance2 = new PointOp();
    instance2.point[0].draw(visual );    // Problem: all Points mixed on single              canvas.
}
DennisW
  • 1,057
  • 1
  • 8
  • 17