Why a single EJB session bean is allowed to have multiple interfaces?

583 Views Asked by At

I am learning about EJB 3.0 from the book EJB 3 in Action. In the section under The anatomy of a session bean it is mentioned that :

An interface through which a client invokes the bean is called a business interface. This interface essentially defines the bean methods appropriate for access through a specific access mechanism. The interesting thing to note right now is the fact that a single EJB can have multiple interfaces. In other words, EJB implementation classes can be polymorphic, meaning that different clients using different interfaces could use them in completely different ways.

I want to understand the reasoning for the design decision why a single EJB is allowed to have multiple interfaces? An example to help understand the concept here would be very helpful.

1

There are 1 best solutions below

2
On BEST ANSWER

The reason is simple. Each interface is supposed to be accessible by a particular type of client. For example, say you're implementing a banking system, and create a bean for balance access. In this case, you might use two interfaces. One for reading the balance, and one for changing it

public interface ReadAccountBalance
{
    float getBalance ();
}

public interface WriteAccountBalance
{
    void setBalance (float balance);
}

public class AccountBalanceBean implements ReadAccountBalance, WriteAccountBalance
{
    ...
}

Now, you can distribute ReadAccountBalance with client packages needing only account balance read access, while WriteAccountBalance would only be distributed with clients needing to actually modify the balance