This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

All factory methods are from singleton class  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)All factory methodes are from singleton classes true or false?

2)Is factory method creates only an object of an interface true or flase?

3)Why to go for factory methods while can create an object using constructor
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like homework. We'll help a bit, but won't do the whole job. Have you googled for "factory method pattern" or "factory pattern"? What have you found so far?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand 1) and 2) - please elaborate.

For 3), the short version of the most important reason is that constructors aren't polymorphic.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saikrishna cinux:
[Q]1)All factory methodes are from singleton classes true or false?--False

2)Is factory method creates only an object of an interface true or flase?---False

3)Why to go for factory methods while can create an object using constructor--What do you really understand by Factory method[/Q]


I can contribute from my side.

Factory method is just a fancy name for a method that instantiates objects. Like a factory, the job of the factory method is to create -- or manufacture -- objects.

Factory methods prove useful when you're not sure what concrete implementation of a class to instantiate. Instead, you can leave those details to the factory method.

In short Factory methods :
1. Can return a subtype of their return type - in particular, can return an object whose implementation class is unknown to the caller. This is a very valuable and widely used feature in many frameworks which use interfaces as the return type of static factory methods.
2. Do not need to create a new object upon each invocation - objects can be cached and reused, if necessary.
3. Have names, unlike constructors, which can clarify code
Common names for factory methods include getInstance and valueOf.

Hope this helps you.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankur Sharma:

Factory method is just a fancy name for a method that instantiates objects.


In fact, Factory Method is the name of a design pattern - a method that instanciates objects, but is declared in the base class, to be implemented by subclasses.

Other (for example static) methods that instanciate objects are better called "creation methods" or something, to avoid confusion.
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saikrishna cinux:
1)All factory methodes are from singleton classes true or false?


True. Singleton would be the best choice because it will the single place of management for all the objects created. i.e. The singleton's purpose is to control object creation. The objects can be cached etc.
I think even all ConnectionFactory objects in Java SE 1.4 API are singleton i.e QueueConnectionFactory, TopicConnectionFactory etc..


2)Is factory method creates only an object of an interface true or flase?


False. It can also create objects of Abstract classes or concrete classes


3)Why to go for factory methods while can create an object using constructor


Creating objects inside a class with a factory method is always more flexible than creating an object directly

....just my thoughts..
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just browsed through the API for classes with "Factory" in the name and found interfaces, abstract classes, classes with all static methods, and a few public constructors. Only two (in the ssl package) had protected constructors. Sun didn't seem to think factories had to be singletons, but as we often comment here, what did Sun know about Java when they started it?

The problem with using normal constructors is you've named the implementation class in the code.

Widget w = new MyFavoriteWidget();

Now you have a hard dependency on MyFavoriteWidget which must be present to compile and to run. If you get a NewFavoriteWidget one day, you'll have to change all references to the old class name. A factory makes those problems go away, which is key to testability and flexibility over time in some designs.

Widget w = WidgetFactory.todaysFavorite();

Now you can introduce new widgets with a bit of factory configuration and the rest of the code never needs to know about it.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's been at least three distinct patterns/idioms discussed here. In the absence of any formal definitions (that I'm aware of) I'll have to resort to the GoF book and Josh Bloch's Effective Java in an attempt to name them. Unfortunately it won't be long before someone else writes another book and uses another contradicting term to confuse matters further. :sigh: None the less, the three things I've seen described here are:

1) Static Factory Method

A static method provided by a class to return instances of the object instead of providing constructors. It's described in Josh Bloch's book and has been described here.

2) Factory Method

A pattern in which an interface is defined for creating an object but instantiation is left to subclasses. Ilja mentioned this briefly.

3) Abstract Factory

An interface for creating related or dependent objects without specifying the concrete classes. This is what I believe Stan was referring to and it's often implemented using factory methods.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was probably using what Ilja called a vanilla creation method. I tend to blur these with "factory" because the first one I ran into lo these many years ago was on a class called BOFactory. That predates my exposure to GoF and in spite of their stature in the OO world, I have not yet let them make me change words. I said in another post I find GoF a bit "fussy" on these two patterns, and that's because they exclude the thing that I do all the time.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
I was probably using what Ilja called a vanilla creation method. I tend to blur these with "factory" because the first one I ran into lo these many years ago was on a class called BOFactory. That predates my exposure to GoF and in spite of their stature in the OO world, I have not yet let them make me change words. I said in another post I find GoF a bit "fussy" on these two patterns, and that's because they exclude the thing that I do all the time.


I believe what you described was very similar to an abstract factory though I'm not sure if it technically is since it was just one concrete factory. The method todaysFavorite would be the creation method/static factory method. If WidgetFactory were made into an interface or was made abstract and the method todaysFavorite was an instance method then that method would be a factory method and WidgetFactory an abstract factory. You could then implement different concrete subclasses with different implementations that return different kinds of widgets. Then the system that uses the widgets isn't tied to any specific implementation at compile-time and the factory could be supplied at runtime.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16043
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saikrishna cinux:
1)All factory methodes are from singleton classes true or false?

Rajah Nagur:
True. Singleton would be the best choice because it will the single place of management for all the objects created. i.e. The singleton's purpose is to control object creation. The objects can be cached etc.
I think even all ConnectionFactory objects in Java SE 1.4 API are singleton i.e QueueConnectionFactory, TopicConnectionFactory etc..


No - that's false. A factory method does not necessarily have to be in a singleton class, and also in the Java SE API that is not always the case, as Rajah is saying.

Have a look at javax.xml.SAXParserFactory, for example. It is not a singleton. You have to create a new instance of SAXParserFactory first, and then you call the newSAXParser() factory method on that instance to create a new SAXParser.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!