实体类的正确设计。需要建议 [英] Correct design for entity classes. Need recommendations
问题描述
例如,我有实体类用户
:
public用户
{
private long id;
private String name;
// setters和getters
}
接下来,我添加新的实体类:注释
public class Comment
{
private long id;
private String comment;
// setters和getters
}
接下来,我可以添加越来越多的实体类。
此时,我想:我可以/必须绑定/连接逻辑结构我的实体类或不? >
我的意思是什么?我尝试解释:
点1:所有这些类:用户
,评论
等等 -
POJO
。
想法1:需要通过界面为这个类进行逻辑绑定或抽象类。
点2:我看到,所有实体类都有相同的方法: getId
和 setId()
。
想法2:需要避免在所有类中声明此方法。
我的解决方案:
添加界面 BaseEntity
:
public interface BaseEntity
{
public long getId();
public void setId(long id);
}
添加所有实体类必须实现此界面。
结果我们逻辑连接所有实体类。我们保证每个实体类实现 getId()
和 setId()
方法。
但是,该解决方案无法解决多个声明 getId
和 setId
的问题。 / p>
解决方案是创建一般的BaseEntity类:
public class BaseEntity
{
private long id;
public long getId(){return this.id};
public void setId(long id){this.id = id;};
}
所有实体类必须扩展BaseEntity类。
mmmm,声音很好:)
但是,使用当前实现 - 用户可以创建instense BaseEntityClass。这个有道理吗?我可以提供创建一个类BaseEntity的可能性?
或者,很好的解决方案将这个类标记为 abstract
?
你觉得如何?
如果你同意我以前的所有步骤:
我有最后一个问题:
通信beetween类必须基于接口。但是我没有实体接口。未来可能会为我造成问题?
谢谢。
是的,使您的基础实体成为一个抽象类,并让其他扩展。
public abstract class BaseEntity
{
private long id;
public long getId(){return this.id};
public void setId(long id){this.id = id;};
}
作为一般规则,应该始终对界面进行编程,而不是实现。
For example, I have entity class User
:
public class User
{
private long id;
private String name;
// setters and getters
}
Next, I add new entity class: Comment
public class Comment
{
private long id;
private String comment;
// setters and getters
}
Next, I can add more and more entity classes.
And, at this moment I think: I can/must bind/connect in logical structure my entity classes or no?
What I mean? I try explain:
Point 1: All this classes: User
, Comment
and more other - POJO
.
Idea 1: Need logical binding for this classes via interface or abstract class.
Point 2: I see, that All entity classes has same methods: getId
and setId()
.
Idea 2: Need to avoid declaration this methods in all classes.
My Solution:
Add interface BaseEntity
:
public interface BaseEntity
{
public long getId();
public void setId(long id);
}
Add all entity classes must implement this interface.
In result we logical connect all entity classes. And we guarante that each entity class implement getId()
and setId()
methods.
But this solution doesn't resolve problem with multiple declaration getId
and setId
.
A solution is to create general BaseEntity class:
public class BaseEntity
{
private long id;
public long getId() {return this.id};
public void setId(long id) {this.id = id;};
}
And all entity class must extends BaseEntity class.
mmmm, sound nice :)
But, with current implementation - user can create instanse BaseEntityClass. This make sense? I can give possibility to create a class BaseEntity?
Or maybe, good solution mark this class as abstract
?
What do you think?
And if you agree with all my previous steps:
I have last question:
Communication beetween classes must based on Interfaces. But I dont have interface for entities. It is can create problems for me in future?
Thank you.
Yes, make your base entity an abstract class and let other extend it.
public abstract class BaseEntity
{
private long id;
public long getId() {return this.id};
public void setId(long id) {this.id = id;};
}
As a general rule, one should always program to an interface and not to an implementation.
这篇关于实体类的正确设计。需要建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!