需要类设计帮助
我需要开发一个特定的软件模块,它以以下格式输出数据主要对象和相关对象和数量
,即
Desktop Computer
---- CPU 1x
---- Mouse 1x
---- KB 1x
---- Monitor 1x
---- Speakers 2x
这意味着对于台式计算机对象,应有 1 个 CPU、1 个鼠标、1 个键盘、1 个显示器、2 个同样
Cubicles
---------- Desktop Comps 4x (shall mean each cubicle shall contain 4 pc's)
---------- Power Supply 1x (shall mean each cubicle shall have a main pow.
supply)
,对于每个隔间对象,应有 4 个桌面和 1 个电源对象
规则:每 4 个隔间应有一个 HUB 对象
The sample Output for 8 cubicles shall be
Total CPU's - 32
HUB - 2
Mouse - 32
KB - 32
Monitor - 32
Speakers - 64
Cubicles - 8
Desktop PC's - 32
Pow. Supply - 8
任何人都可以帮助我在 OOP 类方面实现相同的目标/接口?或者指向在这种情况下使用的设计模式的指针。 非常感谢您花费同样的时间
I need to develop a certain sw module which outputs the data in the following format Main object and related object and Quantity
i.e
Desktop Computer
---- CPU 1x
---- Mouse 1x
---- KB 1x
---- Monitor 1x
---- Speakers 2x
This shall mean that for a Desktop Computer object, there shall be 1 CPU,1 Mouse,1 Keyboard,1 Monitor,2 speakers
Cubicles
---------- Desktop Comps 4x (shall mean each cubicle shall contain 4 pc's)
---------- Power Supply 1x (shall mean each cubicle shall have a main pow.
supply)
Similarly for each Cubicle object, there shall be 4 Desktops and 1 Power supply object
Rule : For every 4 cubicles there shall be a one HUB object
The sample Output for 8 cubicles shall be
Total CPU's - 32
HUB - 2
Mouse - 32
KB - 32
Monitor - 32
Speakers - 64
Cubicles - 8
Desktop PC's - 32
Pow. Supply - 8
Can anyone help me in realizing the same in terms of OOP classes/interfaces ? Or a pointer to a design pattern to use in such situations.
Deeply appreciate your time for the same
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然我不是专家,但您可以执行以下操作: -
您可以创建两类计算机和多维数据集,并且您的计算机类可以监视资源的数量,例如连接到 CPU 的两个监视器或连接的四个扬声器。
您可以通过添加或更新添加或更新计数来监视资源数量
.这将允许您监视计算机和隔间的各个资源。
你的计算机和cube类实现了一个接口IDisplayCount,它将列出它们拥有的所有资源,这就是策略模式。
您可以通过遵循装饰器和工厂模式的组合来监视隔间列表。装饰器用于跟踪,工厂用于确保每个实例(更新或添加)都添加到通过创建实例或从同一位置更新来跟踪资源的列表中。
集线器的数量可以从您的隔间数量中进行监控。
希望这有帮助。
Although I am not an expert but below is what you can do:-
you can create two class computer and cube and you computer class can monitor the number of resources say two monitors attached to a CPU or four speakers attached .
you can monitor the number of resources by adding or updating count on add or update
.this will allow you to monitor individual resources for computer and cubicle.
your computer and cube class implement an interface IDisplayCount which will list all the resources they have, this is strategy pattern.
you can monitor the list of cubicle by following mix of decorator and factory pattern. decorator to keep track and factory to make sure every instances(updated or added) is added to the list that keeps track of resources by creating instance or updating from same place.
Number of hubs can be monitored from your number of cubiles class.
Hope this helps.
为您拥有的每个名词创建一个类。每个类都可以有一个它包含的元素列表(object-x has-a object-y)。对于每个类,创建一个
toString()
方法,该方法以正确格式的方式输出它包含的元素。Create a class for each noun you have. Each class can have a list of elements it contains (object-x has-a object-y). For each class, create a
toString()
method which outputs the elements it contains in the properly formatted way.看来您已经完成了大部分设计工作。 UML/类设计与您已经编写的内容非常接近。起点可能如下所示: alt text http://img6.imageshack.us/img6 /9232/cubicle.png
从这里继续您已经开始的大纲,让类设计遵循。您可能会考虑进行一些进一步的抽象,例如 DesktopComputer 将实现的 IComputer,这样您就可以将 LaptopComputer 对象替换为桌面。无论您的域名建议您需要什么,请遵循它。
It seems you've already done most of the design work. The UML/Class design follows pretty closely from what you've already written. A starting point might look like: alt text http://img6.imageshack.us/img6/9232/cubicle.png
From here just continue with the outline you've begun and let the class design follow. You might consider making some further abstractions like IComputer that DesktopComputer would implement so you could swap out, say, a LaptopComputer object for a desktop. Whatever your domain suggests you need, follow that.