views:

55

answers:

3

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

A: 

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.

Vinay Pandey
+1  A: 

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.

JRL
A: 

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

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.

Dave Sims