Results 1 to 11 of 11

Thread: Factory design pattern issue

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Factory design pattern issue

    Quote Originally Posted by ayanda83 View Post
    Thank for your reply wysota but I am a little confused by the statement you made above. I think this will defeat the purpose of "inheritance", if I have include all the properties I can possibly want for the derived classes in the base class. Maybe I am misunderstand you, but in my understanding, you are saying the member variable and functions for derived classes like "Circle", "Square", "Rectangle" ect, must all be defined in the base class "Shape". If I do this - what would be the point of deriving the classes like "Circle", "Square", etc for shape?
    Inheritance is a much broader use-case than the Factory pattern. Factory makes use of inheritance to do its job, so its scope is much more narrow than that of inheritance itself. The goal of using a factory method is that the caller doesn't have to know the internal details of the object he wants created. If you have a factory method for "Shape objects" (ie. method returning Shape*) then it means you are not interested in what the shape actually is and you just want to handle it via the Shape interface regardless if it is a Circle, Rectangle or Square. The point of using a factory is that you don't directly tell what class of object you want created. You simply pass some parameters to the factory method and it is the factory method that makes a choice of class based on those parameters.

    Consider an abstract example that you want to buy a car but you don't care what brand and model it is going to be as long as it suits your preferences. So you go to the car dealer and tell him that you want a red sedan car with electric mirrors, navigation and parking assistance. The dealer makes a choice for you and sells you ("instantiates") a particular concrete brand, model and version of a car. Then you use the car as any other car, regardless what brand and version of the car this is. You go to your friends and say "Hey, I bought a car today!" and not "Hey I bought a car of brand X, model Y in version Z today!" (in our words, you use it through the Car interface and not through "XYZCar" interface).

    If that's a problem for you then maybe you don't want a factory at all. There is a number of other patterns you can employ for different needs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. The following user says thank you to wysota for this useful post:

    ayanda83 (27th August 2014)

Similar Threads

  1. Facade design pattern implementation
    By ehnuh in forum General Programming
    Replies: 5
    Last Post: 2nd November 2012, 16:37
  2. QObject factory (singleton pattern)
    By area51 in forum Qt Programming
    Replies: 8
    Last Post: 24th June 2010, 14:55
  3. Design pattern of qwt library
    By Hogwarts in forum Qwt
    Replies: 1
    Last Post: 4th June 2010, 10:04
  4. GIS design pattern
    By olosie in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2009, 16:19
  5. Observer Design Pattern
    By joseph in forum General Programming
    Replies: 1
    Last Post: 21st January 2008, 12:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.