Enum containing class?

Discussion in 'Plugin Development' started by Poppi, Jun 7, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Poppi

    Hello,
    For the last couple of days I've been working on a project, and I thought of the idea of using enums to access a specific class instead of having to go to the class directly.

    It works like this:
    Code:java
    1. public enum LookupClass{
    2.  
    3. CLASS1(new Class1()),
    4. CLASS1(new Class1()),
    5. CLASS1(new Class1());
    6.  
    7. Whatever classInstance;
    8. LookupClass(WhateverNeededToGet here){
    9. classInstance = here;
    10. }
    11.  
    12. public Whatever getClassInstance(){
    13. return classInstance;
    14. }
    15.  
    16. }


    Code:java
    1. public class Class1 extends AbstractClass{
    2.  
    3. Plugin pl
    4. public Class1(Plugin pl){
    5. }
    6.  
    7. public int getNumber(){
    8. return 0;
    9. }
    10. }


    Now each of those classes extends an abstract class.

    My question is how would I be able to access the class and not the abstract class it extends?
     
  2. Offline

    Rocoty

    Poppi I would go for Generics. What I don't understand is why you would need such behavior?

    EDIT: On second thought. Generics wouldn't work either. Only thing I can think of is casting, to be honest. Which makes this all a bundle of bad practice. Which in turn leads back to the question: How would you benefit from such behavior?
     
  3. Offline

    Poppi

    Im trying to make an overly simple API for people, because I'm a huge fan of making things easy for others. It would also be a very easy process to expand on the enum and add more methods for the API users to use.
     
  4. Offline

    Rocoty

    Well. If I were to do something similar to this (which I still don't see the point of), I would make a Utility class with a Set<TheAbstractClass>. You just put an instance of each of the classes into the set. Then a method which returns the properly cast instance based on the class passed to the method. You would have to use generics and reflection. The method declaration would be something along these lines:
    Code:java
    1. //You can make it static if it is a utility method
    2. public <T extends TheAbstractClass> T getInstance(Class<T> clazz) {
    3. //Try to figure this out
    4. }
     
Thread Status:
Not open for further replies.

Share This Page