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 {

Similar Messages

  • 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

  • What is repair request? when we use this?

    Hi all,
    what is repair request?
    when actually we use this?
    Thanks& Regards,
    Ravi Alakuntla.

    Hi Ravi,
    When you find some records missing when deltas are running then instead of going for re-init and again deltas you can go for repair full request giving selection conditions for those missing records, without affecting your delta laods.
    Hope it helps,
    Sunil.

  • 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

  • I want to buy a hard case for my macbook pro, but I don't know what year it is. When I use the serial number to try and find out, all it says is, "~VIN,MacBook Pro (15-inch Glossy)". When I type that into amazon for a case it gives me nothing!

    I want to buy a hard case for my macbook pro, but I don't know what year it is. When I use the serial number to try and find out, all it says is, "~VIN,MacBook Pro (15-inch Glossy)". When I type that into amazon for a case it gives me nothing!

    Hi T,
    Either of these will give you the info you seek:
    http://www.appleserialnumberinfo.com/Desktop/index.php
    http://www.chipmunk.nl/klantenservice/applemodel.html

  • HT201060 What happens to iTunes credit when you use family sharing.

    What happens to iTunes credit when you use family sharing.

    Making purchases
    After you set up your family, any time a family member initiates a new purchase it will be billed directly to your account unless that family member has gift or store credit. First, their store credit will be used to pay the partial or total bill. The remainder will bill to the family organizer. As the family organizer, any receipts generated by the transaction will be sent to you. Learn more about how iTunes Store purchases are billed.
    Family purchases and payments

  • When to use "this." when not to ?

    Hi, I know this will be a basic question, but can someone tell me the rule of when to use this.method/variable/etc and when not to ?
    If I have (And I'll cut down the code,leaving construtors, etc.)
    public abstract class DataStuff
    protected String message = null;
    protected void clearMessage()
    this.message = null; // Do I use this.
    message = null; // Or not ?
    } // End clearMessage()
    } // End Class ---------
    Lets get more complicated
    public class MoreStuff extends DataStuff
    public void someMethod()
    this.message = "Do I use This ?";
    message = "Or Do I not ?";
    this.clearMessage(); // or
    clearMessage();
    } // End someMethod()
    } // End Class ------
    I know this will be fairly simple, and I am sure there are lots of Tutorials I could not find that explain the difference between "this"
    thanks
    Paul

    Besides using the this reference for instance variables that have the same name as a local variable, it is also used when your class implements an interface and uses it in the class. Let me further explain. Lets pretend that you create a JFrame that has buttons on it. You want your buttons to handle events so you add an ActionListener to the buttons. You also want to code your event handling in the JFrame so you cause your JFrame to implement the ActionListener interface and implement the required method actionPerformed() in your JFrame class. Well, when you add and actionListener to the buttons, you have to use the this reference in the method signature. See code for example.import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class ExampleJFrame extends JFrame implements ActionListener {
       public ExampleJFrame() {
          super("Example using 'this' reference!");
          Container c = getContentPane();
          JButton button = new JButton("Push Me");
          /* Here is the example of how the 'this' reference is
             used.  Since this class implements ActionListener,
             that means it is an ActionListener.  So, when
             we add the method addActionListener to the button,
             it requires that an ActionListener be put into
             the method signature.  So, I just pass a reference
             to this class into it (using the 'this' reference).
             Its actionPerformed method will be called.  There
             are 3 ways I could think of to handle the
             events for this button.  One is to do it the way
             I chose to do it; that is, have the originating
             class (ExampleJFrame) handle it.  That is where
             the 'this' reference comes into play.  Two is
             to use an anonymous inner class.  The second
             option is useful if the code is small and
             self contained.  Three is to have another
             class handle it, such as an Action. */
          button.addActionListener(this);
          c.add(button);
          /* incidentally, you could put the this keyword in
             front of each of these methods for readability,
             but most people don't. I used it on the first
             one to demonstrate it.  */
                   //cause the program to end when closing
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          pack();
          setVisible(true);
       }//end constructor
       /*=**************************************************
       * actionPerformed(): required ActionListener method *
       public void actionPerformed(ActionEvent e) {
          /* the this reference is also used in here because
             the showMessageDialog() should have as its
             parent a component and we want the component to
             be this JFrame */
          JOptionPane.showMessageDialog(this,"You used the this pointer!");
       }//end actionPerformed
       public static void main(String args[]) {
          /* when you run this class, a small JFrame with
             a button will appear in the top left corner. */
          ExampleJFrame app = new ExampleJFrame();
       }//end main
    }//end ExampleJFrame classtajenkins

  • HT202802 What "security vulnerability" will be opened by using this signing technique?

    Regarding article: HT202802
    OS X: Using AppleScript with Accessibility and Security features in Mavericks - Apple Support
    The article says:
    Important: Signing an applet using the following method introduces a security vulnerability that could allow malicious software to use Accessibility without user permission.
    1. What "security vulnerability" will be opened by using this signing technique?
    2. Does signing this way only make the App its applied to vulnerable only? and then the whole computer vulnerable depending on how extensive the app's reach is to the rest of the computer?
    3. More information: My app only relates to the Reminders app and bunch of Finder items....nothing internet based, etc.  That being said, is this still a vulnerability to my computer?
    "Note: If you have your own signing identity, you may use that identity in place of “-” for the -s option." 
    1. What is "my own signing identity?" and if I don't have one, would it add security to get one and use it here?
    Thanks for the help in advance!

    1) There are a few system features, including accessibility, that will override any and all other security protections on you machine. This is the vulnerability. In giving the script the ability to control your machine, you give control of your machine to the script.
    2) By signing the script, that control is permanent. If the app doesn't do anything malicious, there is no problem. But malicious apps sometimes don't manifest until later.
    3) Did you write the app? If so, then there is nothing to worry about. If not, then how much do you trust the author of the app?
    Generally, this isn't too big a deal. Apple is very protective, but most people generally hand over their passwords to anyone. They shouldn't, of course, but generally they do. They don't realize the extent to which they have handed over control of their machine and all of their data. Apple is trying to point that out.

  • I want to get a Nikon D3100, but my macbook has OS 10.5.5 on it and I've read that the D3100 needs at least 10.6.5. What will happen if I try to use this camera with my computer? Will iPhoto simply not recognize it or will I get distorted pictures, or..?

    I want to get a Nikon D3100, but my macbook has OS 10.5.5 on it and I've read that the D3100 needs at least 10.6.5. What will happen if I try to use this camera with my computer? Will iPhoto simply not recognize it or will I get distorted pictures, or....what?

    andyBall_uk wrote:
    The manual says
    ❚❚ Supported Operating Systems The supplied software can be used with computers running the following operating systems: •          Windows: <snip>
    •          Macintosh: Mac OS X (version 10.4.11, 10.5.8, 10.6.4)
    Nikon say that you need 10.6.5+ for the latest firmware, although I don't see why.
    Thanks for that! I was looking for that manual, but for some reason, couldn't find it.
    I guess I'll have to contact Nikon and find out what the real story is. The camera was introduced in '08 or '09, so I'm wondering why you'd need software as recent as 10.6.5??
    Still very confusing.  :-?
    Why does it state that the camera is compatible with "10.4.11, 10.5.8..."? Does that mean that anything between 4.11 and 5.8 is incompatible???
    BTW, I tried downloading the 10.5.8 combo update recently and got an error message that the update was "corrupted" after it was all downloaded, so I was not able to install that.

  • What is a factory method

    Can anyone tell me wat exactly is a factory method??

    Native classs..
    Java code cannot directly make calls to the local
    operating system to access disk files, run programs,
    use network resources, or query databases. Instead,
    such requests must be made indirectly through a
    native class. what's a native class, hot-chips?
    To build a native class, the programmer starts with
    Java code that defines the field variables and the
    names and arguments of all methods. The methods
    contain no code. This file is then processed by a
    utility that converts it into C. The rest of the
    programming is done with the native C language and
    all the subroutines available through it. not a native class. there's no such thing. you mean native methods, hot-chips. a class can happily mix both java and native methods. Object.class, for example
    other native languages than 'C' are available

  • Few HFM questions, What is XBRL tages? when we use it?  User defined1,2,3?

    Hi professionals,
    1) What is XBRL tags? I never use it in HFM. I want to know it in a smile way with an example if possible
    2) What is user Defined 1,2,3. HFM application run by many users. So how they share it and what it means exactly?
    Please revert me
    Thanks
    Sarath

    1.) XBRL - http://en.wikipedia.org/wiki/XBRL
    You may or may not use this depending on the particular business.
    2.) UD1, UD2, UD3 are what you want them to be. They are basically "extra" dimensions or work areas which you can take advantage of. For example we use one of the UD fields to link our accounts with Tax Stream (I think it's One Source now....).
    If you end up in a position where you need more than 3 UD fields, you can subdivide them by. For instance if I put a value in UD1 like cou=US,con=NA I could use the split command on the field and divide out the values into unique pieces, etc.

  • When to use 'this' keyword

    Hi,
    I saw some code for a class which extended JFrame
    at the end of the constructor it says:
    this.setVisible(true);
    but of course it works when we have just:
    setVisible(true);
    I know how to use 'this' in other ways but what is the point of having it here
    Cheers
    Jim

    'this' is used for making differences between class variables and local variables.
    public class Test {
    private int value;
    public void setValue(int value) {
    this.value=value;
    regards
    Stas

  • Privacy has ruined my app store. What the world would be when we saw this:

    专业代购软件
    评论人: A我的电脑5
    为解决众多iphone,ipad用户没有信用卡的烦恼,特推出代购正版iphone,ipad软件业务。本店可以代购itunes软件商城所有的软件和游戏!超低价格代 购! 支持淘宝,财富通,工农中建四大网银转账,方便快捷,安全省心! 联系qq:107904328
    For brief translation, the guy above just what to buy all the apps, and then shares his/her account to all the people in China. And whats moreover, Apple has allowed this kind of behavior to share one app among different deviced. So this disgusting behavior really badlly hurt those who choose to pay for those softwares, and turned out to be dishonest.
    Why cant Apple forbid this kind of disgusting messages among their customer commendation and even forbid this kind of SECOND SELLER ?
    Can even imagine when I saw this message right after I bought several hundreds dollars of apps.
    What about set a better limit on the app account sharing issue?
    Really hope Apple can fix this, to bring us the light and hope for future.
    P.S I`m not a developer, at least so far. But I`m really a person who loves to see different kind outstanding softwares come from whatever places of the world. But those low-moral Chinese really badly wounded me. So angry about that.

    http://www.apple.com/support/mac/app-store/contact.html?form=account

  • I have macbook pro 2012. I 'm using netbeans 7.2 for using programming. It took about 700mb when I use this application.

    I have macbook pro 2012 4gb ram. I 'm using netbeans 7.2 for using programming. It took about 700mb.

    I'm runing out of memeory when I use other applicaiton such as safari with netbens and inactive memory not clearing when not enough memory for other application.Why is this happing? Does netbens application leak memory?

  • Fn + Spacebar - when to use this?

    I have never seen Fn + Spacebar do anything.  On the paper that came with the ThinkPad, it says this magnifies the screen contents.  But nothing happens when using this with the net, or looking at an image, so when does this work?

    It should've came preloaded. If you installed a stock OS, then it would've been part of the Hotkeys Integrations package.
    It's just how it's done. Better than a huge EXE always staying resident to handle every possible hotkey action.
    W520: i7-2720QM, Q2000M at 1080/688/1376, 21GB RAM, 500GB + 750GB HDD, FHD screen
    X61T: L7500, 3GB RAM, 500GB HDD, XGA screen, Ultrabase
    Y3P: 5Y70, 8GB RAM, 256GB SSD, QHD+ screen

Maybe you are looking for

  • How to Enter a Checkmark in Captivate 5

    I want to have a box on a screen that when the learner clicks it they will see a checkmark appear.  I am using Captivate 5.  Any help is greatly appreciated.

  • JDBC drivers for SQL Server 2008 and NW 7.00 (UD connect)

    HI, we recently updated a UD connect Source DB fronm SQL Server 2000 to 2008; currently struggling to find a compatible JDBC driver; does anyone have a UD connect or PI system connected to a SQL 2008 DB via JDBC drivers?

  • Validating parser ignoring start tag?

    Hello There appears to be a discrepency between what (I think a) validating parser (SAXParser) should do and what it's doing. An echo program using the following DTD <?xml version='1.0' encoding='utf-8'?> <!-- Document Type Definition for the AList.x

  • Problem with beeping sounds on laptop

    I have a Compaq nc8000 laptop that I've had for a couple of months now and all of a sudden the other day, it started making this beeping noise. It a short beep that comes like every 5 to 10 minutes. Can anyone help me?

  • Integration of CRM with BW

    does anyone have materials related to CRM extraction into BW system ? i will be looking forward to response . thanks ajit