Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Problem-specific design question

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem-specific design question

    Say I have a Ninja with certain configurable skills. By configurable, I mean the program reads in a configuration file and adds them in at runtime. ie:

    Ninja:
      color: Red
      Skills:
        - High Jump
        - Invisibility

    Now, assume these skills need to dynamically add functionality to the Ninja class. As in, if we configure Ninja to have a High Jump skill, then the class needs to follow the CanHighJump Interface, with public void highJump() as a method.

    Now there are two ways I can think of to go about this. My first reaction would be to have a Ninja class with String color and then use Reflection to add in the functionality. However, now I realize I could do it through a relational database as well. Have a Ninja class with String color, and forget about any objects relating Ninjas with Skills - instead, whenever I need to check for a skill, just make a call to the database retrieving the possible skills that class can use. Those are the only two truly dynamic solutions I can come up with, and I can't comprehend the design pros/cons of each. I have a feeling the database solution would be far more scale-able, but the reflection method would make most sense to me when coding. No need to guess which skills a Ninja has, because I could easily check for the interface.

    I'm hoping someone can give me a bit of insight over a standard design solution for this problem. Whether it be one of the two I came up with, or more likely something i didn't think of.

    Thank you,
    Phil


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem-specific design question

    With questions like these, it's almost always entirely dependent on how it fits best into your head, so what we say doesn't really matter.

    But for what it's worth, I try to keep things as simple as possible- why not just have a List of Skills, and populate that based on the config file? Then each Skill would contain the code that does the work for that skill instead of an interface that is added to the Ninja class. To check whether a Ninja had that skill, you'd just check whether its List contained an instanceof that Skill (you could optimize it if you really felt the need, but that's the gist).

    This would also allow you to have multiple instances of Ninja, each with different Skills, and it would make adding Skills later easier, IMHO.

    But like I said, it's really up to how you see it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem-specific design question

    Quote Originally Posted by KevinWorkman View Post
    Then each Skill would contain the code that does the work for that skill
    Hmm, I'd say I'm familiar with java yet I don't understand how this would work. So say I want the Ninja to highJump, I would then...

    ArrayList<Skill> mySkills = new ArrayList<Skill>
    mySkills.add(new HighJump())
    mySkills.add(new Invisibility())
     
    Ninja myRedNinja = new Ninja("Red", mySkills)
    Skill jump = myRedNinja.getSkill(HighJump)
    if(jump != null){
      jump.executeSkill()
    }else{
      //can't jump
    }

    The skill would gain a reference to the Ninja in the Ninja's constructor, thus executing the skill as expected. This code just reads strangely to me because it seems as if the Ninja is not the one jumping. Although i always read how reflection should be avoided - and this is basically doing the same thing as the relational database, so it's obviously not a bad solution. Two questions then.

    1) I'm understanding your solution correctly?
    2) Are there any large differences between using this solution as opposed to having the database? I mean in terms of functionality. The reason i ask is because this game Ragnarok uses a database to solve this exact problem, but I can't tell if it was a functional decision or solving a scaling problem (thousands of monsters with skills)

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem-specific design question

    Quote Originally Posted by olsonpm View Post
    1) I'm understanding your solution correctly?
    I think you get the gist of it, although I did leave the implementation details in the air.

    Quote Originally Posted by olsonpm View Post
    2) Are there any large differences between using this solution as opposed to having the database? I mean in terms of functionality. The reason i ask is because this game Ragnarok uses a database to solve this exact problem, but I can't tell if it was a functional decision or solving a scaling problem (thousands of monsters with skills)
    There is a huge functional difference, in that "my" way avoids anything like database lookups or reflection. But in terms of what is possible with either approach, they're probably pretty similar. I can't really speak for their decisions, but in that game, does each class of monster contain the same skills? I gathered that you wanted different Ninja instances to have different skills. And I would also predict that "my" approach would be completely scalable- thousands of Ninjas probably isn't too bad, as long as you aren't displaying them all at the same time (not that a database would fix that problem at all).

    In the end, just go with whatever way fits in your brain the best. I like my way because I probably could have implemented it in the time it took to write this post, but I don't really know anything about databases. If you're a database guy, go that route. I don't really put much importance in the "what is the absolute best way to do this" first step, as everything is going to change by the end anyway.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem-specific design question

    I'm not a database guy, but I couldn't figure out why they used a database to solve that problem. I will post on their forum to understand that portion.

    It seems to me that as long as your solution is scalable, it will be the route I take. I do not want to use reflection if I don't have to, and this is very flexible.

    Thanks for your time,
    Phil

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem-specific design question

    Well, they probably used a database so that they could centralize it. Also, it's a bit of two different questions- they might be storing this information in a database, but that doesn't describe how they're using and accessing that data in the game. Sure, they're looking the data up in a database, but then what? I wouldn't be surprised if they're building a data structure similar to the one I suggested.

    So, like I said, it's a bit of two different questions: how are you going to store the information (text config file, database, hardcoded, etc), then how are you going to represent that data in the data structures of the game?

    But again, I know nothing about that game, so I can't speak for them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem-specific design question

    I wouldn't be surprised if they're building a data structure similar to the one I suggested.
    I guess that makes sense. Where they might have 6000 monsters with associated skills, but the game only needs maybe 500 of them in memory at a time. That would be the only reason to use it in that situation correct? Because otherwise they might as well build the structure and hold it in memory, saving database calls.

Similar Threads

  1. Help in possible Design UI
    By beni.kurniawan in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2011, 10:51 AM
  2. OS Specific Jars
    By sinistaDev80 in forum Java Theory & Questions
    Replies: 0
    Last Post: March 26th, 2011, 09:29 AM
  3. Class design
    By arithmetics in forum Object Oriented Programming
    Replies: 4
    Last Post: November 4th, 2010, 08:44 AM
  4. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM
  5. Design question about maps
    By KaleeyJ in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2010, 04:17 AM