View Poll Results: Which do you prefer?

Voters
0. You may not vote on this poll
  • interface

    0 0%
  • Object

    0 0%
Results 1 to 6 of 6
  1. #1
    bc112354 is offline Member
    Join Date
    Mar 2014
    Location
    USA
    Posts
    2
    Rep Power
    0

    Default Better API, interface or Object?

    I am going to have a couple of API classes in a game I am creating. As devs, which would you prefer:

    Java Code:
    package test;
    
    public class RageWarrior implements Warrior{
    
    public Resource (included in API) getResourceForCreation(){
    
    return Main.gold;
    
    }
    
    public long getResourceAmountForCreation(){
    
    return 1000;
    
    }
    
    public int attack(){
    
    return 100;
    
    }
    .
    .
    .
    
    }
    or

    Java Code:
    Warrior rageWarrior = new Warrior(Resource, resourceAmount, attack);
    ?

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

    Default Re: Better API, interface or Object?

    Your poll lacks the answer 'it depends'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,870
    Rep Power
    5

    Default Re: Better API, interface or Object?

    I prefer the one that works :s
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Better API, interface or Object?

    With the proviso that I'm guessing at what you are writing...

    With this one:
    Java Code:
    public class RageWarrior implements Warrior
    are you intending on adding a new class for each type of warrior? For example, what if you came up (late in the day) with an idea for a SpikyWarrior? Would it be easier for you to write a new class, or simply add some stats in a data file which define you Warrior types? Think how moddable games work...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Better API, interface or Object?

    I'll choose "it depends".

    If it's a small, simple game with just 1 and only 1 type of Warrior, then I may initially go with just a concrete class. If more Warrior types are to be added later, I may apply either "Extract Interface" (to get an interface) or "Extract Superclass" (to get an abstract class) refactorings. Depending on the operations that are available in the Warrior types, the Template Method pattern may be applicable (SpecificWarrior extends AbstractWarrior). An interface can help with unit testing to make Warrior types easily mockable when testing classes that are dependent on it.

    In general, if you can't decide now, then maybe the requirements are not clear enough or not decided yet. If that's the case, apply the Lean principles of "Decide as late as possible" and "Deliver as fast as possible", and adapt your code when you actually can decide what you need to do. In other words, start small and simple, and modify as needed. If you're not well-versed with Refactoring, fill in the knowledge gap as it's an enabler for "good procrastination". ;-)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Better API, interface or Object?

    Simply looking at what's supplied in the OP, all the RageWarrior is is a set of hardcoded values.
    That's not a class...that's an instance.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. check whether a object is interface or class
    By rajeev in forum New To Java
    Replies: 20
    Last Post: 08-26-2013, 04:27 AM
  2. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  3. Replies: 0
    Last Post: 10-03-2011, 08:27 AM
  4. create an object of interface
    By yma16 in forum New To Java
    Replies: 6
    Last Post: 04-16-2011, 04:28 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
  •