Results 1 to 5 of 5
  1. #1
    Algatron is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default Q. about abstract factory

    Typical abstract factory might be something like this.
    Java Code:
    public interface Window {}
    
    public interface GUIfactory {
      public Window createWindow();
    }
    
    public class XYZWindow implements Window {}
    
    public XYZFactory implements GUIfactory {
      public Window createWindow() {  //note: return type Window
        return new XYZWindow();
      }
    }
    So this is basically an example that appears in a hundred books and websites. My question is this:

    Why does the XYZ implementation return a "window" instead of a "XYZWindow" ?

    I realize the GUIFactory interface says to return "Window"
    but XYZWindow IS a "Window" and java is ok with it...

    so basically why do we do as above, instead of this:
    Java Code:
    public XYZFactory implements GUIfactory {
      public [B]XYZWindow[/B] createWindow() {  //note: return type XYZWindow
        return new XYZWindow();
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    My take on this: XYZFactory implements GUIfactory, and so it must implement the methods declared in this interface, and the method signatures must match precisely. Since the interface declares that the method return the more general type, Window, any class that implements this interface has to adhere to this.

  3. #3
    Algatron is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    Yeah ok, I guess its only that, although it is legal java (according to eclipse at least) to make it
    public XYZWindow ...

    Thanks, I tend to over think things too much :)

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Doing that would no longer make it "abstract". The purpose of an abstract factory is to present you with an object - a Window - without bothering you with the tiresome and difficult details of how exactly it works and where it came from. The details - like how you draw pretty corners - are abstracted away.

  5. #5
    Algatron is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    Ah good point. It was bothering me that no one ever returns an XYZWindow.... but thats the whole point of the "abstraction".

    Thanks everyone.

Similar Threads

  1. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  2. Replies: 2
    Last Post: 11-21-2008, 12:20 AM
  3. Replies: 6
    Last Post: 10-27-2008, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •