英文:
Data encapsulation for db access: Do I always have to write public + private methods?
问题
同事说这是数据封装,在使用数据库访问时必须这样做:
public String foo(final int x) {
return fooHidden(x);
}
private String fooHidden(final int x) {
return db.lookFor(x);
}
我简化了代码以使其更易读,但它仍然保持相同的结构。
所以在我看来,这不是数据封装,它没有隐藏任何内容,因为返回值和传输参数是相等的。所以对我来说,写成这样也没有任何区别:
public String fooHidden(final int x) {
return db.lookFor(x);
}
上面的解决方案对我来说只有在我们重叠方法或者私有方法有其他使用类内部属性的参数时才有意义,但这不是这种情况。
你能告诉我谁是对的吗?你如何为这种情况实现真正的数据封装?
英文:
A colleague of my says that this is data encapsulation and it has to be done when using database access:
public String foo(final int x) {
return fooHidden(x);
}
private String fooHidden(final int x) {
return db.lookFor(x);
}
I simplified the code to be more readable, but it's still the same structure.
So in my opinion this is not data encapsulation, it doesn't hide anything, because the return value and the transfer parameter are equal. So for me, it would make no difference to write:
public String fooHidden(final int x) {
return db.lookFor(x);
}
The upper solution would make sense for me if we overlay the method or have other parameters for the private method which use class internal attributes, but this is not the case.
Can you tell me who is right? How would you accomplish real data encapsulation for this case?
答案1
得分: 2
两种方法的实现基本相同,即在您的情况下没有区别,但从语义上讲,更合理的做法是:
public String foo(final int x) {
return db.lookFor(x);
}
注意名称是 foo
而不是 fooHidden
,以引用公开提供的方法(hidden 会暗示您正在公开一个本应为私有的方法)。
同时,这两种实现仍然隐藏了 db
字段,该字段应该对于类的调用者是隐藏的,对于封装类来说是私有的,从而实现了封装。
因此,封装关注的是字段状态而不是方法实现。
这意味着整体上类的结构应该如下所示:
public class MyClass {
private db; // 必须是私有的,否则您将违反封装规则
public String foo(final int x) {
return db.lookFor(x);
}
}
回到问题:
> 我是否总是需要编写公共方法 + 私有方法?
问题应该改为:我是否总是需要编写公共 + 私有字段?
在大多数情况下,是的,您应该这样做。同时,可能会有需要打破这一规则的设计要求。
在 Java 中,封装 关注的是访问修饰符;即哪些字段 / 方法 是公开的,从而可以被外部世界访问。
> 面向对象的编程语言将程序分为称为类的模块。每个类包含由数据(字段)和方法组成的特征。(并非所有语言都使用这些术语,但这些术语对于本文来说足够了。)语言对于其他类可以访问类的特征有各种规则,这些规则通常基于适用于类的访问修饰符。
英文:
The two methods implementations are quite the same, i.e. in your case it makes no difference, but semantically speaking, it would make more sense to have:
public String foo(final int x) {
return db.lookFor(x);
}
Note the name foo
and not fooHidden
to make reference to publicly provided method (hidden would otherwise imply you are exposing a supposed-to-be-private method).
Meanwhile, both implementation still hide the db
field, which should be hidden from class callers and private
to the wrapping class, hence encapsulated.
So encapsulation is about the field state and not the method implementations.
This implies that the class structure should be as follows in a whole:
<!-- language : lang-java -->
public class MyClass {
private db; // must be private, otherwise you'll be violating the encapsulation rule
public String foo(final int x) {
return db.lookFor(x);
}
}
Going back to the OP question:
> Do I always have to write public methods + private methods?
The question should instead be: Do I always have to write public + private fields?
And I would state that in major cases, you should do it yes.
Meanwhile, there may be design requirements that need to have the rule broken.
Encapsulation, in Java, is about Access Modifiers; meaning what fields / methods you make public thus accessible for the outside world.
Here is what what Martin Fowler states about Access Modifiers:
> Object-oriented languages divide a program into modules called classes. Each class contains features, which consist of data (fields) and methods. (Not all languages use these terms, but they'll do for this.) Languages have various rules about what other classes can access the features of a class, these are often based on access modifiers that apply to a class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论