|
-
Dec 18th, 2003, 08:32 AM
#1
Thread Starter
Frenzied Member
How to implement collection the OO way
OK, I have an UML class dragram where I have CCustomer, CItem, CBasket
This is for a true UML-OO webshop in ASP.NET
Anyway the relationship is 1->n for the Basket and the Item, meaning one basket can contain multiple items
BUT
how do I implement this code wise? I need only one basket object, but how and where do I create the items (I know about factory/abstract factory patterns) and how do I store them in the basket object?
Should the basket inherit som arraylist or collection class?
I want to perform some basic MyBasket.Add(Item as CItem)
There are no child classes for the item object, since all items in the shop share the same functionality.
My two questions are:
1) What pattern should I use when I create the items??
2)How should I add them to the basket???
Some code or UML would be nice. Im currently in the Class diagram phase of the project
thanks
Henrik
ps I prefer a GOF pattern solution to the problem ds
-
Dec 18th, 2003, 08:39 AM
#2
Fanatic Member
the basket would just need a system.collections.arraylist private variable. You can then have public methods that add item objects to the arraylist, return individual items, or a full collection(ie a basket), and any thing else you would need to do. When adding an item to the collection you could either pass inividual parameters or an item object.
I'm not sure if this is a good way, or if it is a design patterns. I've read a book on design patterns but struggle to match them up to work i have aready done. In some ways i feel design patterns over-complicate simple effective ideas.
Anyway not sure if that helps at all but it's my 2 cents/pence!!!
Cheers
Nick
-
Dec 18th, 2003, 08:44 AM
#3
Thread Starter
Frenzied Member
Thanks for the reply... I think it is very interesting to discuss design issues like this.
Now I have some new questions I came up with when I started to think about it...
1) Should I implement the IEnumerable interface in the Basket class, or inherit a class that give me similar functionality, so I can use the "for each item as CItem in myBasket"
2)So all I have to do is inherit the arraylist so I can perform the "add" but with CItem objects...?
thanks!!!
Henrik
-
Dec 18th, 2003, 08:47 AM
#4
Fanatic Member
1, i suppose it depends whether you are having an item class and that's it. If you are having an interface and then different implementations of the class then you should go the way you suggested
2, I meant create a class called basket as well items. basket will have the private collection declared in it that will be filled up with items.
-
Dec 18th, 2003, 08:51 AM
#5
Thread Starter
Frenzied Member
VB Code:
Class CBasket : Inherits ArrayList
End Class
Class CItem
Public name As String
Public Sub New(ByVal mname As String)
name = mname
End Sub
End Class
Sub Main()
Dim basket As New CBasket
Dim item As New CItem("Hammer")
Dim item2 As New CItem("Apple")
basket.Add(item)
basket.Add(item2)
For Each testitem As CItem In basket
Console.WriteLine(testitem.name)
Next
Console.ReadLine()
End Sub
Somthing like this???... forget that I have all variables public, wrote this in like 1 minute. I will use properties and an itemfactory as shared function... unless anyone has a better idea???????
With this application I will show my company the benefits of working with OO, patterns and design 
kind regards
Henrik
-
Dec 18th, 2003, 08:58 AM
#6
Fanatic Member
yeah i didn't know about the Inherits ArrayList bit, so that is cool. I would put the Citem class in a class of it's own right (i.e. a different .vb file), that way it would be easier to may be seperate them into seperate dll's if you needed to.
Like i said i'm a beginner at this too so don't trust just what I say!
-
Dec 18th, 2003, 05:32 PM
#7
Thread Starter
Frenzied Member
Okay, yeah like I wrote it was written completely by heart in about 2 minutes, with a little help from intellisense 
Perhaps someone else with experience in OO design pattern concept design would care to comment on this rather trivial example?
I know there is an arhitecture&design area in this forum, but from teh looks of it, that area felt pretty cold... not much activity.. too bad since A&D is the real benefit with .NET, the ability to really implement all patterns, OO with ease.. then it only takes lot of practice to be good at it 
kind regards and merry christmas!
Henrik
-
Dec 18th, 2003, 07:42 PM
#8
yay gay
Originally posted by MrNorth
Thanks for the reply... I think it is very interesting to discuss design issues like this.
Now I have some new questions I came up with when I started to think about it...
1) Should I implement the IEnumerable interface in the Basket class, or inherit a class that give me similar functionality, so I can use the "for each item as CItem in myBasket"
2)So all I have to do is inherit the arraylist so I can perform the "add" but with CItem objects...?
thanks!!!
Henrik
http://www.vbforums.com/forumdisplay.php?s=&forumid=40
\m/  \m/
-
Dec 19th, 2003, 03:18 AM
#9
Thread Starter
Frenzied Member
Hmm, yes I know about that part of the forum, but I take my chances and post in a forum visited by more people...
Anyone care to comment on the design?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|