Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Would this be possible in any sense?

Joined
Jun 20, 2015
Messages
183
HTML:
public enum blah {

        something("Nametofind", new extendedclass());
  
       private Abstractclasstoreload areyoudtforno;
       private String nudesplz;

       public blah(String nameofblah, Abstractclasstoreload dtf){
              areyoudtforno = dtf;
              nudesplz =nameofblah;
       }     

       public Abstractclasstoreload createinstance(){
              return new areyoudtforno();
       }

}

Some logic is flawed in it, but its 2:30AM and I tired, but the idea is there, and it would be cool to rebuild from many classes on the fly without doing if(name=="desired") new specificclass(); But would rather Abstractclasstoreload me = blah.something.newinstance();
 
Mod Automation
Joined
Jul 26, 2013
Messages
3,046
Typical pattern:

Java:
public class Application {
     private Application instance;

     private Application() {
          ...
     }

     public static Application getInstance() {
          return instance != null ? instance : (instance = new Application());
     }
}
 
Joined
Jun 20, 2015
Messages
183
But I want to create a new instance of one of many classes based off an enum.

So I could do
Abstractclassing me;
me = enum.class1.newinstance();
me = enum.class2.newinstance();

Trying the shortest way here.
 
Got it, so it does initialize in the enum, but I got it to create a new instance to return from my abstract class. So it works.
 
Joined
Mar 26, 2014
Messages
33
But I want to create a new instance of one of many classes based off an enum.

So I could do
Abstractclassing me;
me = enum.class1.newinstance();
me = enum.class2.newinstance();

Trying the shortest way here.
 
Got it, so it does initialize in the enum, but I got it to create a new instance to return from my abstract class. So it works.

From this it seems you want more of a factory design pattern i was unsure you wanted this to include the singleton pattern but the code below should show a basic, singleton and lazy singleton factory for how it could be achieved without using class objects and calls to newInstance.

Code:
/**
* Author: Tom
* Date: 25/06/2015
* Time: 22:12
*/
public class FactoryExample {

    public static abstract class SomeAbstractClass {}

    public static class SomeAbstractClassImplA extends SomeAbstractClass {}

    public static class SomeAbstractClassImplB extends SomeAbstractClass {}

    public static class SomeAbstractClassImplC extends SomeAbstractClass {}


    public enum SomeAbstractClassFactory {
        IMPL_A() {
            @Override
            public SomeAbstractClass create() {
                return new SomeAbstractClassImplA();
            }
        },
        IMPL_B {
            @Override
            public SomeAbstractClass create() {
                return new SomeAbstractClassImplB();
            }
        },
        IMPL_C() {
            @Override
            public SomeAbstractClass create() {
                return new SomeAbstractClassImplC();
            }
        };

        public abstract SomeAbstractClass create();
    }

    public enum SomeAbstractClassSingletonFactory {
        IMPL_A() {
            @Override
            public SomeAbstractClass create() {
                return new SomeAbstractClassImplA();
            }
        },
        IMPL_B {
            @Override
            public SomeAbstractClass create() {
                return new SomeAbstractClassImplB();
            }
        },
        IMPL_C() {
            @Override
            public SomeAbstractClass create() {
                return new SomeAbstractClassImplC();
            }
        };

        private final SomeAbstractClass implementation;

        SomeAbstractClassSingletonFactory() {
            implementation = create();
        }

        public SomeAbstractClass getImpl() {
            return implementation;
        }

        protected abstract SomeAbstractClass create();
    }

    public enum SomeAbstractClassLazySingletonFactory {
        IMPL_A() {
            @Override
            public SomeAbstractClass create() {
                return new SomeAbstractClassImplA();
            }
        },
        IMPL_B {
            @Override
            public SomeAbstractClass create() {
                return new SomeAbstractClassImplB();
            }
        },
        IMPL_C() {
            @Override
            public SomeAbstractClass create() {
                return new SomeAbstractClassImplC();
            }
        };

        private SomeAbstractClass implementation;

        public SomeAbstractClass getImpl() {
            if (implementation == null) {
                synchronized (this) {
                    if (implementation == null) {
                        implementation = create();
                    }
                }
            }
            return implementation;
        }

        protected abstract SomeAbstractClass create();
    }
}
 
Joined
Jun 20, 2015
Messages
183
From this it seems you want more of a factory design pattern i was unsure you wanted this to include the singleton pattern but the code below should show a basic, singleton and lazy singleton factory for how it could be achieved without using class objects and calls to newInstance.

I appreciate the response, basically this is what I used to get what I wanted lol
Code:
public enum Yolo {

      
        Test1("T1", new T1Call()), VerizonWireless("T2", new T2Call());   ;
      
        private String name; // Really not used right now
        private AbstractThing ins;
        Bosses(String names, Rooms r) {
            name = names;
            ins = r;
       }
      
        public static AbstractThing createinstance(String bname){
            for(int i = 0; i < values().length; i++){
                if(values()[i].name.equalsIgnoreCase(bname)){
                    return values()[i].ins.newInstance();
                }
            }
            return null;
        }
    }

Then make my abstract class have the newInstance method. And in the class extending the abstract just make it make a new version of itself.
 
Joined
Mar 26, 2014
Messages
33
I appreciate the response, basically this is what I used to get what I wanted lol
Code:
public enum Yolo {

   
        Test1("T1", new T1Call()), VerizonWireless("T2", new T2Call());   ;
   
        private String name; // Really not used right now
        private AbstractThing ins;
        Bosses(String names, Rooms r) {
            name = names;
            ins = r;
       }
   
        public static AbstractThing createinstance(String bname){
            for(int i = 0; i < values().length; i++){
                if(values()[i].name.equalsIgnoreCase(bname)){
                    return values()[i].ins.newInstance();
                }
            }
            return null;
        }
    }

Then make my abstract class have the newInstance method. And in the class extending the abstract just make it make a new version of itself.

So T1Call, T2Call all extend some class providing an abstract method called newInstance which creates the specific type of AbstractThing you want for that call type?

Is there any other functionality in the T1Call and T2Call classes?
 
Joined
Jun 20, 2015
Messages
183
So T1Call, T2Call all extend some class providing an abstract method called newInstance which creates the specific type of AbstractThing you want for that call type?

Is there any other functionality in the T1Call and T2Call classes?

Exactly lol
 
Joined
Mar 26, 2014
Messages
33
Exactly lol
If theres no other functionality in the T1 and T2 classes there essentially bloated code as you could completely bypass them by moving the create instance method to your enum and simply overriding the method for each enum value returning the abstract thing you want for each one
 
Joined
Jun 20, 2015
Messages
183
If theres no other functionality in the T1 and T2 classes there essentially bloated code as you could completely bypass them by moving the create instance method to your enum and simply overriding the method for each enum value returning the abstract thing you want for each one


Can you show me what you mean by chance?
 
Joined
Jun 20, 2015
Messages
183
Yeah, with what I displayed as my fix a few posts back works now, but I am always down to simplify + work better.
 
Joined
Mar 26, 2014
Messages
33
Yeah, with what I displayed as my fix a few posts back works now, but I am always down to simplify + work better.

I'm guessing that your creating some form of room handler but this is what id suggest

Code:
public class DGExample {

    public static abstract class RoomHandler {
        public abstract void handle();
    }

    public static abstract class BossRoomHandler extends RoomHandler {}

    public static class AsteaFrostwebHandler extends BossRoomHandler {

        @Override
        public void handle() {
            //Astea combat logic
        }
    }

    public static class IcyBonesHandler extends BossRoomHandler {

        @Override
        public void handle() {
            //Icy bones combat logic
        }
    }

    public enum BossType {
        ASTEA_FROSTWEB("Astea frostweb", 466464) {
            @Override
            public BossRoomHandler create() {
                return new AsteaFrostwebHandler();
            }
        },
        ICY_BONES("Icy bones") {
            @Override
            public BossRoomHandler create() {
                return new IcyBonesHandler();
            }
        };

        private final String name;
        private final int[] ids;

        BossType(String name, int... ids) {
            this.name = name;
            this.ids = ids;
        }

        public boolean valid() {
            return !Npcs.getLoaded(ids).isEmpty();
        }

        public abstract BossRoomHandler create();

        public static BossRoomHandler getHandler() {
            for (BossType b : values()) {
                if (b.valid()) return b.create();
            }
            return null;
        }

        public static BossRoomHandler getHandlerByName(String name) {
            for (BossType b : values()) {
                if (b.name.equals(name)) return b.create();
            }
            return null;
        }
    }
}
 
Joined
Jun 20, 2015
Messages
183
cool, ill take a look and see what I may implement, thx
 
The overriding simplified a bit, thanks for helping remove unneeded code!
 
Top