Confusion in factory method pattern.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumittyagi
    Recognized Expert New Member
    • Mar 2007
    • 202

    #1

    Confusion in factory method pattern.

    Hi all,
    I was reading factory method pattern from wikipedia link.

    There are some limitations mentioned three limitations mentioned there.
    These are all fine. But there is a conclusion statement written, which I am not able to get.
    "All three problems can be alleviated by making factories first-class class members rather than using the design pattern"

    Limitations mentioned there are as follows.

    1*The first limitation is that refactoring an existing class to use factories breaks existing clients. For example, if class Complex was a standard class, it might have numerous clients with code like:
    [code=java]Complex c = new Complex(-1, 0);[/code]
    Once we realize that two different factories are needed, we change the class (to the code shown above). But since the constructor is now private, the existing client code no longer compiles.

    2*The second limitation is that, since the pattern relies on using a private constructor, the class cannot be extended. Any subclass must invoke the inherited constructor, but this cannot be done if that constructor is private.

    3*The third limitation is that, if we do extend the class (e.g., by making the constructor protected -- this is risky but possible), the subclass must provide its own re-implementation of all factory methods with exactly the same signatures. For example, if class StrangeComplex extends Complex, then unless StrangeComplex provides its own version of all factory methods, the call StrangeComplex. fromPolar(1, pi) will yield an instance of Complex (the superclass) rather than the expected instance of the subclass.


    Any assistance will be greatly appreciated.

    Regards,
    Sumit
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sumittyagi
    Hi all,
    I was reading factory method pattern from wikipedia link.

    There are some limitations mentioned three limitations mentioned there.
    These are all fine. But there is a conclusion statement written, which I am not able to get.
    "All three problems can be alleviated by making factories first-class class members rather than using the design pattern"

    Limitations mentioned there are as follows.

    1*The first limitation is that refactoring an existing class to use factories breaks existing clients. For example, if class Complex was a standard class, it might have numerous clients with code like:
    [code=java]Complex c = new Complex(-1, 0);[/code]
    Once we realize that two different factories are needed, we change the class (to the code shown above). But since the constructor is now private, the existing client code no longer compiles.

    2*The second limitation is that, since the pattern relies on using a private constructor, the class cannot be extended. Any subclass must invoke the inherited constructor, but this cannot be done if that constructor is private.

    3*The third limitation is that, if we do extend the class (e.g., by making the constructor protected -- this is risky but possible), the subclass must provide its own re-implementation of all factory methods with exactly the same signatures. For example, if class StrangeComplex extends Complex, then unless StrangeComplex provides its own version of all factory methods, the call StrangeComplex. fromPolar(1, pi) will yield an instance of Complex (the superclass) rather than the expected instance of the subclass.


    Any assistance will be greatly appreciated.

    Regards,
    Sumit
    So which part are you getting confused with?

    P.S It is best to study patterns using the patterns bible itself (Gang of four)

    Comment

    • sumittyagi
      Recognized Expert New Member
      • Mar 2007
      • 202

      #3
      Originally posted by r035198x
      So which part are you getting confused with?

      P.S It is best to study patterns using the patterns bible itself (Gang of four)
      I am confused in, what the writer wants to say by this statement.

      "All three problems can be alleviated by making factories first-class class members rather than using the design pattern"

      *what he want to say by first-class class members?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sumittyagi
        I am confused in, what the writer wants to say by this statement.

        "All three problems can be alleviated by making factories first-class class members rather than using the design pattern"

        *what he want to say by first-class class members?
        See the wiki entry for first-class members

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by sumittyagi
          *what he want to say by first-class class members?
          Nothing much; he explained what could go wrong and what would be difficult to
          implement. If some hypothetical language supports factories *in the language itself*,
          as if they were first class citizens, the language itself would (or should) solve
          these problems. Just wishful thinking.

          kind regards,

          Jos

          Comment

          • sumittyagi
            Recognized Expert New Member
            • Mar 2007
            • 202

            #6
            Thanks a lot.
            I got the point.

            Comment

            Working...