需要帮助定义类职责
我目前正在通过书籍和网站自学 OOP。我现在正处于需要练习的阶段。我开始了一个小项目,我知道如何以程序方式完成。但当我尝试以面向对象的方式进行操作时,它给我留下了疑问。
我的项目理念是这样的。我想在工作中组织/存档/管理我们系统上的互连。我用了2个类。设备和接口。
设备有一些接口。
设备类有以下方法:
- void addInterface( String name)
- void removeInterface( Interface i)
- Interface getInterface( Interface i) # 我应该写: Interface getInterface( String interfaceName )
- void printAllInterface()
接口类有以下方法:
- void connectInterface ( Interface interfaceToConnectTo )
- void disconnectInterface( Interface interfaceToDisconnectFrom )
- void printAllConnection()
基本上,我创建两个设备,向每个设备添加一些接口,最后在接口之间建立一些连接。
设备知道它的所有接口。接口知道它所连接的所有其他接口。
但是给定一个接口,我如何知道它属于哪个设备?
如果接口知道设备,它们就会紧密耦合。就我到目前为止所学到的来说,它并不是真正好的面向对象。另一种方法是浏览所有设备以了解它们是否具有我正在寻找的接口。看来效率确实很低。我确信我错过了一些明显的事情。有人能看到吗?
感谢
更新: 这可以看作 MS Visio 文件中的形状和连接。接口实际上只是形状上的连接器。器即形。
I am currently learning OOP on my own with books and websites. I am at the point I need to practice. I started a small project that I know how to do in a procedural way. But it leaves me with questions when I try to do it in a OOP manner.
My project concept is like this. I want to organize/archive/manage interconnection on our system at work. I use 2 classes. Device and Interface.
A Device have some Interfaces.
Device class have the following methodes:
- void addInterface( String name)
- void removeInterface( Interface i)
- Interface getInterface( Interface i) # I should have wrote : Interface getInterface( String interfaceName )
- void printAllInterface()
Interface class have the following methodes :
- void connectInterface( Interface interfaceToConnectTo )
- void disconnectInterface( Interface interfaceToDisconnectFrom )
- void printAllConnection()
Basically, I create two device, add some interfaces to each and finally make some connection between interfaces.
A device know all its interfaces. An interface know all other interface it is connected to.
But given an interface how do I know to what device it belong?
If Interface is aware of Device, they become tightly coupled. For what I learn so far it is not really good OO. An other way would be to browse all device to know if they have the Interface I am looking for. It seems really inefficient. I am sure I missed something obvious. Can anybody sees it?
Thanks
update :
This can be seen as shapes and connections in a MS Visio file. the interface is really just a connector on a shape. The device is the shape.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要求必须能够确定给定
接口
的设备
,则意味着设备
和接口<即使您还没有编码, /code> 也已经紧密耦合。如果您将这两个类解耦,那么您就不能再假设所有
Interfaces
都属于Device
。如果所有
Interface
对象必须属于Device
,那么我认为setDevice< 没有任何问题
Interface
上的 /code>/getDevice
方法。是的,它创建了循环依赖关系,但看起来这是对特定域进行建模的最佳方式。在我看来,搜索每个Device
只是为了看看它是否包含特定的Interface
是一个更糟糕的设计决策。但是,如果希望
Interface
存在而不属于Device
,或者可能属于完全不同的类,那么您需要重新考虑架构更高的水平。大致如下:如何重组这些类,这样我就不需要从接口
获取设备
?答案确实如此取决于您的具体领域,仅从您问题中的信息我们对此不太了解。If you have the requirement that you must be able to determine the
Device
for a givenInterface
, that means thatDevice
andInterface
are already tightly coupled even if you haven't coded it. If you decouple the two classes then you can no longer assume that allInterfaces
belong to aDevice
.If it is the case that all
Interface
objects must belong to aDevice
, then I don't see any problem havingsetDevice
/getDevice
methods onInterface
. Yes it creates a circular dependancy, but it looks like that is the best way to model your specific domain. Searching through everyDevice
just to see if it contains a specificInterface
is a much worse design decision in my opinion.However, if it is desirable for an
Interface
to exist without belonging to aDevice
, or maybe belonging to a totally different class, then you'll need to rethink the architecture at a higher level. Something along the lines of: How can I restructure these classes so I don't need to get aDevice
from anInterface
? The answer really depends on your specific domain, which we don't know very much about just from the information in your question.对现有界面的一个小评论。恕我直言,接口上的打印方法应该移动到不同的接口,以便设备接口只负责维护它控制的接口。另外,方法“Interface getInterface(Interface pInterface)”接收什么作为输入参数并返回什么?
至于答案,上面的答案提出了几个问题。如果我理解正确的话,设备类会在调用 addInterface() 时创建一个接口;如果用例是给定一个接口,要返回一个设备,则接口上的 getDevice() 是可以的(我不会添加 setDevice() 方法;设备可以传递到接口的构造函数中)如果接口总是属于一个设备。
编辑:但是,如果您想给 api 用户留下这样的印象:您的用例要求与接口关联的设备可以在运行时更改,则最好使用 setDevice() 方法。
A small comment on your existing interfaces. IMHO the print method on the interfaces should be moved to a different interface so that the Device interface has a single responsibility of just maintaining the Interfaces it controls. Also, what does the method "Interface getInterface(Interface pInterface)" receive as Input argument and return?
As for the answer, there are a few questions that the answer above raises. If I understand correctly, the device class creates an interface when addInterface() is invoked; If the use case is such that given an interface a device is to be returned, a getDevice() on Interface is okay (I wouldn't add a setDevice() method; Device can be passed into the Interface's constructor) if an Interface always belongs to a device.
Edit : The setDevice() method would however be preferrable if you want to give the user of your api an impression that your use case so demands that the device associated with an interface can change at runtime.