This is a discussion on Is it possible to reuse the Singleton instance variable and getInstance method in subclasses within the Software Patterns forums, part of the Testing category; I do not believe this is possible, at least not with java, but maybe there is some clever way of ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Is it possible to reuse the Singleton instance variable and getInstance method in subclasses
I do not believe this is possible, at least not with java, but maybe
there is some clever way of doing it ? Anyway, below is a code example of what I would like to be able to do. Below I have two Singleton classes "Oval" and "Rectangle" inheriting from "AbstractFigure". This would be an example of the state pattern, where the state is either "the user has selected to draw Rectangles" or "the user has selected to draw Ovals" and those two states are implemented as Singletons. Ssome other class, i.e. an event listener, would have a method "setFigure(AbstractFigure figure)". The code: import java.awt.Graphics; public class Rectangle extends AbstractFigure { { // figure below is declared in the baseclass figure = new Rectangle(); // I wish this would become invoked } private Rectangle() {} public void drawFigure(Graphics graphics, int left, int upper, int width, int height) { graphics.drawRect(left, upper, width, height); } } import java.awt.Graphics; public class Oval extends AbstractFigure { { figure = new Oval(); } private Oval() {} public void drawFigure(Graphics graphics, int left, int upper, int width, int height) { graphics.drawOval(left, upper, width, height); } } import java.awt.Graphics; public abstract class AbstractFigure { protected static AbstractFigure figure; public static AbstractFigure getInstance() { return figure; } public abstract void drawFigure(Graphics graphics, int left, int upper, int width, int height); public static void main(String[] args) { // This is what I wish would be true: System.out.println(Oval.getInstance() != Rectangle.getInstance()); // becomes false ! System.out.println(Oval.getInstance()); // becomes null ! } } Obviously, the istantiations in the subclasses never will happen, so my solution usually is to not have any instance code in the baseclass and instead duplicate it in the subclasses like this: import java.awt.Graphics; public abstract class AbstractFigure { public abstract void drawFigure(Graphics graphics, int left, int upper, int width, int height); } import java.awt.Graphics; public class Oval extends AbstractFigure { private Oval() {} private static AbstractFigure figure = new Oval(); public static AbstractFigure getInstance() { return figure; } public void drawFigure(Graphics graphics, int left, int upper, int width, int height) { graphics.drawOval(left, upper, width, height); } } import java.awt.Graphics; public class Rectangle extends AbstractFigure { private Rectangle() {} private static AbstractFigure figure = new Rectangle(); public static AbstractFigure getInstance() { return figure; } public void drawFigure(Graphics graphics, int left, int upper, int width, int height) { graphics.drawRect(left, upper, width, height); } } It is not a big problem in a small example like this, but if there are more states/subclasses you want to be singletons, is there any better way of implementing state pattern subclasses as singletons without code duplication as above ??? |