Java EE中的Facade要点是什么? [英] What is the point of a Facade in Java EE?

查看:214
本文介绍了Java EE中的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));
查询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();
根< T> rt = cq.from(entityClass);
cq.select(getEntityManager()。getCriteriaBuilder()。count(rt));
查询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);
}

}

这是什么意思?为什么叫这个门面?对我来说,这只是一个类似功能的抽象类。谢谢。

解决方案

Facade是一种设计模式。一种模式,一种软件模式,是一组规则,用于组织代码并提供一定的结构。通过使用模式可以达到一些目标。
设计应用程序时使用设计模式。



Facade模式允许程序员为对象创建一个简单的界面来使用其他对象。
考虑使用一组非常复杂的类,所有类都实现自己的接口。
嗯,你想提供一个接口,只暴露出你所拥有的许多功能。
通过这样做,您可以实现代码简单性,灵活性,集成和松散耦合。



在您的示例中,使用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 {
私人MyClass1 a;
私人MyClass2 b;
//调用方法call1 call2 call3和call4在这个类的其他方法
...
...
}
/ pre>

如果您不必更改位于call1或call2 ...使用的类中的业务逻辑,方法是不更改接口,则不需要更改所有这些类,但只是前两类接口方法之一使用的类。



Facade可以改进这种机制。



我很抱歉,但我意识到这看起来并不美好。设计模式在软件行业大量使用,在大型项目上工作时,它们非常有用。
您可能会指出,您的项目不是那么大,可能是真的,但Java EE旨在帮助企业和企业级应用程序编程。这就是为什么有时候,默认情况下会使用外观图案(一些IDE也使用它)。


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.

解决方案

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).

这篇关于Java EE中的Facade要点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆