• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Singleton Factory Object

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frankly, I have no idea what the term "Singleton Factory Object" is supposed to mean. Where did you get that question from?
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was a question on this test i gotta do to get this job.
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Water proof donuts! Eat them while reading this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic