• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Factory pattern question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,
Need some help from the experts !
Ok so i have this 1000 line of code i need to refactor ! We are constructing quite a few objects (say X) using helper class..so we have makeaX(), makebX() and so on..i was thinking of using the factory pattern in this instance ..so we have a factory that churns out X objects provided you pass a XEnum lets say ....one problem is we have to pass a lot of parameters to each method so that the method can construct the object..now these parameters are determined from several service classes ...in the factory pattern would we have to pass the parameters too or can the factory itself have those services and pass on the needed ones to the respective classes ? How will that work ? Hope i am clear !!
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Javaranch !!!

i think you should not involve your service classes in your factory classes.

you can create objects in your factory class. at that time , object will be created with default inital values. then when you got your object back from factory, you can use your service class to fill in values in object using setters method.

~ abhay
 
Sam Hogg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So then the factory class will be used to produce blank objects and the code that i wanted to encapsulate will still be in the class where this blank object is populated. Is it better if i draw up an inputTO with required data and pass to the factory class when i need the objects to be created...in that case too the information required to make the class will be outside..is there some way where i call the factory class and it gives me a full object with all required values ? but question is then how to populate those values in the factory class without access to service classes. Basically i want the calling class to know nothing much about how the object that is being created...if the calling class were to populate the blank return class then the purpose is lost ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maybe for your application the Builder pattern might be a good fit. It is a little bit like the Factory pattern, but allows you to pass parameters to the builder before building your object.

An advantage of having a separate builder to build some complex object over just making the complex object a bean with getters and setters, is that you can make the complex object immutable (which makes it simpler to work with, especially when you have multiple threads that might be concurrently using the object - if it's immutable, you don't need to worry about synchronization because multiple threads might try to modify the object.

The examples on Wikipedia for the Builder pattern is not that good in my opinion, so here's an example of where I'm using this in the project I'm currently working at.

In our project we have to create messages. A messsage has a type (for which we have an enum) and can contain fields. We have a MessageBuilder class that's the builder for Message objects. You pass it the message type, then you can add fields to the builder, and at the end you call the build method to create the Message object. The builder's methods return this, so that you can chain them. Something like this:

Ofcourse you could make the build method so that it would return different subclasses of Message, if that's what you want.
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic