何时应将对象设置为包私有或公共?

huangapple 未分类评论47阅读模式
英文:

When should I make objects package-private or public?

问题

以下是翻译好的部分:

我有两个类:Foo.classFooContainer.classFooContainerUser.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实例的数据,例如用于显示数据。

这带我来到我的问题:

  1. 我应该将Foo类设为public,并使FooContainerUser使用FooContainer类的第一个获取器实现,还是应该将Foo类设为包私有,并使FooContainerUser使用FooContainer类的第二个获取器实现?
  2. 假设第二个获取器实现更受欢迎。如果我创建更多扩展Foo类的类,每个子类都有不同的字段,而且需要将不同的子类存储在FooContainer中,我应该为FooContainer实现更多针对Foo的每个子类特定的获取器方法吗?

如果我在解释问题时出现了错误,对此我深感抱歉。我对编程相对较新,我想学习基本的设计模式。

英文:

I have two classes: Foo.class, FooContainer.class, and FooContainerUser.class

Foo.class

class Foo {
    private Map&lt;String, String&gt; fooData;

    public Foo () {
        fooData = new HashMap&lt;&gt;();
    }

    public Map&lt;String, String&gt; getFooData() {
        return new HashMap&lt;&gt;(fooData);
    }

FooContainer.class

public class FooContainer {
    private Map&lt;String, Foo&gt; fooMap;

    public FooContainer () {
        fooMap = new HashMap&lt;&gt;()
    }

    // First getter implementation
    public Map&lt;String, Foo&gt; getFooMap() {
        return new HashMap&lt;&gt;(fooMap);
    }

    // Second getter implementation
    public Map&lt;String, String&gt; 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:

  1. 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?
  2. 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.

huangapple
  • 本文由 发表于 2020年5月5日 17:57:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/61610420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定