解耦——OOP
我有一个简单的问题(使用Java)。我有两个类,一个代表文档,第二个代表单词。
Document 类需要了解有关 Word 中保存的单词的一些信息。我的问题是,解耦这两个类的最佳方法是什么?我有两个选择:
类之间没有联系,每次我调用 Document 中的方法时,我都会向它传递一个 Word 对象(因此我有第三个类,其中的 main 方法启动 Document 和 Word)。
在Document中声明一个Word的私有对象。
需要注意的一件事是,我只有一个用于 Word 的对象和一个用于 Document 的对象。我不会为每个新文档或单词创建一个新对象。我将整个文档的列表存储在 Document 中,并将整个单词的列表存储在 Word 中。
谢谢!
I have a simple question (working with Java). I have two classes, one represents a Document, a second represents a Word.
The Document class needs to know some info about the words that is kept in Word. My question is, what's the best way to decouple the two classes? I have 2 options in mind:
Have no connection between the classes, and each time I call a method in Document, I pass it an object of Word (so I have a third class with a main method that initiates
both Document and Word).Declare a private object of Word inside Document.
One thing to note, I only have one object for Word and one for Document. I don't create a new object for every new document or word. I store a list of the entire documents in Document, and a list pf the entire words in Word.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不同意你对解耦的理解。解耦不仅仅涉及哪些对象创建其他对象,还涉及哪些对象了解其他对象的行为,以及(最重要的是)如果 Word 发生更改,则需要更改(您的示例)文档中的哪些内容。
不过,我真的不明白你这两个短语的意思:
从文档开始。这个类的对象可以做什么?您似乎在说,
如果类 Document 包含一个列表,那么它的列表是什么?我认为你需要:
现在一个架子可以提供诸如 getRecentDocumets() findDocumentsContaining(String text) 之类的方法
Document 可以包含 getWordCount() 和 insertParagraph(List, START);等等。
为了更好地进行讨论,我们需要更多地了解您的想法,更多地了解您的行为。
我确实同意您的总体想法,即除了文档和 Word 之外还有其他东西。可以合理调用 createDocument() 和 insertParagraph() 等方法的东西
I don't agree with your understanding of Decoupling. Decoupling is not just about which objects create other objects, it's also about which objects know about the behaviour of other objects and (crucially) what needs to change in (your example) Document if Word changes.
However, also I really don't understand what your mean by these two phrases:
Start from Document. What can objects of this class do? You seem to be saying that
If class Document contains a List, what's it a List of? I think you need:
Now a Shelf could offer methods such as getRecentDocumets() findDocumentsContaining(String text)
and Document could contain getWordCount() and insertParagraph(List, START); and so on.
To have a better discussion we need to see a bit more of what you had in mind, a bit more about behaviour.
I do agree with your general thought that there is Something other than Document and Word out there. Something that can reasonably invoke methods such as createDocument() and insertParagraph()
从我的角度来看......
这是一个合理的方法。在此示例中,您可以创建一个不包含任何
Word
的Document
,这会形成一个有效的空文档。您仍然可以按照您的建议创建第三个类,但是,我没有看到它的好处。
From my point of view...
It's a reasonable approach. In this example you can create a
Document
without anyWord's
, which makes for an empty Document, which is valid.You could still create a third class as you suggest, however, I don't see the benefit with it.
你的问题应该通过组合来解决。因此,拥有一个
Word
列表似乎是一种有效的方法。通过分离文档和单词,您已经实现了所需的解耦。我不明白您将 Document 和 Word 对象解耦的确切观点。Your problem should be solved by composition. Thus having a List of
Word
seems to be a valid approach. By separating out Documents and Words, you have already achieved the required de-coupling. I do not get your exact point of de-coupling Document and Word objects.你的问题是
两个选项都不能满足您的要求。如果他们要一起工作,那么他们就会结合在一起。唯一的问题是耦合有多紧或多松。
您的两个选项听起来都像是紧密耦合。松散耦合的一种形式是存储接口引用并将其接收到构造函数或 setter 方法中。
Your question is
Neither option satisfies your request. If they are going to work together, then they are going to be coupled. The only thing is how tight or loose the coupling is.
Both of your options sound like tight coupling. A form of looser coupling would be to store an
interface
reference and take it in on a constructor or setter method.如果要解耦类,一种标准方法是使用接口:
这样 Class 和 Word 都依赖于 IWord,但类和 Word 都不依赖于对方。这称为依赖倒置。
If you want to decouple the classes, one standard way is to use an interface:
This way both Class and Word depend on IWord, but neither class nor Word depends on the other. This is known as Dependency Inversion.
好吧,首先我认为你的类命名不正确。
从你在这里所说的来看,你有这样的事情:
并且你想在文档中使用 Words 类。如果你想这样做,那就意味着你无法将其解耦(因为这违背了“解耦”一词的定义)。我再次猜测,但我认为您想要对其进行编码,以便可以更改 Documents 类的实现,以便将来它可以使用另一个类,例如 BetterWords。
为了做到这一点,我将创建一个抽象类或一个接口(取决于架构的其余部分),然后使 Words 类扩展它或实现它。然后你可以做这样的事情:
或者你可以把它放在Document类(单词)中,不知道你想要它们在哪里。
Well first of all I think you're naming your classes incorrectly.
Judging by what you said here you have something like this:
And you want to use the Words class in the Documents. If you want to do that, that means you can't decouple it (as it's against the very definition of the word "decouple"). I'm guessing here again, but I think you want to code it so you can change the implementation of the Documents class so in the future it could use another class like for example BetterWords.
In order to do that I would create either an abstract class or an interface (depending on the rest of your architecture) and then make the Words class either extend it or implement. Then you can do something like this:
Or you can put it in the Document class (the words), don't really know where you want them.