Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这是怎样的设计模式?_Java_Oop_Design Patterns - Fatal编程技术网

Java 这是怎样的设计模式?

Java 这是怎样的设计模式?,java,oop,design-patterns,Java,Oop,Design Patterns,我需要帮助。我正在互联网上搜索如何为我的代码调用设计模式,但我没有找到它。 我有一个包含getter、setter和manager的类(f.e.exercise),它们都有练习(在DB或数组中,没关系)。在我的应用程序中,我有一个练习管理器实例,我通过它处理练习 练习: public class Exercise { private Long id; private String name; public String getName() { retur

我需要帮助。我正在互联网上搜索如何为我的代码调用设计模式,但我没有找到它。 我有一个包含getter、setter和manager的类(f.e.exercise),它们都有练习(在DB或数组中,没关系)。在我的应用程序中,我有一个练习管理器实例,我通过它处理练习

练习:

public class Exercise {
    private Long id;
    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}
运动经理:

public interface ExerciseManager{
    void createExercise(Exercise exercise);

    Exercise getExerciseById(Long id);

    Collection<Exercise> getAllExercises();

    Exercise getExerciseByName(String name);

    void updateExercise(Exercise exercise);

    void deleteExercise(Long id);

    Boolean isIdInDB(Long id);
}

public class ExerciseManagerImpl implements ExerciseManager{
    ...
}
公共接口管理器{
无效行使(行使行使);
练习getExerciseById(长id);
集合getAllExercises();
练习getExerciseByName(字符串名称);
作废更新演习(演习演习);
无效操作(长id);
布尔isIdInDB(长id);
}
公共类ExerciseManagerImpl实现ExerciseManager{
...
}
主要(f.e.):

publicstaticvoidmain(字符串[]args){
数据源数据源=;
ExerciseManager ExerciseManager=新的ExerciseManagerImpl(数据源);
练习newExercise=新练习();
newExercise.setName(“Name1”);
exerciseManager.createExercise(newExercise);
exercisexercise=exerciseManager.getExerciseByName(“Name1”);
exerciseManager.deleteXercise(existExercise.getId());
}

涉及到以下几个方面:

  • 工厂:只允许一个类创建特定类型的对象
  • Singleton:一个类型只存在一个实例
  • 调解员:专门操作和管理某一类型
但我想,你要找的是ORM,让我们看看维基百科是怎么说的:

中的对象关系映射(ORM、O/RM和O/R映射工具) 计算机科学是一种用于转换数据的编程技术 面向对象编程中不兼容类型系统之间的关系 语言。这实际上创建了一个“虚拟对象数据库” 可以从编程语言内部使用


这看起来像一个,它将是持久层的一部分;它充当应用程序代码(使用
Exercise
实例)和持久性机制(例如
EXERCISES
数据库表)之间的接口,将前者与后者分离。

首先是什么让你认为这是一个命名的设计模式?还有另一个不相关的问题,为什么
getExerciseById
返回的是
Dragon
而不是
Exercise
?我不知道,我在更多的代码中看到了类似的结构,所以我认为存在任何描述此结构的设计模式。对不起,我忘了更改一个类的名称,当然,练习是正确的。(我已经编辑了我的帖子。)在我看来,这只是一个合理的界面设计。看起来像是使用JPA的典型实现-只是没有@。它是表示模型的代码,以及与模型交互的一些方法。仅仅因为你有一个叫做“经理”的东西并不意味着你突然有了一个设计模式。我不明白这个问题的重点。
public static void main(String[] args) {
    DataSource dataSource = <some datasource>;
    ExerciseManager exerciseManager = new ExerciseManagerImpl(dataSource);
    Exercise newExercise = new Exercise();
    newExercise.setName("Name1");
    exerciseManager.createExercise(newExercise);
    Exercise existExercise = exerciseManager.getExerciseByName("Name1");
    exerciseManager.deleteExercise(existExercise.getId());
}