Stupid Question?

Ron Teitelbaum Ron at USMedRec.com
Wed Jan 16 18:20:44 UTC 2008


Hi Richard,

There are no Stupid ...  well you know.  

I thought I'd add to this thread with another answer.

There are plenty of ways to pass information into initialize.  You can use
any information that already exists and then just reference it in
initialize.

For example say you have some parameter that your application sets and you
want to use the value of that parameter in your class.

You could set your initialize to do something like this.

MyDomainObject>>initialize
	"set initial values"
	super initialize. 
	self myApplicationParameter: MyApplicationParameterClass
someParameter.


As long as you use information that exists somewhere else you can reference
it.

The issue that people are trying to point out is that if your information is
not static and changes with each instantiation then you are better building
an instance creation method.  This method moves allows you to setup an
object the way you need it during creation.  

For example

MyDomainObject class>>newWithApplicationParameter: aParameter
	"return to the sender an instance of MyDomainObject"
	^self new
		myApplicationParameter: aParameter;
		yourself

Now instead of calling:
	aDomainObject := MyDomainObject new.

You call
	aDomainObject := MyDomainObject newWithApplicationParameter:
MyApplicationParameterClass someParameter.

This makes much more sense since many people expect that initialize sets
either default values or sets up ivars with proper classes.

For Example you might use initialize to do something like this.

MyDomainObject>>initialize 
	"set initial values"
	super initialize.
	self collectionOfValues: OrderedCollection new.
	self initialState: #new.
	self applicationParameterHolder: (MyApplicationParameterHolder
newFor: self).

Hope that helps!

Happy Squeaking

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists


> From: Richard Eng
> 
> I have a stupid question:  How do you pass information into the
> #initialize
> method at object creation time? You can't use any of the instance methods
> or
> accessors because they don't yet exist! So how do you pass information in?
> 
> Thanks,
> Richard
> 
> 





More information about the Squeak-dev mailing list