I feel like I have an ok grasp on the design pattern, however I can't seem to get my head around one thing and it doesn't seem to be exactly explained in the resources I've looked at.
That question is, how is the data common, without the data being static, considering the objects are separate entities?
In this example, I cannot figure out where the optimisation is occurring, as it looks as if the objects are just being created each time as normal.
Circle circle = (Circle)ShapeFactory.getCircle(getRandomColor());
circle.setX(getRandomX());
circle.setY(getRandomY());
circle.setRadius(100);
circle.draw();
This code block seems like my best guess as the first line retrieves from the HashMap a circle with a certain colour, but this creates a new instance of a circle, with no lasting reference to the colour of the circle.
Therefore, unless I'm mistaken, there is no actual shared data, so I am unsure of how this provides benefits other than maybe during creation.
Aucun commentaire:
Enregistrer un commentaire