From: shaunpatterson@gmail.com
Subject: Factory Pattern
Date: Wed, 25 Jul 2007 11:19:31 -0700
Date: 2007-07-25T11:19:31-07:00 [thread overview]
Message-ID: <1185387571.367570.163160@r34g2000hsd.googlegroups.com> (raw)
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;
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;
end create;
Factory method:
-- I would like it to work like this...
function createMessage (type : Integer) return Message_Class is
begin
case type is
when 0 =>
return MessageInteger_Pkg.create (1234);
when others =>
return MessageSomethingElse_Pkg.create
("blah");
end case
end createMessage
However - it doesn't like me returning the MessageInteger from a
class
that returns a Message_Class (obviously..)
Now if I do this -- changing the constructor to take in a
Message_Class....
function createMessage (type : Integer) return Message_Class is
decodedMessage : Message_Class;
begin
case type is
when 0 =>
decodedMessage := new
MessageInteger_Pkg.MessageInteger;
MessageInteger_Pkg.create (decodedMessage,
1234); -- change the decoded message this way
return decodedMessage;
when others =>
return MessageSomethingElse_Pkg.create
("blah");
end case
end createMessage
-- That method works... but I find the intermediary step of making a
new object decodedMessage
to someone unneeded? There must be another way of doing this.
Perhaps I should move the "new"
to the create function?
And... I'm not completely sure I'm doing all this correctly in Ada.
LIke I said, I'm very new to
Ada.
Thanks
--
Shaun
next reply other threads:[~2007-07-25 18:19 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-07-25 18:19 shaunpatterson [this message]
2007-07-25 18:28 ` Factory Pattern Martin
2007-07-25 18:51 ` Dmitry A. Kazakov
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