英文:
When should I make objects package-private or public?
问题
以下是翻译好的部分:
我有两个类:Foo.class、FooContainer.class 和 FooContainerUser.class
Foo.class
class Foo {
private Map<String, String> fooData;
public Foo () {
fooData = new HashMap<>();
}
public Map<String, String> getFooData() {
return new HashMap<>(fooData);
}
}
FooContainer.class
public class FooContainer {
private Map<String, Foo> fooMap;
public FooContainer () {
fooMap = new HashMap<>();
}
// 第一个获取器实现
public Map<String, Foo> getFooMap() {
return new HashMap<>(fooMap);
}
// 第二个获取器实现
public Map<String, String> getFooData(String fooName) {
Foo selectedFoo = fooMap.get(fooName);
if (selectedFoo != null) {
return selectedFoo.getFooData();
} else return null;
}
}
假设Foo和FooContainer类位于同一个包中,而一个名为FooContainerUser的类位于包含前述类所在包的父包中。
FooContainerUser类被提供了一个FooContainer实例。出于某种原因,它需要获取FooContainer包含的Foo实例的数据,例如用于显示数据。
这带我来到我的问题:
- 我应该将Foo类设为public,并使FooContainerUser使用FooContainer类的第一个获取器实现,还是应该将Foo类设为包私有,并使FooContainerUser使用FooContainer类的第二个获取器实现?
- 假设第二个获取器实现更受欢迎。如果我创建更多扩展Foo类的类,每个子类都有不同的字段,而且需要将不同的子类存储在FooContainer中,我应该为FooContainer实现更多针对Foo的每个子类特定的获取器方法吗?
如果我在解释问题时出现了错误,对此我深感抱歉。我对编程相对较新,我想学习基本的设计模式。
英文:
I have two classes: Foo.class, FooContainer.class, and FooContainerUser.class
Foo.class
class Foo {
private Map<String, String> fooData;
public Foo () {
fooData = new HashMap<>();
}
public Map<String, String> getFooData() {
return new HashMap<>(fooData);
}
FooContainer.class
public class FooContainer {
private Map<String, Foo> fooMap;
public FooContainer () {
fooMap = new HashMap<>()
}
// First getter implementation
public Map<String, Foo> getFooMap() {
return new HashMap<>(fooMap);
}
// Second getter implementation
public Map<String, String> getFooData(String fooName) {
Foo selectedFoo = fooMap.get(fooName);
if (selectedFoo != null) {
return selectedFoo.getFooData();
} else return null;
}
Suppose the classes Foo and FooContainer is in the same package, and a class called FooContainerUser is in a package parenting the package where the previous classes reside.
The FooContainerUser class is supplied a FooContainer instance. It needs to get the data from the Foo instances the FooContainer contains for some reason e.g. to display the data.
Which leads me to my question:
- Should I make the Foo class public and make FooContainerUser use the first getter implementation of the FooContainer class, or should I instead make the Foo class package-private and make FooContainerUser use the second getter implementation of the FooContainer class?
- Suppose the second getter implementation is preferred. What if I make more class that extends the Foo class with more but different fields for each subclass, and different subclasses needs to be stored in the FooContainer? Should I implement more getter methods in the FooContainer specific for each subclasses of Foo?
Sorry if I made mistakes on how I explained my problem. I am relatively new to programming and I want to learn the basic design patterns.
专注分享java语言的经验与见解,让所有开发者获益!
评论