Generic Abstract Factory

Discussion in 'C++' started by Sebastian Faust, Oct 27, 2003.

  1. Hi,

    I am currently reading the chapter about abstract factorys from "Modern C++
    Design" and wondering how it would be possible to create an object which has
    a constructor with parameters. At the moment I can't see any way how to
    solve this problem? Is there any way how to implement such?
    Furthermore I read in the GoF book that usually Abstract Factorys are
    implemented using Factory Methods. At the moment I can't see where in the
    generic case of Abstract Factory, Factory Methods are used.

    Thanks in advance
    Sebastian
    Sebastian Faust, Oct 27, 2003
    #1
    1. Advertising

  2. Sebastian Faust wrote:
    > Hi,
    >
    > I am currently reading the chapter about abstract factorys from "Modern C++
    > Design" and wondering how it would be possible to create an object which has
    > a constructor with parameters. At the moment I can't see any way how to
    > solve this problem? Is there any way how to implement such?
    > Furthermore I read in the GoF book that usually Abstract Factorys are
    > implemented using Factory Methods. At the moment I can't see where in the
    > generic case of Abstract Factory, Factory Methods are used.
    >
    > Thanks in advance
    > Sebastian
    >
    >


    Here is what I use:
    class Any_Class
    : public Some_Base_Class
    {
    public:
    Any_Class(/* deault parameters */);
    Any_Class(const Any_Class& ac); // copy constructor
    Any_Class(istream& inp); // construct from text stream
    virtual ~Any_Class();

    static Some_Base_Class * create(istream& inp,
    unsigned int id);
    };

    Some_Base_Class *
    Any_Class ::
    create(istream& inp, unsigned int id)
    {
    Some_Base_Class * p_base(NULL);
    if (id = ID_ANY_CLASS)
    {
    p_base = new Any_Class(inp);
    }
    return p_base;
    }


    In the factory:
    typedef Some_Base_Class * (*P_CREATE_FUNC)(istream& inp, id);

    const P_CREATE_FUNC creation_table[] =
    {
    Any_Class::create()
    };
    const unsigned int NUM_CREATION_FUNCS = sizeof(creation_table)
    / sizeof(creation_table[0]);

    Some_Base_Class *
    Factory ::
    create(istream& inp)
    {
    unsigned int id;
    Some_Base_Class * p_base(NULL);

    inp >> id;
    for (unsigned int i = 0;
    (p_base == NULL) && (i < NUM_CREATION_FUNCS); ++i)
    {
    p_base = (creation_table)(inp, id);
    }
    return p_base;
    }

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
    Thomas Matthews, Oct 27, 2003
    #2
    1. Advertising

  3. Sebastian Faust

    Noah Roberts Guest

    Sebastian Faust wrote:
    > Hi,
    >
    > I am currently reading the chapter about abstract factorys from "Modern C++
    > Design" and wondering how it would be possible to create an object which has
    > a constructor with parameters.


    No. But you can use the prototype pattern as Sebastian recommends.

    --
    Noah Roberts
    - "If you are not outraged, you are not paying attention."
    Noah Roberts, Oct 27, 2003
    #3
  4. Sebastian Faust wrote:
    > Hi,
    >
    > I am currently reading the chapter about abstract factorys from "Modern C++
    > Design" and wondering how it would be possible to create an object which has
    > a constructor with parameters. At the moment I can't see any way how to
    > solve this problem? Is there any way how to implement such?
    > Furthermore I read in the GoF book that usually Abstract Factorys are
    > implemented using Factory Methods. At the moment I can't see where in the
    > generic case of Abstract Factory, Factory Methods are used.
    >


    Here is a complete generic factory template library that allows you to
    create objects with various parameters (0 ... 3 args - but easily extended).

    It's a tad long so I placed it on my web server.

    http://mariani.ws/~gianni/at_factory.h

    This is going to be part of the "Austria C++" library I'm toying with.

    I'll be doing V 0.1 soon so critique (preferably friendly but I'll
    accept anything) is welcome.

    Copyright is mine. Use it via the GPL - I can be arm twisted to do LGPL.
    Gianni Mariani, Oct 27, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christer
    Replies:
    3
    Views:
    957
    Drebin
    Aug 27, 2003
  2. =?Utf-8?B?bWF4?=

    Design Pattern - Abstract Factory

    =?Utf-8?B?bWF4?=, Oct 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    650
    tdavisjr
    Oct 18, 2005
  3. Medi Montaseri
    Replies:
    17
    Views:
    852
    Medi Montaseri
    Sep 3, 2003
  4. Charles Krug

    File Paramaterized Abstract Factory

    Charles Krug, Jan 11, 2006, in forum: Python
    Replies:
    2
    Views:
    401
    Charles Krug
    Jan 11, 2006
  5. C#
    Replies:
    4
    Views:
    398
Loading...

Share This Page