Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default OO Class Theory Question

    As I mentioned in my last post, I am writing a simple rpg allowing me to expand on my knowledge by putting it to practical use. However now my question is on theory. I created a basic hero template to include stats of the hero and the hero's inventory.

    However, the inventory only stores the name of an item. Would it be in my interest to make an Items class with a set number of static classes to define the description, damage, durability, buy cost, sell cost, and stat requirements?

    I'm thinking the answer is yes, but before I embark I would like some reassurance.

    I'm also curious as to how I would call it. Say I have a dagger class, and that dagger has a damage of 1. I'll leave all the other stats out for simplicity.

    So my class looks something like this
    Java Code:
    public class Items
    {
         private String itemName;
         private int itemDam;
    
         static class Dagger
         {
              itemName = "Dagger";
              itemDam = 1;
          }
    }
    Now say I go to equip the hero with this dagger. If I created a new Item object:
    Java Code:
    Items item = new Items();
    If I called
    Java Code:
    int heroDam = item.Dagger.itemDam();
    Would that correctly call the itemDam from the Items object?
    Last edited by Dark; 04-19-2011 at 06:39 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    I would have a parent Weapon class, perhaps make that abstract, with attributes for name, damage, etc.

    Then you could either simply instantiate a bunch of Weapons (in that case, don't make Weapon abstract), passing in the appropriate name and damage into each constructor, or you could extend Weapon (then Weapon could be abstract) for each type you're going to have.

    Your "static class" approach might work, but it's not the way I would go.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm, so would I be looking at something like
    Java Code:
    Items.Dagger dagger = new Items.Dagger();
    Is that what you're talking about?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Sort of, although I don't see a need to have Dagger be an inner class of Items. Methinks Items be better suited as a package, mayhap.

    I have no idea why I started talking like a pirate. ARR.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You're just getting ready for September 19th, which just so happens to be Talk Like A Pirate Day.

    What would the benefits of items being a package instead of a class? If I just instaniated a bunch of inner classes to an object array wouldn't it be just as efficient?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Quote Originally Posted by Dark View Post
    What would the benefits of items being a package instead of a class? If I just instaniated a bunch of inner classes to an object array wouldn't it be just as efficient?
    Hmm, I guess it depends on your definition of "efficient". It probably wouldn't matter in terms of memory use or computation cycles used.

    But if you're going for good OO design, having a ton of inner classes inside a "holder" class doesn't sound like the best way.

    What else does the Items class do or contain?

    I'm thinking your setup should be something more like this:

    Java Code:
    public class Weapon{
       public Weapon(int damage, String name){
          //set damage, name, etc
       }
       //getters for damage, name, etc
    }
    
    Weapon dagger = new Weapon(10, "Dagger");
    OR

    Java Code:
    public abstract class Weapon{
       //abstract getters for damage, name, etc
    }
    
    public class Dagger extends Weapon{
       public Dagger(){
          name = "Dagger";
          damage = 10;
       }
    
       @Override
       //getters for name, damage, etc.
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Well the inner class is currently storing the item name, item min damage, item max damage, item description, item durability, item max durability, item stat requirements.

    However if I was ever to modify the information stored, is there the possibility with inner classes that I would have to go add all of the new information instead of using a superclass? Maybe a formula is added instead of just a line of code?

    I'm not 100% sure if what I said was correct, or even made sense, but I'm trying to find a flaw in my design. Like I was once taught, you can prove a method a 1,000 different ways but you only need 1 way to prove it wrong.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    My question was, what else is the outer class storing or doing? If the answer is nothing, then I'd question the design.

    Another question: how are you planning on storing these Items? I assume that the player doesn't start out with all of them. With my approach, you could have a List of Weapons.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm, I've never really messed with lists. However that's the point of this project.

    The outer class is currently just intializing variables. Past that, I have no plans for it yet.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm, it seems I skipped over part of one of your posts last night in my sleep deprivation. I see what you mean, and I will indeed use it.

    EDIT: As I was starting to implement the code, I came across a problem I see happening. If durability is stored within the object, and durability pertains only to the individual weapon, if I make a new weapon instead of an inner class new dagger how will I efficiently manipulate multiple daggers? There will be more than one dagger in the game, so I would have to create a new weapon object each time.
    Last edited by Dark; 04-20-2011 at 04:05 AM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I think Kevins idea to have an abstract super class would be the easiest. Maintaining it would probably be simpler. You can store each weapon type in a new file and package them all together. It would also be helpful because if you need to change common functionality you can change the super class and see the changes applied correctly through all the sub classes. The abstract super class appears to be more flexible and easier to manage.
    Last edited by sunde887; 04-20-2011 at 04:10 AM.

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ok so I have a weapons class file, you're suggesting that I should make another file called dagger.java that extends weapons.java? Just making sure I understand what you're saying.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ya, that should work, you can also abstract it more, where weapon has sub classes named blunt, sharp, magic etc if you want. With this than all the classes would have some common functionality and then for common functionality for all blunt weapons can be stored in the blunt class.

    That is merely a suggestion, the final decision rests on what you think is best. There is a lot of oo goodness to be had here.

  14. #14
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Well currently I have no intention of storing a class of item just yet, as this isn't going to be anything more than a short game that just lets me learn more.

    I think I will just make a class file for each item that extends a weapon class, and then later on if I plan on making a more indepth classification system I can always copy pasta the crap out of my code.

    However I'm still curious as to how I would manage multiple daggers.

    Ie. Hero kills Goblin, Goblin drops loot containing dagger.
    Do I create a new dagger every time the hero picks one up? And then remove the reference to it when he sells it or drops it so the heap deletes it? Then my storing would have to be an array list, and then my question is I don't know how I would have the problem remember the new item slot number.

    If I added an array slot number to my class would that work? I'm not sure if what I just said made any sense to anyone but me.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  15. #15
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If I were doing this, I would define a Weapon interface, which probably extends an Item interface, and probably then an AbstractWeapon class that provides basic common weapon related services, that possibly extends an AbstractItem class that provides basic Item level services, though I would tend to err on the side of maximum abstraction and only introduce an AbstractItem when it became clear it was needed.

    I would not make these static inner classes of Item. The purpose of static inner classes is to handle cases where 'is-a' and 'has-a' don't work well from either a design perspective, or an efficiency perspective. For example, Color.yellow defines a specific common instance of Color. Even though yellow is-a color, it's a very common instantiation, and there are potentially large efficiency gains to be had by statically defining that instance.

    Of course, even the Java API does not follow this concept rigorously. Rectangle.Double is a specific type of rectangle. I don't know why they didn't define a DoubleRectangle class instead (of course, with generics, you wouldn't use either of those approaches if you were going to make a Rectangle class today).

    Sometimes, a holder class is defined that contains common functionally related static constants, such as java.lang.Math. I think this is kind of what you were going for, but you're misusing the concept, because the 'is-a' relationship holds between Item and Weapon.

  16. #16
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Well I'm not going with the inner class idea that I was originally intending to use, yet the weapon superclass that was suggested.

    My question now is how do I handle multiple dagger objects? There are going to be multiple daggers in the game, so how do I save the slot in the array list to the item so that I can correctly delete the chosen one instead of just the first one in the array?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    My 2 cents:
    Java Code:
    InventoryItem -+- Weapon -+- Dagger
                   |          +- Sword 
                   |          +- Bow
                   |
                   +- Potion -+- Healing
                              +- Strength

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When you go to sell an item you can make a display of all the items in your bag along with an appropriate index. Then get user input to sell the correct item.

  19. #19
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I would define containers for holding Items, and an Item can hold other Items. A Hero should have-a Set of Items, which may include a bag/satchel Item, which then has-a list of other items. I would not use an ArrayList, since an ArrayList allows the same object to occupy multiple slots. I would use a Set instead, because a particular Dagger/Bazooka can only belong to one container at a time, which is proper for modelling multiple instance of real world objects.

    Suppose a bag of weapons is dropped. The Bag is-a Item that has-a bunch of other Items. If your Hero picks it up, he now has-a Item that has-a Dagger Item (supposing the bag has a Dagger in it). Maintain the specific object references, because this will allow you to add instance specific features later on. For example, you may decide to introduce the idea of remaining-usefulness to a Weapon, and if you do, you'll need to keep track of that on an instance by instance basis. A Dagger that is half used up in teh bag is not the same as a brand new one.

  20. #20
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm, I'll experiment. That's a good idea but I may or may not need to store the array slot info inside the object.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Page 1 of 2 12 LastLast

Similar Threads

  1. Minimax game theory with mancala
    By jigglywiggly in forum New To Java
    Replies: 2
    Last Post: 01-01-2010, 12:04 PM
  2. [SOLVED] Class question
    By Supamagier in forum Advanced Java
    Replies: 19
    Last Post: 05-12-2009, 05:11 PM
  3. I have some question about jar and class??
    By low224 in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 04:02 AM
  4. [SOLVED] Need help in theory of Java in netbeans
    By kirly in forum New To Java
    Replies: 3
    Last Post: 11-11-2008, 05:47 AM
  5. question regarding class
    By kavithas in forum New To Java
    Replies: 4
    Last Post: 11-16-2007, 09:12 AM

Posting Permissions

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