Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Loading different implementations of a class

Loading different implementations of a class

Scheduled Pinned Locked Moved Java
tutorialquestiondatabasegame-devdata-structures
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Klazen
    wrote on last edited by
    #1

    Hey all, let me start off by saying that I'm not totally sure of whether or not I'm phrasing my question right, but here goes. I'm trying to make a trading card game, and I want to make it expandable (not to mention I don't want to hard code the cards into the game). I was brainstorming the best way to have different cards (represented as objects) and let them have different actions in their methods (for example, onPlay() and onDiscard() will have different implementations for different cards). What I want to be able to do is, on initiation, have the program load these cards into an array of AbstractCard (which all card objects will implement), from which the game can use as its database of cards. At first, I thought of storing the actions in a text file, and then having the card determine its action based on what was saved in the text file. However, I quickly realized how messy that would be, and had a different idea. Perhaps I could create a different class for each card, and compile them all separately and put them in a location on the hard drive. The program would then check that directory and load all the compiled classes it finds there, declare objects from them, and put them in the AbstractCard array (to me, this sounds similar to what a plug-in system does). So, my question(s) is(are), is this a good way of going about this? If so, how should I implement it? With regards to the implementation, this sounds kind of like reflection, but I have no experience in that area and so am not sure how to go about doing that. Thanks, Chuck Murphy

    N 1 Reply Last reply
    0
    • K Klazen

      Hey all, let me start off by saying that I'm not totally sure of whether or not I'm phrasing my question right, but here goes. I'm trying to make a trading card game, and I want to make it expandable (not to mention I don't want to hard code the cards into the game). I was brainstorming the best way to have different cards (represented as objects) and let them have different actions in their methods (for example, onPlay() and onDiscard() will have different implementations for different cards). What I want to be able to do is, on initiation, have the program load these cards into an array of AbstractCard (which all card objects will implement), from which the game can use as its database of cards. At first, I thought of storing the actions in a text file, and then having the card determine its action based on what was saved in the text file. However, I quickly realized how messy that would be, and had a different idea. Perhaps I could create a different class for each card, and compile them all separately and put them in a location on the hard drive. The program would then check that directory and load all the compiled classes it finds there, declare objects from them, and put them in the AbstractCard array (to me, this sounds similar to what a plug-in system does). So, my question(s) is(are), is this a good way of going about this? If so, how should I implement it? With regards to the implementation, this sounds kind of like reflection, but I have no experience in that area and so am not sure how to go about doing that. Thanks, Chuck Murphy

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      Inheritance is your friend! This is the classic example of using it. Start with your nice basic abstract class:

      abstract class Card
      {
      // put any common stuff in:
      private String description;

      Card(String description)
      {
      this.description = description;
      }

      // ... etc

      // add in any abstract stuff:
      abstract void onPlay();
      abstract void onDiscard();
      }

      Then for each type of card you need, have another class:

      class RealCard extend Card
      {
      RealCard()
      {
      super("This is a real card");
      }

      void onPlay()
      {
      // stuff
      }
      void onDiscard()
      {
      // other stuff
      }
      }

      That is the better way to design things. The second part of how to store the objects is covered by a simple enough idea - serialization. Get your card classes right and hard code them to start with, then worry about the persistaance; it should be relatively easy.


      Panic, Chaos, Destruction. My work here is done.

      K 1 Reply Last reply
      0
      • N Nagy Vilmos

        Inheritance is your friend! This is the classic example of using it. Start with your nice basic abstract class:

        abstract class Card
        {
        // put any common stuff in:
        private String description;

        Card(String description)
        {
        this.description = description;
        }

        // ... etc

        // add in any abstract stuff:
        abstract void onPlay();
        abstract void onDiscard();
        }

        Then for each type of card you need, have another class:

        class RealCard extend Card
        {
        RealCard()
        {
        super("This is a real card");
        }

        void onPlay()
        {
        // stuff
        }
        void onDiscard()
        {
        // other stuff
        }
        }

        That is the better way to design things. The second part of how to store the objects is covered by a simple enough idea - serialization. Get your card classes right and hard code them to start with, then worry about the persistaance; it should be relatively easy.


        Panic, Chaos, Destruction. My work here is done.

        K Offline
        K Offline
        Klazen
        wrote on last edited by
        #3

        Well, you see, I'm already doing that, I'm just looking for how to offer users the ability to create their own cards and add them to the game, without needing to get the source, add some classes, and recompile the game. Also, assume here that these users are trusted, I know someone could stick some malicious code in one of these user-created cards if I follow this method ;) Here's some psuedo-code:

        LinkedList<AbstractCard> lstAllCards = new LinkedList<AbstractCard>();
        for each file in directory "user_cards/"
        {
        if file is a java class //Maybe just attempt to add it, and catch an exception if it doesn't work
        {
        AbstractCard newCard = file.instantiate() //I don't know what to do here
        lstAllCards.add(newCard);
        }
        }

        From what I've read of reflection, the way to do this might be to try this:

        AbstractCard newCard = Class.forName(fileName).newInstance();

        Also, after reading this, I may want to have the card list be a LinkedList<Class>, not a LinkedList<AbstractCard>, so that I can instantiate new cards at will without having to refer back to these initial instances. Am I on the right track? EDIT: angle brackets weren't showing up for the generics, replaced them with "& lt;" and "& gt";

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups