*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singleton Factory Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Singleton Factory Object" Watch "Singleton Factory Object" New topic
Author

Singleton Factory Object

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I have an exercise about Singleton Factory Object that I need to do, but I don't quite understand.

The question:
Write a Singleton Factory Object for creating shapes that keeps track of the number of things it has made:

Well I know how to make a singleton class:

But I'm not sure about the Factory part, I can't seem to find much info on Singleton Factory Objects.

Can anyone help? Thanks
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I think you need to split the problem into two parts. It looks like you understand the idea of a Singleton, but a Singleton with no behaviour is not much use.

Take another look at the question, and see if you can spot the hints about what behaviour your Singleton object should have.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
I've had a little think about it..
I could make a Singleton class...

And then get the shapes classes to extend from it.
So there can only be at most one Triangle instance, and at most one Square instance.

But I'm still not sure about the factory part, or maybe I've got it wrong above?

Any thoughts?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Frankly, I have no idea what the term "Singleton Factory Object" is supposed to mean. Where did you get that question from?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Was a question on this test i gotta do to get this job.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I reckon it just means a Singleton which is also a factory (ie. it has a public method which can be called to "create" other objects.) That way you get a Singleton which also acts as an Abstract Factory for other objects.

This seems to be one of the more common uses for Singleton.

Make sense?
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, I think I get it now, is this meaningful?

If the Factory is singleton, then there can only exist one factory object.

So then if this Factory object has a field int numOfThingsMade.
Then this value will be a true reflection of how many things have been made.

If the Factory was NOT singleton, then...

Would require you to do f1.numOfThingsMade+f2.numOfThingsMade ( =3 ) to get the total, whereas the singleton factory has special powers since you would just call SingletonFactory.getInstance().numOfThingsMade, and get 3.

Have I got the right idea?
Thanks
[ May 14, 2007: Message edited by: colin shuker ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Originally posted by colin shuker:

Have I got the right idea?
Thanks


That certainly sounds reasonable. I can't say for certain without knowing the details of the question, and what was going on in the minds of whoever wrote it.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, continuing from above, I need the Factory to create Shape (an Interface) objects, such as Circle,Triangle,Rectangle(implements Shape).

But the Constructor for Circle takes one argument, the radius.
The constructor for Triangle and Rectangle take 2, base and height.

So in my Singleton Factory class, I have the method:

So to create a Circle radius 8:


So you can see I'm passing an array of parameters in since this is variable( 1 or 2 parameters).
This is probably not the best approach, I have thought about subclassing the SingletonFactory to give two classes, one that deals with objects that require 1 parameter passed in, and one that deals with objects that require 2 parameters passed in.

Is this a good idea, or is there a better way to pass in the parameters to the factory to produce specific sized shapes?

Thanks for help.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
VarArgs might help. New in 1.5.

But it seems like the client already knows what it wants by the parameters it gathers. Maybe the factory should just have methods for createCircle, createSquare, etc. Yes, you have to add new methods for new shapes, but the client is already going to be going through code changes for new shapes by new sets of parameters ... like Parallelogram and Elipse.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton Factory Object