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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,174ec7dc941a1068 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!news.glorb.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Factory Pattern Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1185387571.367570.163160@r34g2000hsd.googlegroups.com> Date: Wed, 25 Jul 2007 20:51:12 +0200 Message-ID: NNTP-Posting-Date: 25 Jul 2007 20:51:07 CEST NNTP-Posting-Host: f2d57f43.newsspool1.arcor-online.net X-Trace: DXC=Vk8`FcbeG9deoCI^f\Y]Eaic==]BZ:afn4Fo<]lROoRaFl8W>\BH3YbiG79[537SkdDNcfSJ;bb[eIRnRBaCd 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