Several subclasses, such as any of them?

It will be a little difficult to explain, but I will try my best.

I want to have several elements that with each click have a different result. Otherwise, they all have the same variables. So basically I have the Item class, and subclasses of Ball, Rope, Book and many, many more. I want to be able to easily add elements if I want. The Item class has the name String, a String description, and the 'onUse' method, which overrides each of the subclasses.

I would like to handle this as dynamic as possible, which means that I need one method in my Frame / Activity (for Android, but Java is applied) to execute the method.

I use this for one method:

public void useItem(Item i)
{
    i.onUse();
}

My problem is that I have different items stored in the database. I want to randomly grab one of the elements from the database and point it out; except that I have no way of knowing which of the subclasses, for example, this. I tried to save the data type in the database, but this does not work ... Pseudocode:

//Database cursor stuff
//...
                String itemType = c.getString(6);

                //Get the class name if possible
                Class itemClass= null;
                try {
                    itemClass = Class.forName(itemType);
                } catch (ClassNotFoundException)
                    e.printStackTrace();
                }

            Item l = null;
            // Check here what type it is!
            if (itemClass.equals(Ball.class)) {
                l = new Ball(name,description);

            } else if(lootClass.equals(Book.class)){

                l = new Book(name,description); 
            }

Seeing that there are so many different elements is not very cool since I will need to go through many classes (plus I often get a ClassNotFoundException)

What do you say, Stackoverflow? What would be a better approach?

+4
source share
2 answers

factory - , , . , factory, , :

Item item = (Item) Class.forName(theDBField).newInstance();
item.setName(name);
item.setDescription(desc);

factory, , . factory , , .

.

+1

Factory (aka Factory).

Factory - . , .

, :

Model

public abstract class Model {  
   protected abstract void init();
}

public class Book extends Model {
 @Override
protected void init() {
    System.out.println("I'm a Book");
}
}

public class Ball extends Model {
    @Override
protected void init() {
    System.out.println("I'm a Ball");
}
}

DbModelFactory

public abstract class DbModelFactory {
    public abstract Model getModel(int modelId);
}

SimpleDbModelFactory

public class SimpleDbModelFactory extends DbModelFactory  {

    @Override
    public Model getModel(int modelId) {
        Model model = null;
        if(modelId == Const.MODEL_BOOK) {

            model = new Book();
        }
        else if(modelId == Const.MODEL_BALL) {

            model = new Ball();
        }
        else {// throw Exception

        }
        return model;
    }
}

Const

public class Const {
    public static final int MODEL_BOOK = 0;
    public static final int MODEL_BALL = 1;
}

Launcher

public class Launcher {
  public static void main(String[] args) {
    DbModelFactory factory  = new SimpleDbModelFactory();
    Model book = factory.getModel(0);
    book.init(); // I'm a Book

    Model ball = factory.getModel(1);
    ball.init(); //I'm a Ball
    }
}

Factory pattern . factory pattern ,

2 Models book ball, , Model.

+2

All Articles