Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (714)
Games in Android Showcase (214)
games submitted by our members
Games in WIP (788)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Type checking an init method  (Read 739 times)
0 Members and 1 Guest are viewing this topic.
Offline DayTripperID

Senior Devvie


Medals: 8
Projects: 1
Exp: 1-3 months


Living is good!


« Posted 2016-12-27 15:12:16 »

Hello Java Gaming world! I have a question about type checking. So let me describe my situation.

I have this interface

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public interface State<T extends Entity> {
   
   public void init(T t);
   
   public void update();
   
   public void render(Screen s);
   
   public void processCollision(T t);
   
   public T getEntity();
}


And this class:

1  
2  
3  
4  
5  
6  
7  
8  
public abstract class MobState<T extends Mob> implements State<Mob> {

   protected T m = null;
   
   // more fields defined here...
   
   // some methods defined here...
}


In my code, Mob extends Entity, and MobState<T extends Mob> implements State<Mob>. Ball extends Mob, and BallNormalState extends MobState<Ball>:

1  
2  
3  
4  
5  
6  
7  
8  
public class BallNormalState extends MobState<Ball> {
         
   public void init(Mob m) {
      this.m = (Ball) m;
   }
   
   // more methods defined here...
}


My question is, in BallNormalState::init, I can't pass anything more specific than Mob because of the interface, so how can I best enforce type checking? It needs to be Ball and not any other type of Mob! Thanks for any advice!  Cool

Living is good!
Offline Abuse

JGO Ninja


Medals: 57


falling into the abyss of reality


« Reply #1 - Posted 2016-12-27 15:44:54 »

I'd advise that you don't get yourself wrapped up in unnecessary Generics boilerplate.

What value is this bringing to your code?
Is it reducing, or increasing, complexity & readability?
Offline DayTripperID

Senior Devvie


Medals: 8
Projects: 1
Exp: 1-3 months


Living is good!


« Reply #2 - Posted 2016-12-27 16:24:20 »

Maybe I'm over complicating things. I'm trying to come up with something that would be easily reusable in another project and applicable to entities that don't extend Mob. I'm also in part just experimenting.

Before, I had a State interface and T was just Mob, which worked just fine.

As far as complexity and readability is concerned, I am trying to reduce the former and increase the latter. So what I will probably do is ditch the generic typing in favor of doing just that, because it is rather obfuscating.

Living is good!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2016-12-27 16:26:43 »

public abstract class MobState<T extends Mob> implements State<T> {
}

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
Offline DayTripperID

Senior Devvie


Medals: 8
Projects: 1
Exp: 1-3 months


Living is good!


« Reply #4 - Posted 2016-12-27 16:35:51 »

Thanks dude, I don't know why I didn't catch that.

Living is good!
Offline SHC
« Reply #5 - Posted 2016-12-27 16:39:50 »

@Abuse

Generics aren't necessarily the evil, it is just better to use them internally and not expose them. For example, I have this declaration in my DynamicTree broadphase.

1  
class DynamicTree<AABBType extends DynamicTree.AABB, CollisionType>

That is because I have the same implementation for both 2D and 3D scenes! I just require the AABB type to follow an interface, and CollisionType is simply the component type, CollisionComponent2D and CollisionComponent3D are the actual types that get there.

However, it is bad if you expose this class to the user, as that will make the user to use generics in all the type declarations, so I have wrapper classes DynamicTree2D and DynamicTree3D that I expose, they just use the DynamicTree class internally.

Pages: [1]
  ignore  |  Print  
 
 

 
CopyableCougar4 (111 views)
2017-03-24 15:39:42

theagentd (94 views)
2017-03-24 15:32:08

Rule (155 views)
2017-03-19 12:43:22

Rule (142 views)
2017-03-19 12:42:17

Rule (147 views)
2017-03-19 12:36:21

theagentd (159 views)
2017-03-16 05:07:07

theagentd (156 views)
2017-03-15 22:37:06

theagentd (132 views)
2017-03-15 22:32:18

theagentd (126 views)
2017-03-15 22:31:11

ral0r2 (155 views)
2017-03-03 11:52:41
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!