What is a Factory method and when to use this concept?

Could any one please describe what a factory method is and explain how I can use it by giving a simple example

A Factory Method (sometimes called a "virtual constructor") is a way to avoid hard coding what class is instantiated. Consider:
DataSource myDataSource = new DataSource();Now, if you want to use some other DataSource in your app, say, an XMLDataSource, then you get to change this code and all subsequent lines that use this, which can be a lot. If, however, you specified and interface for your DataSources, say, IDataSource, and you gave the DataSource class a static "create" method that would take some indication of what sort of DataSource to actually use, then you could write code like:
IDataSource myDataSource = DataSource.create(dataSourceString);And be able to pass in a dataSourceString describing what DataSource you wanted to use - and not have to recompile. Check out the Java Design Patterns site ( http://www.patterndepot.com/put/8/JavaPatterns.htm )
Make sense?
Lee

Similar Messages

  • What is a factory method? when to use this concept?

    Could any oneplease describe what a factory method is and explain how I can use it by giving a simple example?

    Instead of instantiating a class all over your program you do it in one method that's part of the class. This means the class gets control over the instantiating process, a factory.
    1. You can implement a dispose facility, that is giving back objects that can be reused (when the factory method is called an old object is returned if there is one in store, only if not a new one is instantiated).
    2. An abstract class can have a factory method that returns concrete classes on demand, like
    abstract class Hello {
       static newHello(int id) {  // factory method
          switch (id) {
          case 0: return new Hi();
          case 1: return new Howdy();
          return null;
    public class Hi extends Hello {
    public class Howdy extends Hello {

  • Type Casting? When to use this concept?

    There is one question bothering me very very much.
    When should I use type casting?
    For example
    ClassNameQ c = (ClassNameQ) ......displayable
    How do I know which 'cast type' to use? Is there a suitable pattern
    we need to follow so that we can know 'Ah this is the cast
    we need to use'
    Please can any one point out which 'Cast type' points needs to be
    followed?
    Regards

    You can Cast an object from any subclass to its superclass,
    or from a superclass to the sublass, if the object is already
    an object of the subclass.
    I have a class named Name:
    public class Name {
    I have a subclass named LastName:
    public class LastName extends Name { [/b]
    And another named FirstName:
    [b]public class FirstName extends Name { [/b]
    I create an object like this:
    [b]LastName ln = new LastName("Johnson");
    Then pass it to a method that can work on all names:
    capitalFirstLetter(ln);
    capitalFirstLetter(Name n) {
    Now n in CapitalFirstLetter can be used only as a Name
    object. Anything specific to the LastName object can't be used.
    So if I need to use things specific to LastName I have to cast
    the object to LastName. There is a problem, however, since this
    method can work on all Names, so I don't know if the Name is
    a LastName or not. The object itself DOES know, however, and
    we can test, so we would cast to a subclass as follows:
    if (n instanceof LastName) {
    LastName ln = (LastName)n;
    ln.addToGeneology();
    } else of (n instanceof FirstName) {
    FirstName fn = (FirstName)n;
    fn.addOccurance();
    Steve

  • My iPad 1 is not responding. I restored to factory settings and when it says to hook up to I tunes it still won't work. I have restarted I tunes and uninstalled and installed and still not working. Help!!!!

    I restored to factory settings and when it says to hook up to I tunes it still won't work. I have restarted I tunes and uninstalled and installed and still not working. Help!!!! Before I reset it it would say not charging but it was still charging. This could be a hardware problem with the I pad.

    Explain exactly what "it won't work" means. The iPad does not respond at all? Does it turnn on? It is not recognized in iTunes when you connect it to your computer?

  • How do I download the music from iTunes onto an old iPhone 3gs? I have reset the iPhone to factory settings and when I plug it in it wants to use the device name for my current iPhone 5 and I don't want to change anything on my old iPhone.

    How do I download the music from iTunes onto an old iPhone 3gs without affecting my iPhone 5? I have reset the iPhone to factory settings and when I plug it in it wants to use the device name for my current iPhone 5 and I don't want to change anything on my current iPhone. iTunes won't let me change the device name for the 3gs. I just want to use the old iPhone like an touch.

    Hi littlemansa,
    If I am understanding you correctly, it sounds like you are returning to an old iPhone that has been erased and you would like to set it up as a new device without restoring from a backup which would affect the name of your iPhone, as well as the contents therein. I have an article for you that can help you set up your iPhone as a new device, and that information can be found below:
    How to erase your iOS device and then set it up as a new device or restore it from backups - Apple Support
    http://support.apple.com/en-us/HT4137
    Basically, it seems like you may be stuck on a screen that is asking if you would like to restore your device from the backup that is filed under the name of your new iPhone, or if you would like to set up the device as a new device. I would suggest that you set it up as a new device to achieve your desired results. 
    Take care, and thanks for visiting the Apple Support Communities.
    -Braden

  • I'm moving to an imac from a PC. I want to move my itunes library from the PC to my new imac. What is the best method and where can I find instructions?

    I'm moving to an imac from a PC. I want to move my itunes library from the PC to my new imac. What is the best method and where can I find instructions?

    Move iTunes from PC to MAC, http://www.macworld.com/article/146958/2010/03/move_itunes_windows_mac.html

  • HT201269 i bought my iphone 4 about a year ago and just decided to upgrade it to ios5 just recently. My phone restored to to its factory settings and when I clicked continue restore from back up it is now asking a backup password which i dont remember set

    i bought my iphone 4 about a year ago and just decided to upgrade it to ios5 just recently. My phone restored to to its factory settings and when I clicked continue restore from back up it is now asking a backup password which i dont remember setting up one. I am currently using a windows vista PC. How can I retrieve the password or restore all my old data into the new OS. I have been reading blogs about the same situation and is it true that it is one of apple's bugs?

    It is NOT a bug.  You provided a password.  If you can't remember it, you can't restore your phone from backup.  Very simple.

  • What's wrong with my iPhone? I put it on the charger it's says it's charging but nothing happens and when I use it while it's charging it starts to die and stops at 1%

    What's wrong with my iPhone? I put it on the charger it's says it's charging but nothing happens and when I use it while it's charging it starts to die and stops at 1%

    Please help

  • What is a cloned DataSource and when do we need it?

    Hi,
    Could anybody tell me what a cloned DataSource is and when we need it? How to make a clone from a datasource? Thanks

    When you need to perform more than one thing with a DataSource (like saving to a file and broadcast a webcam stream) you need to clone the WebCam Datasource to create two differents graphs, one for saving to a file, and other for broadcast.
    The cloned DataSource will only repeat the stream that the cloned DataSource is producing.
    Use the methdos im Manager to create a clone.
    RGB

  • What is a customer statement and when do we use it?

    Hi,
    What is a customer statement and when do we use it? An example in terms of business scenario would surely help me.

    Hi,
    In business sense Customer statment is the list of  transactions that were executed over a period of time.
    When ever customer buys the material from the company bill is generated and the same is debited to his account.
    whenever customer pays the amount to the company, the amount will be credited to his account.
    So the Customer statment will have the list of DEBIT and CREDIT entries.
    There will be Reconciliation for every quarter with the customer by the company sales executive and related price, discounts, freight which might be excess or less will be settled

  • I have IPhone 6 and when I using Viber Siri is actives itself ones I turn it off is actives my player itself as well. What can I do to stop that happen.

    I have IPhone 6 and when I using Viber Siri is actives itself ones I turn it off is actives my player itself as well. What can I do to stop that happen.

    Try turning off Siri in your Settings. Follow the steps on this page: http://www.imore.com/need-disable-notification-center-control-siri-access-lock-s creen-heres-how-ios-8
    If this doesn't help, try resetting your iPhone. This won't delete your data stored on the phone.
    Press and hold the Sleep/Wake button
    Press and hold on the Home button
    Keep holding both buttons until the display turns off and back on with Apple logo on it.
    Alternatively, you may go to Settings - General - Reset - Reset All Settings

  • I bought 120 gold in Haypi dragon on Mar 12 and 15 2012 , but I havenot received them yet.What is wrong with it ,and when i can reveive them ?

    I bought 120 gold in Haypi dragon on Mar 12 and 15 2012 , but I havenot received them yet.What is wrong with it ,and when i can reveive them ?

    For in-app purchases you should contact the developer's customer support, assuming there is one.

  • When to use PGA_AGGREGATE_TARGET and when to use temporary table space?

    Hi, all.
    I am a little confused with regard to when to use pga work area, and
    when to use temporary tablespace?
    Is there any one who could give me an advice or a document?
    Thanks in advance.
    Best Regards.
    Message was edited by:
    user507290

    You are more than a little confused... What has one to do with the other, do you think?
    All Oracle documentation is at http://tahiti.oracle.com - I suggest you start with the Concepts manual of your version

  • Muvo slim,What is displayed in window and does it use windows explorer drag and drop to trans

    Im about to buy a muvo slim and already own a zen xtra.Does muvo slim allow for recharge using plug accessory I got with my zen or can i only charge using usb cord and computer?What is displayed in screen and can i use drag and drop transfer method from windows explorer?Finally can i use the mediasource 3.20 softwear update i now use with my zen or must i input different mediasource version for slim than one i use with the zen? thanks matt

    mattg3 wrote:
    Im about to buy a muvo slim and already own a zen xtra.Does muvo slim allow for recharge using plug accessory I got with my zen or can i only charge using usb cord and computer?
    It charges via its USB socket, so you can't use the Xtra's power supply.
    What is displayed in screen and can i use drag and drop transfer method from windows explorer?
    It works as a flash dri've, so appears as a dri've letter in Windows Explorer.
    Finally can i use the mediasource 3.20 softwear update i now use with my zen or must i input different mediasource version for slim than one i use with the zen? thanks matt
    Go to Nomadworld.com downloads and you'll see the Slim listed there with MediaSource and the MuVo plugin.

  • When to use jsp,and when to use servlet?

    I think that jsp and servlet can realize the same functions, because when run a jsp, it is transferred to a servlet program, so when to use jsp and when to use servlet?
    I am now developing the input interface for a website, I just use jsp and javabean to connect to weblogic and database, and I didn't use servlet, Is there any unseemliness?
    Thank you!

    IMHO I use servlets to control the flow between my jsp's based on a number of factors in a webapp. For instance, user authorization. If a user has the authorization to conduct various administrative functions on an application (like change user rights, reset passwords etc) they will have access to specific buttons or links on some screens that others will not. I use servlets to establish what access rights a user has and direct them to the appropriate pages. I also use servlets to test data validity on form input screens. I know that I can also do this with JavaScript but that can be disabled by the client and in order to prevent that I also double check the form input from a servlet. All my jsp's do is display the results of a business process (which is held in a JavaBean or EJB) and the servlets act as the controllers for the application, connecting to multiple databases, verifying application state, flow control etc. I try to keep the jsp as simple as possible as some of them are maintained by html developers who lack the necessary experience to write java code. I hope this helps.

Maybe you are looking for