Problems with instanceOf when creating new objects from other objects

I have a class that is responsible for creating Formation objects from Shape objects. Shapes are what the name says, shapes that are drawn on the canvas ( TriangleShape , RectangleShape , etc.).

Shapes are similar to shapes, but I plan to use them differently.

RectangleShape , for example, looks like this:

 public class RectangleShape extends Shape { public RectangleShape() { this(0, 0, 0, 0); } public RectangleShape(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; this.nPoints = 4; } @Override public void drawShape(Graphics2D g) { Color color = g.getColor(); fillShape(g, new Color(g.getColor().getRed(), g.getColor().getGreen(), g.getColor().getBlue(), 16)); g.setColor(color); g.drawLine(x, y, x + width, y); g.drawLine(x, y, x, y + height); g.drawLine(x, y + height, x + width, y + height); g.drawLine(x + width, y, x + width, y + height); } @Override public String toString() { return "Rectangle"; } @Override public Shape createCopy() { return new RectangleShape(); } @Override public void fillShape(Graphics2D g) { xPoints = new int[] { x, x, x + width, x + width }; yPoints = new int[] { y, y + height, y + height, y }; g.fillPolygon(xPoints, yPoints, nPoints); } } 

I save a list of all the drawn shapes declared as List<Shape> = new ArrayList<>(); .

My problem arises when I need to dynamically create a formation from a form. The first approach was to have a class with these methods:

 public static TriangleFormation createFormationFrom(TriangleShape shape) { // my code here } public static RectangleFormation createFormationFrom(RectangleShape shape) { // my code here } public static PentagonFormation createFormationFrom(PentagonShape shape) { // my code here } public static HexagonFormation createFormationFrom(HexagonShape shape) { // my code here } public static OvalFormation createFormationFrom(OvalShape shape) { // my code here } 

The problem is that when I retrieve a form from my list, it is of type Shape , and I cannot call any of these methods without dropping the shape into the appropriate class, and then it begs the question of using instanceOf .

Should I combine the form and the formula in one class, should I try to implement a visitor template, perhaps (if so, how will it be done in this case) or is there something else that I did not think about?

+4
source share
1 answer

It all depends on how much you would like your Shape and Formation be. The simplest solution - as you mentioned - is to add the Formation createFormation() method to the Shape interface.

But if you are trying to keep Shape and Formation separate, you will need to do something more advanced. I would recommend NOT using a visitor template as it is very inflexible.

As an alternative, consider adding the FormationBuilder class:

 public interface FormationBuilder { /** * Builds a particular Formation implementation from the given shape. * Perhaps the shape is passed into the constructor of the Formation, * perhaps this method extracts the necessary information to build the * Formation... */ Formation build(Shape shape); } 

This can be used with factory as follows:

 public class FormationFactory { private final Map<Class<? extends Shape>, FormationBuilder> builders = new HashMap<Class<? extends Shape>, FormationBuilder>(); public <T extends Shape> void register( Class<T> shapeType, FormationBuilder builder); { builders.put(shapeType, builder); } public Formation getFormation(Shape shape) { return builders.get(shape.getClass()).build(shape); } } 

But now the question is where the Factory should be initialized. Regardless of whether it suits your needs, it depends on how your code is structured.

+1
source

Source: https://habr.com/ru/post/1482605/


All Articles