Exforsys

Online Training

Is it possible to reuse the Singleton instance variable and getInstance method in subclasses

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 ...


Go Back   Exforsys > Testing > Software Patterns

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-26-2005, 11:02 AM
tomjbr.10216233@bloglines.com
Guest
 
Posts: n/a
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 ???

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:12 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.