|
From: William Allaire on 3 May 2007 20:54 I'm pretty new to OOP, so bear with me. I'd appreciate any guidance or suggestions of material to be read. I have a class called container. This class contains a collection of inventory item classes. In this case, the inventory item class only tracks a serial number of a box that contains widgets. The collection can contain from 1..n inventory item classes depending on the type of container. For a box, the collection would contain just one inventory item class. A skid could contain up to 36 inventory item classes. Etc. Now, I have a need to also keep track of the current quantity of widgets contained in a box identified by its serial number. I can think of a couple ways to do this and I'd appreciate some guidance by those who know much more about OOD than I do. Option 1: I can modify the inventory item class to contain an integer representing the quantity. When I initiate the inventory item class with a serial number, I can fetch and store the quantity. When I need to know the totaly quanity of widgets for the container, I can sum the quantities returned from each inventory item class in the collection. Option 2: Have a variable on the container level. As I learn each serial number, fetch the quanity and sum/store it in that variable. This is done on the container class level insted of the inventory item class level. The inventory item class would continue to know nothing about quantity. I can see quantity tied to both options and both having their pros and cons. The container has a quantity, a total one, and the boxes each have a quantity which are reflected by the inventory item class. This leads to my design delema. Depending on how the container is used, I may not need quantity information. Point of fact, until now, I haven't needed it. It seems a waste of cycles to gather quantity information when it's not needed. Option 1 I can inherit from the original inventory item class and add the quantity attribute along with the code to fetch the quanitity for this serial number. The problem I see with this is that my container object has to be modified in order to make use of the new attribute in the inventory item class, both in getting the information and in exposing the information. Now, because I don't need to gather quantity all of the time, I'd have need to create a second container class, to handle my new inventory item class that handles quantity as well. class Container { // array of inventory item classes } class ContainerWithQty extends Container { // array of inventory item with quantity classes } class InventoryItem {} class InventoryItemWithQty extends InventoryItem {} My two classes have become four. Option 2 Almost the same as option 1 but sans the class inventory item with quanitity. My two classes have become three. I'm not sure that inheritence is the way to go. What if I then needed to track widget color? And what if I don't always need to track the color? It would seem, at least with what I've offered, option two would start scaling better than option one. I certainly don't like the answers I've come up with, and hope there are better ways of handling this sort of situation. bill
From: H. S. Lahman on 4 May 2007 15:05 Responding to Allaire... > I'm pretty new to OOP, so bear with me. I'd appreciate any guidance or > suggestions of material to be read. > > I have a class called container. This class contains a collection of > inventory item classes. In this case, the inventory item class only > tracks a serial number of a box that contains widgets. The collection > can contain from 1..n inventory item classes depending on the type of > container. For a box, the collection would contain just one inventory > item class. A skid could contain up to 36 inventory item classes. Etc. I am a bit concerned about your terminology here. In OOA/D a class is just a set. It defines properties for objects that are members of that set. You seem to be suggesting that the kind of items is to be instantiated as as object. If so, I don't see a need for it. However, my guess is that what we have here is: * contains [Container] --------------+ A | | R1 | R2 +---------+---------+ | | | 1 | [Box] [Bulk] ------+ + serialNumber | 1 | | R3 | | contains | 1 [Item] + type Note that this is an application of the GoF Composite pattern. Q1: does a Box only contain one type of Item (i.e., it is equivalent to a conventional inventory bin)? If not, the multiplicity on the [Item] side of R3 is *. Q2: are there other flavors of bulk containers than Skid (e.g., boxcars)? If not, replace [Bulk] -> [Skid] and make R2 attach to [Box] rather than [Container]. Q3: is the "serial number" really unique to a Box or is it just a part number for what is in the Box? Q4: I assumed each Box on a Skid could have a different type of item. Is that correct? > > Now, I have a need to also keep track of the current quantity of widgets > contained in a box identified by its serial number. I can think of a > couple ways to do this and I'd appreciate some guidance by those who > know much more about OOD than I do. If the model above is correct, why wouldn't [Box] have a 'count' attribute? > Option 1: > I can modify the inventory item class to contain an integer representing > the quantity. When I initiate the inventory item class with a serial > number, I can fetch and store the quantity. When I need to know the > totaly quanity of widgets for the container, I can sum the quantities > returned from each inventory item class in the collection. So far I don't see any need for an object to capture the notion of a class of inventory. It seems to me that can be handled by a 'type' attribute on [Box] or [Item]. [If all you need to track is count, type of item, and Box serial number, then we may not need [Item] at all. If a Box can only have one type of item in it, then all that information could be associated with [Box].] > > Option 2: > Have a variable on the container level. As I learn each serial number, > fetch the quanity and sum/store it in that variable. This is done on > the container class level insted of the inventory item class level. The > inventory item class would continue to know nothing about quantity. Q5: do you need to track just the total quantity of items for Skid or the totals for each type of item on the Skid? In either case, I am not keen on this because it is indirectly dependent on the count one must necessarily have with each Box. So it will have to be synchronized with adding/removing Boxes to/from the Container. Whether one wants to do that depends upon how often one needs the total Container count vs. how often one adds/removes Boxes. ************* There is nothing wrong with me that could not be cured by a capful of Drano. H. S. Lahman hsl(a)pathfindermda.com Pathfinder Solutions http://www.pathfindermda.com blog: http://pathfinderpeople.blogs.com/hslahman "Model-Based Translation: The Next Step in Agile Development". Email info(a)pathfindermda.com for your copy. Pathfinder is hiring: http://www.pathfindermda.com/about_us/careers_pos3.php. (888)OOA-PATH
|
Pages: 1 Prev: polymorphism; dummy parameters Next: OO Books for newbies? |