Why use facade pattern in EJB?

2019-05-25 03:57发布

问题:

I've read through this article trying to understand why you want a session bean in between the client and entity bean. Is it because by letting the client access entity bean directly you would let the client know exactly all about the database?

So by having middleman (the session bean) you would only let the client know part of the database by implementing the business logic in some certain way. So only part of the database which is relevant to the client is only visible. Possibly also increase the security.

Is the above statement true?

回答1:

  • Avoiding tight coupling between the client & the business objects, increasing manageability.
  • Reducing fine-grained method invocations, leads to minimize method invocation calls over the network, providing coarse-grained access to clients.
  • Can have centralized security & transaction constraints.
  • Greater flexibility & ability to cope with changes.
  • Exposing only required & providing simpler interface to the clients, hiding the underlying complexity and inner details, interdependencies between business components.


回答2:

The article you cite is COMPLETELY out of date. Check the date, it's from 2002.

There is no such thing anymore as an entity bean in EJB (they are currently retained for backwards compatibility, but are on the verge of being purged completely). Entity beans where awkward things; a model object (e.g. Person) that lives completely in the container and where access to every property of it (e.g. getName, getAge) required a remote container call.

In this time and age, we have JPA entities that are POJOs and contain only data. Don't confuse a JPA entity with this ancient EJB entity bean. They sound similar but are completely different things. JPA entities can be safely send to a (remote) client. If you are really concerned that the names used in your entity reveal your DB structure, you could use XML mapping files instead of annotations and use completely different names.

That said, session beans can still perfectly be used to implement the Facade pattern if that's needed. This pattern is indeed used to give clients a simplified and often restricted view of your system. It's just that the idea of using session beans as a Facade for entity beans is completely outdated.



回答3:

It is to simplify the work of the client. The Facade presents a simple interface and hides the complexity of the model from the client. It also makes it possible for the model to change without affecting the client, as long as the facade does not change its interface.



回答4:

It decouples application logic with the business logic.
So the actual data structures and implementation can change without breaking existing code utilizing the APIs.
Of course it hides the data structure from "unknown" applications if you expose your beans to external networks



标签: ejb facade