Results 1 to 9 of 9

Thread: How to implement collection the OO way

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    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

  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    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
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  4. #4
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    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.
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    VB Code:
    1. Class CBasket : Inherits ArrayList
    2.  
    3.  
    4.  
    5.     End Class
    6.  
    7.     Class CItem
    8.         Public name As String
    9.         Public Sub New(ByVal mname As String)
    10.             name = mname
    11.         End Sub
    12.  
    13.     End Class
    14.  
    15.  
    16.     Sub Main()
    17.         Dim basket As New CBasket
    18.         Dim item As New CItem("Hammer")
    19.         Dim item2 As New CItem("Apple")
    20.         basket.Add(item)
    21.         basket.Add(item2)
    22.         For Each testitem As CItem In basket
    23.             Console.WriteLine(testitem.name)
    24.  
    25.         Next
    26.  
    27.         Console.ReadLine()
    28.     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

  6. #6
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    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!
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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
  •  



Click Here to Expand Forum to Full Width