AFAUI EJBHome is an
Abstract Factory.
"new" and the class constructors are great for creating simple, unconnected objects but once you start creating interrelated "objects" things get more complicated and you tend to create "factories" that know how to create things that are actually composites or aggregation of objects.
Look at a
JDBC connection - you don't create it with a "new", you get it from the DriverManager. The DriverManager is a
Factory. Under the hood the connection object the DriverManager creates is interconnected with lots of other details that you don't want to deal with - but DriverManager (the
Factory) knows what needs to be done.
Beans cannot exist in a vacuum - they need the
EJB container. And the client doesn't actually talk to the bean - it talks to an object that implements the EJBObject interface and your component interface. So when a client requests access to a bean there is quite a lot to do.
Furthermore the client doesn�t initially have access to the
Factory that can give it "access to the bean". It has to get it through JNDI � so there has to be one single facility that all clients can use to access beans of a certain type � while there will be many bean instances of that type.
So EJBHome is an
Abstract Factory.
You create a home interface the extends EJBHome.
Ultimately the container implements the class that becomes the
Factory that implements your home interface which manages/orchestrates the bean/EJBObject lifecycle for you.
From a class design perspective it makes sense to separate bean lifecycle management from the bean and its EJBObject:
There is only one Factory instance (accessible via JNDI) but there will be many "EJBObjects".A class should do one single job and it should do that well. Being responsible for orchestration of the creation and destruction of all those beans/EJBObjects for one bean type is big enough for one class (EJBHome). Implementing the component interface with all the container intercepts is another one (EJBObject), and finally there is the business logic (your Bean). [ August 19, 2005: Message edited by: Peer Reynders ]