From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,174ec7dc941a1068,start X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!r34g2000hsd.googlegroups.com!not-for-mail From: shaunpatterson@gmail.com Newsgroups: comp.lang.ada Subject: Factory Pattern Date: Wed, 25 Jul 2007 11:19:31 -0700 Organization: http://groups.google.com Message-ID: <1185387571.367570.163160@r34g2000hsd.googlegroups.com> NNTP-Posting-Host: 155.219.241.10 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1185387571 6214 127.0.0.1 (25 Jul 2007 18:19:31 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 25 Jul 2007 18:19:31 +0000 (UTC) User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30; .NET CLR 1.1.4322),gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: r34g2000hsd.googlegroups.com; posting-host=155.219.241.10; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Xref: g2news2.google.com comp.lang.ada:1177 Date: 2007-07-25T11:19:31-07:00 List-Id: 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