Results 1 to 4 of 4
  1. #1
    pifrely is offline Member
    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0

    Default Classes with arbitrary data members

    Let's say I wanted to make a class of Items. "Items" represent a great many things, from potions to guns to books. However, some Items have fields which not all other Items have; a "revolver", for instance, could have some form of container that stores 6 ammo of type "bullet". But if I were to have all Items directly be of class Item, I could end up with a lot of data members that I just never end up using because they're irrelevant to the current Item (the container described above would be completely unused in a book, for example). But, if I were to make subclasses of Item like "UsableItem" or "UsableAndEquippableItem" or "ConsumableAndEquippableItem" or "UsableAndEquippableAndSellableItem" so that all the items fit neatly into their own niche, I'll end up with a great deal of classes on my hands, since there are definitely 5 or more properties I have in mind that don't necessarily fit into all Items; I'd likely have to make a new class to accommodate every permutation of Item's subclasses. That's a lot of classes!

    Then I thought, "What if I used a map to map data members to their values (assuming that all values are integers)"? ie. if an item is supposed to have a 'ammoContained' field representing the amount of ammo left in this item, I pass in something into the map (like an integer, maybe) whose value stands for this data member, so that I can fetch its value, if it exists as a "data member". And if it doesn't exist, well then I don't have to waste memory/time setting and storing it.

    What I'm wondering, though: Is this approach feasible? Am I breaking any rules or principles of class design or setting myself up for failure later on? Would the performance impact of doing class.getValue(dataMember) (using the map) be bad enough compared to class.getDataMember() (using a setter/getter) to cause issues in a large program where these hash tables could be accessed many times per second on a laptop or desktop? Or is there an easier alternative/I am overthinking and overestimating the problem?
    Last edited by pifrely; 01-26-2013 at 10:09 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Classes with arbitrary data members

    Sometimes 'Wrappers' or 'Decorators' can be of help here; I wrote a blog article about it for this forum once; although those Decorators are more about behaviour (i.e. modified functionality) maybe your problem can be modelled as such. Personally I'd be happy with your approach (it can even be found in the Swing components).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pifrely is offline Member
    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0

    Default Re: Classes with arbitrary data members

    Thanks! In the end, modified functionality at runtime is also something I want to go for. 'using' a potion should call a completely different function, with completely different behavior from 'using' a book. Initially, I was thinking of having it work somewhat like assembly instructions in that I could have a data member like 'useEffect' (of course, not all objects will have a useEffect, so I might end up using the map method of storing data members anyway), most likely an integer or long, whose first few bits tell which function (out of a predefined set of encoded instructions) to call, with the rest containing the arguments that go into the function to call. So I could have a potion call a function heal() upon calling potion.use() with arguments of this (so it can find who to heal) and int (so it can determine how much to heal). Decorators seem like a better and cleaner way to solve this problem, though (no having to use if statements to determine which function to call), although I'll still have to worry about the arguments that I pass into those functions.

  4. #4
    Nouish is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Classes with arbitrary data members

    When reading this I had Mojang's approach come to mind:

    Java Code:
    Item book = new ItemBook(0);
    Item coin = new Item(1);
    Item apple = new ItemConsumable(2);
    Check it out: it's alot like what you describe ;)

Similar Threads

  1. Updating data members in panels
    By Neurax in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2012, 05:21 AM
  2. Replies: 3
    Last Post: 02-11-2012, 03:32 AM
  3. Create Image from Arbitrary 2D Matrix of Data
    By dvreed77 in forum Java 2D
    Replies: 13
    Last Post: 06-12-2011, 10:42 PM
  4. Sharing Data Members between two JFrames
    By PrinceSendai in forum AWT / Swing
    Replies: 3
    Last Post: 10-17-2010, 03:51 AM
  5. private data members?
    By blueduiker in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •