Hi,
I have a class A that creates instances of another class B and does stuff to them: A new doStuff the 'new' method creates the B instances and the doStuff... well, does the stuff. :-) I just created a subclass of B, let's call it B'. I want to use the same class A to do stuff with B and B' instances and was considering the following solution: Create a new method for A that takes a symbol with the name of B or B', create instances using that symbol and do stuff to them: A new: #B doStuff How can I create instances using the class name as a symbol? O:-) Is this a good design? BTW, would it be possible for that method to take a parameter with a default value? So, if no parameter is passed, A should create B instances, and if a parameter (class name) is passed, crate the appropriate instances. Is this possible in Smalltalk? Is it advisable? O:-) Or should I just create 2 different methods (one with and the other without a parameter)? O:-) Thanks O:-) |
On Fri, 03 Dec 2004 18:42:13 +0100,
Fernando <[hidden email]> wrote: > > I have a class A that creates instances of another class B and does > stuff to them: > > A new doStuff > > the 'new' method creates the B instances and the doStuff... well, does > the stuff. :-) > > I just created a subclass of B, let's call it B'. > > I want to use the same class A to do stuff with B and B' instances and > was considering the following solution: > > Create a new method for A that takes a symbol with the name of B or > B', create instances using that symbol and do stuff to them: > > A new: #B doStuff What about (A new: B) doStuff There's no need to use a symbol, when you can use the class itself. Implementation sample: A class>>new: aClass created := aClass new > > How can I create instances using the class name as a symbol? O:-) Is > this a good design? This depends on what you want to achieve with it. It is good, because it is compact and easy to read, it is bad, because you have no way of modifying the creation of B instances. > > BTW, would it be possible for that method to take a parameter with a > default value? So, if no parameter is passed, A should create B > instances, and if a parameter (class name) is passed, crate the > appropriate instances. This is usually done with A class>>new self new: B A class>>new: aClass created := aClass new > > Is this possible in Smalltalk? Is it advisable? O:-) Or should I just > create 2 different methods (one with and the other without a > parameter)? O:-) It is possible, it might be suitable, and that's how it's done. > > Thanks O:-) I just need to know: What's that thing on your head? s. |
On 3 Dec 2004 19:45:07 GMT, Stefan Schmiedl <[hidden email]> wrote:
>> I want to use the same class A to do stuff with B and B' instances and >> was considering the following solution: >> >> Create a new method for A that takes a symbol with the name of B or >> B', create instances using that symbol and do stuff to them: >> >> A new: #B doStuff > >What about > (A new: B) doStuff > >There's no need to use a symbol, when you can use the class itself. You can do that??!! =:-O I'm starting to like this Smalltalk thing, although my mind still needs to be 'deceeplusplusified'. ;-) I wonder how long it takes to remove all the residues and fully recover from the Stroustrup syndrome.... >> Thanks O:-) > >I just need to know: What's that thing on your head? An aureole. ;-) |
Fernando,
> You can do that??!! =:-O In Smalltalk classes are objects so whatever you can do with a "normal" object you can do with a class. I'm not completely sure what you are wanting to do, or what is or is not a suitable way of doing it, but to answer your original question about Symbols. You can create an instance of a class using it's Symbolic name by looking it up in the SystemDictionary aka Smalltalk.... (Smalltalk at: #OrderedCollection) new has the same effect as OrderedCollection new -- Ian Use the Reply-To address to contact me. Mail sent to the From address is ignored. |
In reply to this post by Fernando Rodríguez
On Sat, 04 Dec 2004 12:48:33 +0100,
Fernando <[hidden email]> wrote: >> >>There's no need to use a symbol, when you can use the class itself. > > You can do that??!! =:-O Repeat after me: Everything is an object. If this does not suffice, try: Everything responds to messages. Then take a deep breath and make yourself familiar with the messages available. Maybe you'll need two deep breaths for that. >>> Thanks O:-) >> >>I just need to know: What's that thing on your head? > > An aureole. ;-) Well, being a being with an aureole should be of tremendous help when needing superior support. s. |
In reply to this post by Fernando Rodríguez
"Fernando" <[hidden email]> wrote in message
news:[hidden email]... > Hi, > > I have a class A that creates instances of another class B and does > stuff to them: > > A new doStuff > > the 'new' method creates the B instances and the doStuff... well, does > the stuff. :-) > > I just created a subclass of B, let's call it B'. > > I want to use the same class A to do stuff with B and B' instances and > was considering the following solution: > > Create a new method for A that takes a symbol with the name of B or > B', create instances using that symbol and do stuff to them: > > A new: #B doStuff > > How can I create instances using the class name as a symbol? O:-) Is > this a good design? > > BTW, would it be possible for that method to take a parameter with a > default value? So, if no parameter is passed, A should create B > instances, and if a parameter (class name) is passed, crate the > appropriate instances. > > Is this possible in Smalltalk? Is it advisable? O:-) Or should I just > create 2 different methods (one with and the other without a > parameter)? O:-) > > Thanks O:-) It's tough to say "do exactly this" when I don't know enough about your problem. Here are some thoughts... First, don't use new or new: to create instances of another class. It's better to write A newSomeFlavorOfB. newSomeFlavorOfB could also include doStuff. This name is not meant to be an excellent example. Use a name that describes what you are doing. There was some good discussion of this in the earlier "Object creation" thread. Although anything is always possible, I feel weird about classes creating instances of other classes. Is this really the best approach? I can't tell, because I don't know enough about your problem. It's just one of those things that requires thought to do the right thing. If you absolutely must, you can actually use the name of the class to find the class itself and then ask it to make a new instance of itself. But I think it's probably not the best solution to what you are trying to achieve. Maybe a little more detail would help. In general, when you are trying to do anything, try to stay focused on the problem. I spent a lot of time helping customers solve problems. I got many questions about how to do something, where that something was a particular operation that would in itself be time consuming to do, or difficult to achieve. When I asked what problem they were trying to solve, I would see that there was a better way to approach it, where my product had just the right functions to implement that approach. Your question falls into this category. If we see the broader question, we would likely be able to give you a better approach to solving your problem. Now I yield to other masters in this newsgroup, who will give you other ideas that I have not touched upon. |
Free forum by Nabble | Edit this page |