容器类是什么?

huangapple go评论73阅读模式
英文:

What are container classes?

问题

所以我有一个名为ShipStorage的容器类,它在一个数组中存储对象。我还有另一个名为FileManager的类,它使用ShipStorage的对象。我的教授说容器类不应该进行输入或输出(我认为那意味着不能使用System.out.println)。当FileManager仅在使用ShipStorage对象时,它是否也会被视为容器类?

英文:

So I have this container class called ShipStorage which store objects in an array. I have another class called FileManager which uses the object of ShipStorage. My professor said container classes should not be inputting or outputting(i think that means cannot use System.out.println). Will FileManager be considered a container class as well when it only uses the ShipStorage obj?

答案1

得分: 2

> 我的教授说容器类不应该进行输入或输出。

面向对象设计中有一个通用原则叫做关注点分离。一个类应该处理属于它的"关注点",而不应该处理与它无关的事情。

(比如,洗碗机洗碗是它的关注点,加热微波炉食品不是它的关注点。)

在你正在实现的设计中,容器类的目的是包含对象。输入或输出这些对象不是它的关注点。那(大概)是FileManager类或其他类的关注点。

这是你的老师一个合理的设计决策,也是我会采用的设计方式。无论哪种方式,这就是你的老师要求你采用的方式,也就是"需求"。因此,你的代码应该按照这个方式工作。

> (我认为这意味着不能使用System.out.println)

这是你的容器类在这个项目中不应该做的事情的一个示例。

Java编程语言并不要求应用程序被正确设计,但随着你获得更多经验,你会发现良好的设计有许多好处,特别是当你处理复杂的应用程序时。

英文:

> My professor said container classes should not be inputting or outputting.

There is a general principle in OO design called Separation of Concerns. A class should do things that are its "concern" and not do things that are not its "concern".

(A dishwasher washes dishes. Heating microwave dinners is not its concern.)

In the design that you are implementing, the purpose of a container class is to contain objects. Inputting or outputting the objects is not its concern. That is (presumably) the concern of the FileManager class, or some other class.

This is a reasonable design decision for your teacher to make, and probably how I would design it too. Either way, this is what your teacher has said to do; i.e. the "requirements". Therefore, it is how your code should work.

> (i think that means cannot use System.out.println)

That is an example of the things that your container should not do ... in this project.

The Java programming language doesn't insist that applications are designed properly, but as you get more experience you will find that good design has many benefits, especially when you are dealing with complicated applications.

huangapple
  • 本文由 发表于 2020年4月4日 21:43:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61029015.html
匿名

发表评论

匿名网友

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

确定