Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

'Store' class problems, recursion?

Options
  • 22-03-2007 2:19pm
    #1
    Registered Users Posts: 1,885 ✭✭✭


    I am writing a class 'Store' which represents an store object. This Store can hold Objects of a given Type, and can be added to one object at a time. If however you add an Object of a new Type, a new Store is created.

    my Store is something like this in rough pseudocode

    Class Store
    type objectType
    int storeNumber

    constructor Store(Object o)
    type = o.getType()
    increment number of stores in existance

    method addObject(Object o)
    if (o.getType == type)
    then add an object to the store
    else create new store

    My problem lies with this 'else create new store' bit. How will this store be accessible to me? I will need, at the end of the day, to iterate over the stores printing the storenumber and the number of objects and their type.

    I know there's an easy solution, but whenever i try to see it i get the black brain-fuzz. Help most appreciated


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    It all depends on how you plan to use the information later on, but the most logical course of action would seem to me to return an object from the addObject method. This way you can pass the new store back to the calling class.

    As said, I've no idea exactly what you're trying to do here, but it may make more sense to create a higher-level class, StoreCollection or something.

    This maintains an array of Store Objects, and adds a new Store if adding a new Object Type. In fact, you could extend the Store class to create the StoreCollection class (a Store of Stores, if you will).


  • Registered Users Posts: 1,885 ✭✭✭beans


    That StoreCollection idea is exactly what I need to do. This is just an excercise to flex my long-dormant programming muscles and I just couldn't see where I was running around in circles.

    I thought there would be an array of Stores involved somewhere, just couldn't tell if there were a more elegant solution somewhere. Thank you for the pointer :cool:


Advertisement