Extendable factory

Discussion in 'C++' started by Mark, May 29, 2012.

  1. Mark

    Mark Guest

    I wish to develop an extendable object factory for a library I am
    writing. The library is to provide a simplified interface to a
    messaging application. Most of the work is done within the library
    but the user has to override a class and provide the implementation to
    a single virtual function (to read data).

    It's easy to provide factory methods for the built in classes but I am
    struggling to think how to provide a factory method for classes that
    are added later.

    I know I could allow them to create the objects directly but I would
    rather avoid this because it would expose some unneccessary details of
    the implementation of the library itself.
    --
    (\__/) M.
    (='.'=) If a man stands in a forest and no woman is around
    (")_(") is he still wrong?
     
    Mark, May 29, 2012
    #1
    1. Advertising

  2. Mark

    Luca Risolia Guest

    On 29/05/2012 12:03, Mark wrote:
    > I wish to develop an extendable object factory for a library I am
    > writing. The library is to provide a simplified interface to a
    > messaging application. Most of the work is done within the library
    > but the user has to override a class and provide the implementation to
    > a single virtual function (to read data).
    >
    > It's easy to provide factory methods for the built in classes but I am
    > struggling to think how to provide a factory method for classes that
    > are added later.
    >
    > I know I could allow them to create the objects directly but I would
    > rather avoid this because it would expose some unneccessary details of
    > the implementation of the library itself.


    There exist various creational patterns other than the Factory Method:
    Abstract Factory, Builder, Prototype just to name a few. Look at "Design
    Patterns" by GoF for more informations.
     
    Luca Risolia, May 29, 2012
    #2
    1. Advertising

  3. Mark

    Guest

    On Tuesday, May 29, 2012 11:03:54 AM UTC+1, Mark wrote:
    > I wish to develop an extendable object factory for a library I am
    > writing. The library is to provide a simplified interface to a
    > messaging application. Most of the work is done within the library
    > but the user has to override a class and provide the implementation to
    > a single virtual function (to read data).
    >
    > It's easy to provide factory methods for the built in classes but I am
    > struggling to think how to provide a factory method for classes that
    > are added later.
    >
    > I know I could allow them to create the objects directly but I would
    > rather avoid this because it would expose some unneccessary details of
    > the implementation of the library itself.


    have the new classes register themselves with the factory and provide a static method the factory can call to create them. Another way is to have objects clone themselves.
     
    , May 29, 2012
    #3
  4. Mark

    Mark Guest

    On Tue, 29 May 2012 13:02:45 +0200, Luca Risolia
    <> wrote:

    >On 29/05/2012 12:03, Mark wrote:
    >> I wish to develop an extendable object factory for a library I am
    >> writing. The library is to provide a simplified interface to a
    >> messaging application. Most of the work is done within the library
    >> but the user has to override a class and provide the implementation to
    >> a single virtual function (to read data).
    >>
    >> It's easy to provide factory methods for the built in classes but I am
    >> struggling to think how to provide a factory method for classes that
    >> are added later.
    >>
    >> I know I could allow them to create the objects directly but I would
    >> rather avoid this because it would expose some unneccessary details of
    >> the implementation of the library itself.

    >
    >There exist various creational patterns other than the Factory Method:
    >Abstract Factory, Builder, Prototype just to name a few. Look at "Design
    >Patterns" by GoF for more informations.


    I've seen many examples of these. From what I can see I need a
    special kind of Factory Method -- where it can create any derived
    class. Builders, Abstract Factories, Prototypes are for different
    purposes.
    --
    (\__/) M.
    (='.'=) If a man stands in a forest and no woman is around
    (")_(") is he still wrong?
     
    Mark, May 29, 2012
    #4
  5. Mark

    Luca Risolia Guest

    On 29/05/2012 16:32, Mark wrote:
    > I've seen many examples of these. From what I can see I need a
    > special kind of Factory Method -- where it can create any derived
    > class. Builders, Abstract Factories, Prototypes are for different
    > purposes.


    One way is to register your factories (pointers to function) in a map
    that you give to the user, i.e. :

    shapes["circle"] = Circle::create; // registration
    shapes["triangle"] = Triangle::create;

    Shape* s = shapes["circle"](); // creation

    f(s); // use

    where f() might be:

    void f(Shape* s) { // accept any shape
    s->rotate() // rotate() is virtual
    }

    See Stroustrup "The C++ Programming Language", chapter 25 for more
    informations. Look this to have an idea:

    http://www.linux-projects.org/listing/cpp_solutions/25.1/main.cpp
     
    Luca Risolia, May 29, 2012
    #5
  6. Mark

    Mark Guest

    On Tue, 29 May 2012 17:03:55 +0200, Luca Risolia
    <> wrote:

    >On 29/05/2012 16:32, Mark wrote:
    >> I've seen many examples of these. From what I can see I need a
    >> special kind of Factory Method -- where it can create any derived
    >> class. Builders, Abstract Factories, Prototypes are for different
    >> purposes.

    >
    >One way is to register your factories (pointers to function) in a map
    >that you give to the user, i.e. :
    >
    >shapes["circle"] = Circle::create; // registration
    >shapes["triangle"] = Triangle::create;
    >
    >Shape* s = shapes["circle"](); // creation
    >
    >f(s); // use
    >
    >where f() might be:
    >
    >void f(Shape* s) { // accept any shape
    > s->rotate() // rotate() is virtual
    >}
    >
    >See Stroustrup "The C++ Programming Language", chapter 25 for more
    >informations. Look this to have an idea:


    Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
    date anyway now?

    >http://www.linux-projects.org/listing/cpp_solutions/25.1/main.cpp


    Thanks for this. I think I can adapt this approach. Please keep
    watching this thread though :)
    --
    (\__/) M.
    (='.'=) If a man stands in a forest and no woman is around
    (")_(") is he still wrong?
     
    Mark, May 30, 2012
    #6
  7. Mark

    Ian Collins Guest

    On 05/30/12 08:51 PM, Mark wrote:
    > On Tue, 29 May 2012 17:03:55 +0200, Luca Risolia wrote:
    >>
    >> See Stroustrup "The C++ Programming Language", chapter 25 for more
    >> informations. Look this to have an idea:

    >
    > Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
    > date anyway now?


    It'll only be out of date if and when there's a new edition!

    Some books never date.

    --
    Ian Collins
     
    Ian Collins, May 30, 2012
    #7
  8. Mark

    Mark Guest

    On Wed, 30 May 2012 21:09:02 +1200, Ian Collins <>
    wrote:

    >On 05/30/12 08:51 PM, Mark wrote:
    >> On Tue, 29 May 2012 17:03:55 +0200, Luca Risolia wrote:
    >>>
    >>> See Stroustrup "The C++ Programming Language", chapter 25 for more
    >>> informations. Look this to have an idea:

    >>
    >> Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
    >> date anyway now?

    >
    >It'll only be out of date if and when there's a new edition!
    >
    >Some books never date.


    But hasn't C++ changed quite a bit since the last edition?
    --
    (\__/) M.
    (='.'=) If a man stands in a forest and no woman is around
    (")_(") is he still wrong?
     
    Mark, May 30, 2012
    #8
  9. Mark

    Ian Collins Guest

    On 05/30/12 09:35 PM, Mark wrote:
    > On Wed, 30 May 2012 21:09:02 +1200, Ian Collins<>
    > wrote:
    >
    >> On 05/30/12 08:51 PM, Mark wrote:
    >>> On Tue, 29 May 2012 17:03:55 +0200, Luca Risolia wrote:
    >>>>
    >>>> See Stroustrup "The C++ Programming Language", chapter 25 for more
    >>>> informations. Look this to have an idea:
    >>>
    >>> Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
    >>> date anyway now?

    >>
    >> It'll only be out of date if and when there's a new edition!
    >>
    >> Some books never date.

    >
    > But hasn't C++ changed quite a bit since the last edition?


    The language may have been through a revision, but the fundamentals
    remain the same.

    --
    Ian Collins
     
    Ian Collins, May 30, 2012
    #9
  10. Mark

    Guest

    On Wednesday, May 30, 2012 10:09:02 AM UTC+1, Ian Collins wrote:
    > On 05/30/12 08:51 PM, Mark wrote:
    > > On Tue, 29 May 2012 17:03:55 +0200, Luca Risolia wrote:


    > >> See Stroustrup "The C++ Programming Language", chapter 25 for more
    > >> informations. Look this to have an idea:

    > >
    > > Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
    > > date anyway now?

    >
    > It'll only be out of date if and when there's a new edition!


    editions 1 and 2 are out of date. Ed 1 is almost dangerous!

    > Some books never date.


    you obviously weren't around whrn C++ was new!
     
    , May 30, 2012
    #10
  11. Mark

    Ian Collins Guest

    On 05/31/12 12:35 AM, wrote:
    > On Wednesday, May 30, 2012 10:09:02 AM UTC+1, Ian Collins wrote:
    >> On 05/30/12 08:51 PM, Mark wrote:
    >>> On Tue, 29 May 2012 17:03:55 +0200, Luca Risolia wrote:

    >
    >>>> See Stroustrup "The C++ Programming Language", chapter 25 for more
    >>>> informations. Look this to have an idea:
    >>>
    >>> Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
    >>> date anyway now?

    >>
    >> It'll only be out of date if and when there's a new edition!

    >
    > editions 1 and 2 are out of date. Ed 1 is almost dangerous!


    That's way I said they go out of date when replaced.

    >> Some books never date.

    >
    > you obviously weren't around whrn C++ was new!


    Oh but I was, I still a have full set of Stroustrups!

    --
    Ian Collins
     
    Ian Collins, May 30, 2012
    #11
  12. Mark

    Mark Guest

    On Wed, 30 May 2012 09:51:55 +0100, Mark
    <> wrote:

    >On Tue, 29 May 2012 17:03:55 +0200, Luca Risolia
    ><> wrote:
    >
    >>On 29/05/2012 16:32, Mark wrote:
    >>> I've seen many examples of these. From what I can see I need a
    >>> special kind of Factory Method -- where it can create any derived
    >>> class. Builders, Abstract Factories, Prototypes are for different
    >>> purposes.

    >>
    >>One way is to register your factories (pointers to function) in a map
    >>that you give to the user, i.e. :
    >>
    >>shapes["circle"] = Circle::create; // registration
    >>shapes["triangle"] = Triangle::create;
    >>
    >>Shape* s = shapes["circle"](); // creation
    >>
    >>f(s); // use
    >>
    >>where f() might be:
    >>
    >>void f(Shape* s) { // accept any shape
    >> s->rotate() // rotate() is virtual
    >>}
    >>
    >>See Stroustrup "The C++ Programming Language", chapter 25 for more
    >>informations. Look this to have an idea:

    >
    >Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
    >date anyway now?
    >
    >>http://www.linux-projects.org/listing/cpp_solutions/25.1/main.cpp

    >
    >Thanks for this. I think I can adapt this approach. Please keep
    >watching this thread though :)


    I'm still struggling with this. If I create an example can someone
    look at it, please?
    --
    (\__/) M.
    (='.'=) If a man stands in a forest and no woman is around
    (")_(") is he still wrong?
     
    Mark, May 31, 2012
    #12
  13. Mark

    Mark Guest

    On Thu, 31 May 2012 11:04:22 +0100, Mark
    <> wrote:

    >On Wed, 30 May 2012 09:51:55 +0100, Mark
    ><> wrote:
    >
    >>On Tue, 29 May 2012 17:03:55 +0200, Luca Risolia
    >><> wrote:
    >>
    >>>On 29/05/2012 16:32, Mark wrote:
    >>>> I've seen many examples of these. From what I can see I need a
    >>>> special kind of Factory Method -- where it can create any derived
    >>>> class. Builders, Abstract Factories, Prototypes are for different
    >>>> purposes.
    >>>
    >>>One way is to register your factories (pointers to function) in a map
    >>>that you give to the user, i.e. :
    >>>
    >>>shapes["circle"] = Circle::create; // registration
    >>>shapes["triangle"] = Triangle::create;
    >>>
    >>>Shape* s = shapes["circle"](); // creation
    >>>
    >>>f(s); // use
    >>>
    >>>where f() might be:
    >>>
    >>>void f(Shape* s) { // accept any shape
    >>> s->rotate() // rotate() is virtual
    >>>}
    >>>
    >>>See Stroustrup "The C++ Programming Language", chapter 25 for more
    >>>informations. Look this to have an idea:

    >>
    >>Our copy of Stroustrup dissappeared long ago. Wouldn't it be out of
    >>date anyway now?
    >>
    >>>http://www.linux-projects.org/listing/cpp_solutions/25.1/main.cpp

    >>
    >>Thanks for this. I think I can adapt this approach. Please keep
    >>watching this thread though :)

    >
    >I'm still struggling with this. If I create an example can someone
    >look at it, please?


    I've solved it now so no need to worry. Thanks for the help.
    --
    (\__/) M.
    (='.'=) If a man stands in a forest and no woman is around
    (")_(") is he still wrong?
     
    Mark, May 31, 2012
    #13
    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. Ivan Vecerina

    Re: Extendable envelope/letter idiom

    Ivan Vecerina, Jun 24, 2003, in forum: C++
    Replies:
    0
    Views:
    1,141
    Ivan Vecerina
    Jun 24, 2003
  2. Medi Montaseri
    Replies:
    17
    Views:
    919
    Medi Montaseri
    Sep 3, 2003
  3. Replies:
    1
    Views:
    908
    Oliver Wong
    Jul 10, 2006
  4. Tobias Pfeiffer

    Coding an extendable class

    Tobias Pfeiffer, Dec 30, 2003, in forum: Python
    Replies:
    2
    Views:
    362
    Peter Otten
    Dec 30, 2003
  5. C#
    Replies:
    4
    Views:
    440
Loading...

Share This Page