Java EE 中的 Facade 有什么意义?

发布于 2024-10-14 05:57:11 字数 2002 浏览 10 评论 0原文

我不太明白门面的意义。

public abstract class AbstractFacade<T> {

    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
}

如果我有这段代码,那么我就有一个像这样的 EJB。

@Stateless
public class WrapSpecFacade extends AbstractFacade<WrapSpec> {
    @PersistenceContext
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public WrapSpecFacade() {
        super(WrapSpec.class);
    }

}

这有什么意义呢?为什么称其为门面?对我来说,它只是一个将类似功能分组的抽象类。谢谢。

I'm not really understanding the point of a facade.

public abstract class AbstractFacade<T> {

    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
}

If I have this code and then I have an EJB like this.

@Stateless
public class WrapSpecFacade extends AbstractFacade<WrapSpec> {
    @PersistenceContext
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public WrapSpecFacade() {
        super(WrapSpec.class);
    }

}

What is the point of this? Why call this a facade? To me it's just an abstract class that groups similar functionality. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞慾 2024-10-21 05:57:11

外观是一种设计模式。模式,软件模式,是一组规则,用于组织代码并为其提供一定的结构。某些目标可以通过使用模式来实现。
设计应用程序时使用设计模式。

Facade 模式允许程序员为对象创建一个简单的接口来使用其他对象。
考虑使用一组非常复杂的类,所有类都实现自己的接口。
好吧,您希望提供一个接口来仅公开您拥有的众多功能中的某些功能。
通过这样做,您可以实现代码简单性、灵活性、集成性和松散耦合。

在您的示例中,外观用于管理许多参与者之间的耦合。这是一个设计问题。当许多组件一起交互时,它们联系得越多,维护它们就越困难(我的意思是代码维护)。 Facade 允许您达到松散耦合,这是程序员应该始终努力达到的目标。

考虑以下事项:

public class MyClass1 implements Interface1 {
   public void call1() {}
   public call call2() {}
}

public class MyClass2 implements Interface2 {
   public void call3() {}
   public void call4() {}
}

public class MyClass {
   private MyClass1 a;
   private MyClass2 b;
   //calling methods call1 call2 call3 and call4 in other methods of this class
   ...
   ...
}

如果您必须通过不更改接口来更改 call1 或 call2 使用的类中的业务逻辑...,则不需要更改所有这些类,而只需更改其中之一所使用的方法内的类前两个类的接口方法。

Facade 可以让你改进这个机制。

我很抱歉,但我意识到它看起来并不那么美妙。设计模式在软件行业中被大量使用,它们在处理大型项目时非常有用。
您可能会指出,您的项目并没有那么大,这可能是真的,但 Java EE 旨在帮助业务和企业级应用程序编程。这就是为什么有时默认使用外观模式(某些 IDE 也使用它)。

Facade is a design pattern. A pattern, a software pattern, is a set of rules in order to organize code and provide a certain structure to it. Some goals can be reached by using a pattern.
A design pattern is used when designing the application.

The Facade pattern allows programmers to create a simple interface for objects to use other objects.
Consider working with a very complex group of classes, all implementing their own interfaces.
Well, you want to provide an interface to expose only some functionality of the many you have.
By doing so, you achieve code simplicity, flexibility, integration and loose-coupling.

Facade, in your example, is used in order to manage coupling between many actors. It is a design issue. When you have many components interacting together, the more they are tied the harder it will be to maintain them (I mean code maintenance). Facade allows you to reach loose coupling, which is a goal a programmer should always try to reach.

Consider the following:

public class MyClass1 implements Interface1 {
   public void call1() {}
   public call call2() {}
}

public class MyClass2 implements Interface2 {
   public void call3() {}
   public void call4() {}
}

public class MyClass {
   private MyClass1 a;
   private MyClass2 b;
   //calling methods call1 call2 call3 and call4 in other methods of this class
   ...
   ...
}

If you had to change business logic located in a class used by call1 or call2... by not changing the interface, you would not need to change all these classes, but just the class inside the method used by one of the interface methods of the first two classes.

Facade lets you improve this mechanism.

I am sorry but I realize that it does not look so wonderful. Design patterns are heavily used in the software industry and they can be very useful when working on large projects.
You might point out that your project is not that large and that may be true, but Java EE aims to help business and enterprise-level application programming. That's why sometimes the facade pattern is used by default (some IDEs use it too).

蓝色星空 2024-10-21 05:57:11

通常,此模式用于隐藏其提供接口的底层类的实现,或者简化可能复杂的事物的底层实现。

外观可能向外界提供一个简单的接口,但在幕后做一些事情,比如创建其他类的实例、管理事务、处理文件或 TCP/IP 连接——所有这些都可以通过简化的接口来屏蔽。

Typically this pattern is used to either hide the implementation of the underlying classes it is presenting an interface for, or to simplify the underlying implementation of something that may be complex.

A facade may present a simple interface to the outside world, but under the hood do things like create instances of other classes, manage transactions, handle files or TCP/IP connections -- all stuff that you can be shielded from by the simplified interface.

倦话 2024-10-21 05:57:11

在您的特定环境中,这并不是真正的外观。该代码中的内容基本上是一个 DAO(数据访问对象)。

DAO 可以看作是 DB 操作的 Facade,但这不是它的主要目的。它主要是为了隐藏数据库内部。在您的示例中,如果您将底层存储系统切换到 XML 文件或某些键值存储(例如 HBase),您仍然可以使用该“Facade”中定义的方法,并且不需要在客户端代码中进行任何更改。

(传统)外观处理需要向客户隐藏的复杂设计。您无需公开复杂的 API 和复杂的流程(从该服务获取此信息,将其传递给此转换器,获取结果并用此验证它,然后将其发送到另一个服务),您只需将所有这些封装在 Facade 中,然后简单地向客户公开一个简单的方法。这样,除了您的 API 更易于使用之外,您还可以自由地更改底层(复杂)实现,而不会破坏您的客户端代码。

In your particular context, this is not really a Facade. What you have in that code is basically a DAO (Data Access Object).

A DAO can be seen as a Facade for DB operations, but this is not its main purpose. It mainly intends to hide the DB internals. In your example, if you're switching the underlying storage system to XML files or to some key-value store like HBase, you can still use the methods defined in that "Facade" and no change is required in the client code.

A (traditional) Facade deals with complex designs that need to be hidden from the clients. Instead of exposing a complex API and complex flows (get this from this service, pass it to this converter, get the result and validate it with this and then send it to this other service), you just encapsulate all that in a Facade and simply expose a simple method to clients. This way, along with the fact that your API is a lot easier to use, you are also free to change the underlying (complex) implementation without breaking your clients code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文