• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

doubt on factory design pattern

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have one doubt on factory design pattern
Actually in our project we are creating the object using factory class
suppose to create an object of the class
Plan_f.getFactory().create();
but wecan also do,ike this
PLan_f f= new Plan_f(;
what is the advantage of creating an object by using factory classes
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a little bit more flexibility, since getFactory() could also return a Factory that creates an instance of a subclass of Plan_f (strange class name, by the way...).

This usage is far from showing the full potential of the pattern, though, and something I can't imagine I would do often, if at all. Typically, when using a Factory, you will pass in the Factory to the object which uses it, for example as a constructor parameter (google for "dependency injection").
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pvsr rao:
Hi,
I have one doubt on factory design pattern
Actually in our project we are creating the object using factory class
suppose to create an object of the class
Plan_f.getFactory().create();
but wecan also do,ike this
PLan_f f= new Plan_f(;
what is the advantage of creating an object by using factory classes



Well, you see "PLan_f f= new Plan_f()", the new operator is working with a concrete class Plan_f. What if you need to use Plan_f1 (maybe a subclass of Plan_f) ? You have to open your IDE, change your code, then recompile and finnally you need to redeploy it. Now that there is
Factory pattern, you can specify a default class in your configuration file and let the factory determine which concrete class it should use when it is runtime. All you have to do is just modify a text line
in the config file. After that you can enjoy a coffee or something else.
[ March 11, 2008: Message edited by: Roger King ]
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, you see "PLan_f f= new Plan_f()", the new operator is working with a concrete class Plan_f. What if you need to use Plan_f1 (maybe a subclass of Plan_f) ? You have to open your IDE, change your code, then recompile and finnally you need to redeploy it.



Indeed and this instantiation may occur in a number of different classes, which means you have to edit a whole set of files. Using a factory pattern
will help you to develop a design that is less brittle and more flexible.
Try it.

regards,
Mo
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, suppose someday in the future, it needs many taks such as database querying during creating a object of the class, how should you do, if you
write these codes in constructor, it may seem less elegant and break the encapsulation and division rules of oop.So you need to put these details into the methods in factory class. And this ensures the oo rule: separate mutable parts from stable rule.
At the same time,it gives you flexibility in your design: you can add or remove subclasses in the factory class without changing the client codes who invoke your codes.
Am i clear.
 
He's giving us the slip! Quick! Grab this tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic