简体   繁体   English

Java EE中的Facade有什么意义?

[英]What is the point of a Facade in Java EE?

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. 如果我有这个代码然后我有这样的EJB。

@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. 谢谢。

Facade is a design pattern. Facade是一种设计模式。 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. Facade模式允许程序员为对象创建一个简单的界面来使用其他对象。 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. 在您的示例中,Facade用于管理许多actor之间的耦合。 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. Facade允许您达到松耦合,这是程序员应该总是试图达到的目标。

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. 如果必须通过不更改接口来更改位于call1或call2所使用的类中的业务逻辑,则不需要更改所有这些类,而只需更改其中一个接口方法所使用的方法中的类。前两个班。

Facade lets you improve this mechanism. Facade可以让您改进这种机制。

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. 您可能会指出您的项目不是那么大而且可能是真的,但Java EE旨在帮助业务和企业级应用程序编程。 That's why sometimes the facade pattern is used by default (some IDEs use it too). 这就是为什么有时默认使用Facade模式(某些IDE也使用它)。

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. 外观可以为外部世界提供一个简单的界面,但是在底层可以创建其他类的实例,管理事务,处理文件或TCP / IP连接 - 所有这些都可以通过简化的界面来屏蔽。

In your particular context, this is not really a Facade. 在您的特定背景下,这不是一个真正的Facade。 What you have in that code is basically a DAO (Data Access Object). 你在该代码中拥有的基本上是DAO(数据访问对象)。

A DAO can be seen as a Facade for DB operations, but this is not its main purpose. DAO可以被视为数据库操作的Facade,但这不是它的主要目的。 It mainly intends to hide the DB internals. 它主要是为了隐藏DB内部。 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. 在您的示例中,如果您将基础存储系统切换到XML文件或某些键值存储(如HBase),您仍然可以使用“Facade”中定义的方法,并且客户端代码中不需要进行任何更改。

A (traditional) Facade deals with complex designs that need to be hidden from the clients. (传统)Facade处理需要隐藏在客户端之外的复杂设计。 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. 而不是暴露复杂的API和复杂的流(从这个服务获取,将其传递给此转换器,获取结果并使用它验证它,然后将其发送到此其他服务),您只需将所有内容封装在Facade中向客户端公开一个简单的方法。 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. 这样,除了您的API更易于使用之外,您还可以自由地更改底层(复杂)实现,而不会破坏客户端代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM