1

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.

Geek
  • 26,489
  • 43
  • 149
  • 227

1 Answers1

2

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

etherous
  • 709
  • 4
  • 10
  • Does the EJB bean(in your case AccountBalanceBean) need to implement all the interfaces or is it optional? – Geek Jun 01 '14 at 08:28
  • @Geek As with all interfaces, your bean can implement as many or as few as you'd like. Obviously, if you didn't implement the WriteAccountBalance interface, no clients would have the ability to set the balance – etherous Jun 03 '14 at 03:15