"Hans" == Hans Gruber rukin@web.de writes:
Hans> Now to my question:
Hans> How may I check wheater an instance of a class (in my case MyFileLibrary) has been created and exists at the time I check it.
Hans> (MyFileLibrary = nil) is always false.
Hans> I need this, because wenn an Instance of MyFileLibrary exists at the Hans> checking time, I want to proceed with an other method than I want to Hans> proceed with when an instance of MyFileLibrary does not exists.
Sounds like you want the singleton pattern.
The easiest way to do that is to not create instances at all, and just dump all of your methods class side, and all of your "instance" variables as "class instance" variables.
Then treat MyFileLibrary as if it is the only instance that ever exists, and has a global name.
If that's not what you want, explain more.
-- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/ Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
Hi, thanks for the fast answer.
Let me explain my problem a bit more detailed.
I have 3 Morphs and each of them belong to different classes. What I want to achieve is, that one of the morphs only calls special methods during intialization (in the initialize method) when one of the other morphs has been created before and still exists/is active (so the morph has not been deleted or destroyed).
Maybe some pseudo-code to emphasize my problem:
Initializtion-Method of the morph which dependent initialization:
initialize (Class1 = nil) <---- here should be the Check wheater an Object of Class Xy has been created. ifTrue: [call special methods] ifFalse: [call normal methods] ______________________________________________________ GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT! Jetzt freischalten unter http://movieflat.web.de
On 23.12.2009, at 03:40, Hans Gruber wrote:
> "Hans" == Hans Gruber rukin@web.de writes:
Hans> Now to my question:
Hans> How may I check wheater an instance of a class (in my case MyFileLibrary) has been created and exists at the time I check it.
Hans> (MyFileLibrary = nil) is always false.
Hans> I need this, because wenn an Instance of MyFileLibrary exists at the Hans> checking time, I want to proceed with an other method than I want to Hans> proceed with when an instance of MyFileLibrary does not exists.
Sounds like you want the singleton pattern.
The easiest way to do that is to not create instances at all, and just dump all of your methods class side, and all of your "instance" variables as "class instance" variables.
Then treat MyFileLibrary as if it is the only instance that ever exists, and has a global name.
If that's not what you want, explain more.
-- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/ Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
Hi, thanks for the fast answer.
Let me explain my problem a bit more detailed.
I have 3 Morphs and each of them belong to different classes. What I want to achieve is, that one of the morphs only calls special methods during intialization (in the initialize method) when one of the other morphs has been created before and still exists/is active (so the morph has not been deleted or destroyed).
Maybe some pseudo-code to emphasize my problem:
Initializtion-Method of the morph which dependent initialization:
initialize (Class1 = nil) <---- here should be the Check wheater an Object of Class Xy has been created. ifTrue: [call special methods] ifFalse: [call normal methods]
This would be a highly unusual way of programming. Normally you would not check if there exists some object in the world. If your object depends on another object, a parent object should give that to you and you store a reference in an instance variable.
That said, you could use this
World findA: Class1
but as you can already see from the global reference to World this smells of bad code. There are needs for this but I can't imagine why you would need it in a beginner's project ;)
- Bert -
Hi Hans,
--- On Wed, 12/23/09, Bert Freudenberg bert@freudenbergs.de wrote:
From: Bert Freudenberg bert@freudenbergs.de Subject: Re: [Newbies] Check wheater an instance of a class is actually there or not To: "A friendly place to get answers to even the most basic questions about Squeak." beginners@lists.squeakfoundation.org Date: Wednesday, December 23, 2009, 4:21 AM On 23.12.2009, at 03:40, Hans Gruber wrote:
>> "Hans" == Hans Gruber rukin@web.de
writes:
Hans> Now to my question:
Hans> How may I check wheater an instance of a
class (in my case MyFileLibrary) has been created and exists at the time I check it.
Hans> (MyFileLibrary = nil) is always false.
Hans> I need this, because wenn an Instance of
MyFileLibrary exists at the
Hans> checking time, I want to proceed with an
other method than I want to
Hans> proceed with when an instance of
MyFileLibrary does not exists.
Sounds like you want the singleton pattern.
The easiest way to do that is to not create
instances at all,
and just dump all of your methods class side, and
all of your "instance"
variables as "class instance" variables.
Then treat MyFileLibrary as if it is the only
instance that ever exists,
and has a global name.
If that's not what you want, explain more.
-- Randal L. Schwartz - Stonehenge Consulting
Services, Inc. - +1 503 777 0095
URL:http://www.stonehenge.com/merlyn/
Smalltalk/Perl/Unix consulting, Technical writing,
Comedy, etc. etc.
Smalltalk and Seaside discussion
Hi, thanks for the fast answer.
Let me explain my problem a bit more detailed.
I have 3 Morphs and each of them belong to different
classes.
What I want to achieve is, that one of the morphs only
calls special methods during intialization (in the initialize method) when one of the other morphs has been created before and still exists/is active (so the morph has not been deleted or destroyed).
Maybe some pseudo-code to emphasize my problem:
Initializtion-Method of the morph which dependent
initialization:
initialize (Class1 = nil) <---- here should be the Check
wheater an Object of Class Xy has been created.
ifTrue: [call special methods] ifFalse: [call normal methods]
This would be a highly unusual way of programming. Normally you would not check if there exists some object in the world. If your object depends on another object, a parent object should give that to you and you store a reference in an instance variable.
That said, you could use this
World findA: Class1
but as you can already see from the global reference to World this smells of bad code. There are needs for this but I can't imagine why you would need it in a beginner's project ;)
- Bert -
I agree with Bert. Reaching into other objects for information is the hard way. Think about what you are doing again.
First step is to have a simple to describe user story about what you what to accomplish. It should light on detail. Just enough tell when you have achieved what you wished to do.
Next think about the objects, their natural roles and their natural responsibilities. Who needs to collaborate with whom? Who needs to know about whom?
If something needs to be initialized then just tell it to initialize itself and let it take care of the details. It can always do a lazy initialization because it can check itself to see if its has already done it once. If this is too new for you look for examples in the code of already existing classes.
From what you have writen it also looks like you are confusing a class with its instances. ClassFoo will never equal nil even if it has no instances.
ClassFoo allInstances size = 0 . would be true in that case.
ClassFoo allInstances select: [ :each | each name = 'myInstancesName' ] .
Is one way of sorting through a classes instances and selecting a particular one. If it doesn't exist you get an empty list which you can check for.
Here I am assuming that each instance has a name and that you know the one you want. If not you set up the block to filter on some other criteria that fits the situation.
So homework is: 1) Write down a short descriptive user story about the end task you are trying to accomplish.
2) Let us know.
3) Start rethinking your objects.
Let me know if any of this helps. Please excuse the brusque tone, I wanted to get this out before I sleep.
Yours in curiosity and service, --Jerome Peace
Whenever you create MyFileLibrary, you need to store it in a particular variable "aFileLibrary". That means you need an object that owns the variable. Let's call the class of this object MyApplication (since I have no idea what your application is; this is actually a horrible name and I'm sure you can do better). This object might be the container of the morphs.
Hans> I need this, because wenn an Instance of MyFileLibrary exists at the Hans> checking time, I want to proceed with an other method than I want to Hans> proceed with when an instance of MyFileLibrary does not exists.
This is probably a method of MyApplication. It checks if aFileLibrary s nil and does the appropriate action. If it is the container of the morphs then the morphs can just send to their container (or parent, I forget what it is called). In general, morphs should be as decoupled from each other as possible, so it is usually not a good idea for them all to have varibles that reference each other. Even sending specialized messages to their parent is a little odd, but it is a good compromise.
Of course, you have told us very little. The odds are good that there is something simpler that would work better.
-Ralph Johnson
2009/12/23 Hans Gruber rukin@web.de:
> "Hans" == Hans Gruber rukin@web.de writes:
Hi, thanks for the fast answer.
Let me explain my problem a bit more detailed.
I have 3 Morphs and each of them belong to different classes. What I want to achieve is, that one of the morphs only calls special methods during intialization (in the initialize method) when one of the other morphs has been created before and still exists/is active (so the morph has not been deleted or destroyed).
I think this is more of a design than a language issue. Have a look at the mediator design pattern: http://en.wikipedia.org/wiki/Mediator_pattern
beginners@lists.squeakfoundation.org