comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Factory Pattern
Date: Wed, 25 Jul 2007 20:51:12 +0200
Date: 2007-07-25T20:51:07+02:00	[thread overview]
Message-ID: <ehg8y50sqs84$.2grfhftszzrg$.dlg@40tude.net> (raw)
In-Reply-To: 1185387571.367570.163160@r34g2000hsd.googlegroups.com

On Wed, 25 Jul 2007 11:19:31 -0700, shaunpatterson@gmail.com wrote:

> I'm just starting out learning Ada and I'm having some trouble
> converting some C++ code.
> 
> The last hurtle I'm having trouble with is implementing a Factory in
> Ada
> 
> I basically have a base class Message and all other messages are
> derived from this.
> I have a function call createMessage that I would like to return the
> appropriate subclass type.
> 
> My code looks something like this:
> 
> type Message is abstract tagged;

   -- Abstract factory, each non-abstract derived type will be forced to
   -- implement this:
function Create (I : Integer) return Message is abstract;

> type Message_Class is access all Message'Class;
> 
> derived type:
> 
> package MessageInteger_Pkg is
> 
> -- I want this to act like a constructor
> -- Just fills in the value
> function create (i : Integer) return MessageInteger;
> 
> private
> type MessageInteger with new Message with
> record
>       value : Integer;
> end record;
> 
> 
> end MessageInteger_Pkg;
> 
> -- create looks like this:
> function create (i : Integer) return MessageInteger is
>      msg : MessageInteger;
> begin
>      msg.value := i;
>      return msg;

You could use an extension aggregate here:

   return (Message with I);

> end create;
> 
> Factory method:
> 
> -- I would like it to work like this...
> function createMessage (type : Integer) return Message_Class is

"Type" is a reserved word.

> begin
>          case type is
>                  when 0 =>
>                       return MessageInteger_Pkg.create (1234);
>                  when others =>
>                        return MessageSomethingElse_Pkg.create
> ("blah");
>          end case
> 
> end createMessage

  function Factory (What : Integer) return Message'Class is
   begin
      case What is
         when 0 => return MessageInteger_Pkg.Create (1234);
         ...

> However - it doesn't like me returning the MessageInteger from a class
> that returns a Message_Class (obviously..)

[...]
> Perhaps I should move the "new"
> to the create function?

Yes, you you want to work with pointers. But what for? Ada can return
polymorphic objects on the stack. No need in pointers.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2007-07-25 18:51 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-25 18:19 Factory Pattern shaunpatterson
2007-07-25 18:28 ` Martin
2007-07-25 18:51 ` Dmitry A. Kazakov [this message]
2007-07-25 21:06   ` Georg Bauhaus
2007-07-25 19:27 ` Matthew Heaney
2007-07-26  0:51 ` Jeffrey R. Carter
2007-07-26  6:44   ` Maciej Sobczak
2007-07-26  8:40     ` Georg Bauhaus
2007-07-26  9:53       ` Dmitry A. Kazakov
2007-07-26 11:01         ` Georg Bauhaus
2007-07-26 13:02           ` Maciej Sobczak
2007-07-26 13:44             ` Dmitry A. Kazakov
2007-07-26 14:58             ` Georg Bauhaus
2007-07-26 22:31             ` Randy Brukardt
2007-07-27 13:07               ` Maciej Sobczak
2007-07-27 14:23                 ` shaunpatterson
2007-07-27 22:23                 ` Randy Brukardt
2007-07-28 18:56                   ` Maciej Sobczak
2007-07-29  7:54                   ` Maciej Sobczak
2007-07-29  8:53                     ` Dmitry A. Kazakov
2007-07-29 10:53                     ` Georg Bauhaus
2007-07-26 16:58         ` Adam Beneschan
2007-07-29 11:38         ` Manuel Gomez
2007-07-27 10:16     ` Jeffrey R. Carter
2007-07-27 12:47       ` Maciej Sobczak
2007-08-26  7:18         ` David Thompson
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox