Dave,
In our FoodSmart example (http://www.JavaSuccess.com), we demonstrate a
technique for handling this problem. These patterns mostly apply to domain
objects; they tend not to apply to EJB's because they have their own
techniques for homes to handle instance creation.
As you pointed out, Java objects really like to have zero-argument
constructors, so we provide one of those for each class. We may also provide
multiple-argument constructors for convenience. However, we find
constructors that throw exceptions problematic and tend to avoid those. This
limits how much work a constructor can really do and how useful they really
are. Thus we often just implement the zero-argument constructor and it does
little more than call the super-implementor. We may make the constructor(s)
protected because we don't expect them to be used by clients, but they can
be called by subclasses.
None of that solves your problem, though. What does is that we also
implement several static methods called new<ObjectType> with different
numbers/types of parameters that all return an instance of the class. Each
one throws our CouldNotInstantiateException. The ObjectType is the same as
the class' type, which for most purposes can just be the class. Different
implementors accept different parameters. Each implementor is designed to
either return a valid instance with the parameters specified or to throw
CouldNotInstantiateException.
Each static new<ObjectType> instantiator delegates the heavy lifting of
setting the new instance to a valid state using instance methods, either
initialize() (no parameters) or initializeWith(<parameters>). Because these
are instance methods, their implementations will automatically be inherited,
unlike static methods. If an initialize method fails, it throws our own
CouldNotInitializeException, which the instantiator can catch and transform
into CouldNotInstantiateException.
So with one simple zero-argument constructor and several static
new<ObjectType> methods, you can be Java compliant and still use
instantiators that produce valid instances.
If you download the FoodSmart example, I suggest you take a look at the
com.gemstone.gps.egrocer.domain classes like Food, GroceryChainCustomer, and
Category.
Bobby
-----
Bobby Woolf
Senior Architect
GemStone Systems, a Brokat company
[EMAIL PROTECTED]
-----Original Message-----
From: Dave Ford [mailto:[EMAIL PROTECTED]]
Sent: Friday, February 02, 2001 00:33
To: [EMAIL PROTECTED]
Subject: [EJB-INT] Create methods (and constructors generally)
As part of my object oriented education, I was taught that the purpose of
the constructor is to initialize the object into an "valid state". So, for
example, take an object of type Person that has 3 instance variables:
Person
firstName
lastName
status
Of these three instance variables, 2 can be considered "required"
(firstName,lastName) and 1 will have a default value (status="active").
Thus, Person might have a 2-arg contructor like this:
public Person(String firstName,String lastName){
this.setFirstName(firstName);
this.setLastName(lastName);
this.setStatus("active");
}
My question is this:
1. Are people actually following this theory?
2. Doesn't the JavaBeans (non-ejb) requirement for a zero arg construcotr
kill this idea?
3. In EJB (where ejbCreate actually allows arguments) are people using this
startegy?
4. Say you have an ejb with 25 instance variables, 5 of which have
reasonable default values, would you create an ejbCreate method with 20
arguments?
Dave Ford
Smart Soft - The Java Training Company
http://www.SmartSoftTraining.com
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".