Re: How to implement Factory Pattern in Java
From: Andrew Hobbs (andrewh1_NoSpam__at_iinet.net.au)
Date: 01/20/04
- Next message: Abraham Khalil: "CommunicationException in SerialContext lookup"
- Previous message: Ike: "Re: slow loading images"
- In reply to: Edward A Thompson: "How to implement Factory Pattern in Java"
- Next in thread: Ed Thompson: "Re: How to implement Factory Pattern in Java"
- Reply: Ed Thompson: "Re: How to implement Factory Pattern in Java"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 20 Jan 2004 08:23:56 +0800
"Edward A Thompson" <ed4becky_2000@yahoo.com> wrote in message
news:d913df37.0401190802.b3bba43@posting.google.com...
> Is there any reason that the Factory Pattern cannot be implmented with
> a static getClass method in the base class?
>
> Rather than instantiate a factory, then call its getClass method (2
> steps):
>
> CalculatorFactory f = new CalculatorFactory ();
> Calculator c = f.getClass(someIndentifyingString);
>
> I was thinking of instantiating the final class I want by calling a
> static method in the base class:
>
> Calculator c = Calculator.getClass(someIndentifyingString);
>
> Is there a flaw in this strategy? What am I missing?
Your suggestion is hardly a factory method. Not that it won't work, but you
are starting to mix code for how to make calculators with their functional
code. Plus it means the base class has to have code relating to its derived
classes. None of this is a good idea.
However you can still have a single call to a static method such as
Calculator c = CalculatorFactory.getClass(someIndentifyingString);
This doesn't mean you can't have the advantages of an instantiated object.
Just make the Factory class hold a singleton of itself and make the static
method call on that singleton.
Andrew
- Next message: Abraham Khalil: "CommunicationException in SerialContext lookup"
- Previous message: Ike: "Re: slow loading images"
- In reply to: Edward A Thompson: "How to implement Factory Pattern in Java"
- Next in thread: Ed Thompson: "Re: How to implement Factory Pattern in Java"
- Reply: Ed Thompson: "Re: How to implement Factory Pattern in Java"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|