1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Would this be possible in any sense?

Discussion in 'Developer Support' started by Zasz, Jun 25, 2015.

  1. Zasz

    Joined:
    Jun 20, 2015
    Messages:
    183
    Likes Received:
    52
    HTML:
    1. public enum blah {
    2.  
    3.         something("Nametofind", new extendedclass());
    4.  
    5.        private Abstractclasstoreload areyoudtforno;
    6.        private String nudesplz;
    7.  
    8.        public blah(String nameofblah, Abstractclasstoreload dtf){
    9.               areyoudtforno = dtf;
    10.               nudesplz =nameofblah;
    11.        }    
    12.  
    13.        public Abstractclasstoreload createinstance(){
    14.               return new areyoudtforno();
    15.        }
    16.  
    17. }
    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();
     
  2. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,937
    Likes Received:
    1,266
    Typical pattern:

    Code (Java):
    1.  
    2. public class Application {
    3.      private Application instance;
    4.  
    5.      private Application() {
    6.           ...
    7.      }
    8.  
    9.      public static Application getInstance() {
    10.           return instance != null ? instance : (instance = new Application());
    11.      }
    12. }
    13.  
     
  3. frazboyz

    Joined:
    Nov 15, 2013
    Messages:
    339
    Likes Received:
    56
  4. Zasz

    Joined:
    Jun 20, 2015
    Messages:
    183
    Likes Received:
    52
    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.
    --- Double Post Merged, Jun 25, 2015, Original Post Date: Jun 25, 2015 ---
    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.
     
  5. Overflow

    Joined:
    Mar 26, 2014
    Messages:
    33
    Likes Received:
    4
    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 (Text):
    1. /**
    2. * Author: Tom
    3. * Date: 25/06/2015
    4. * Time: 22:12
    5. */
    6. public class FactoryExample {
    7.  
    8.     public static abstract class SomeAbstractClass {}
    9.  
    10.     public static class SomeAbstractClassImplA extends SomeAbstractClass {}
    11.  
    12.     public static class SomeAbstractClassImplB extends SomeAbstractClass {}
    13.  
    14.     public static class SomeAbstractClassImplC extends SomeAbstractClass {}
    15.  
    16.  
    17.     public enum SomeAbstractClassFactory {
    18.         IMPL_A() {
    19.             @Override
    20.             public SomeAbstractClass create() {
    21.                 return new SomeAbstractClassImplA();
    22.             }
    23.         },
    24.         IMPL_B {
    25.             @Override
    26.             public SomeAbstractClass create() {
    27.                 return new SomeAbstractClassImplB();
    28.             }
    29.         },
    30.         IMPL_C() {
    31.             @Override
    32.             public SomeAbstractClass create() {
    33.                 return new SomeAbstractClassImplC();
    34.             }
    35.         };
    36.  
    37.         public abstract SomeAbstractClass create();
    38.     }
    39.  
    40.     public enum SomeAbstractClassSingletonFactory {
    41.         IMPL_A() {
    42.             @Override
    43.             public SomeAbstractClass create() {
    44.                 return new SomeAbstractClassImplA();
    45.             }
    46.         },
    47.         IMPL_B {
    48.             @Override
    49.             public SomeAbstractClass create() {
    50.                 return new SomeAbstractClassImplB();
    51.             }
    52.         },
    53.         IMPL_C() {
    54.             @Override
    55.             public SomeAbstractClass create() {
    56.                 return new SomeAbstractClassImplC();
    57.             }
    58.         };
    59.  
    60.         private final SomeAbstractClass implementation;
    61.  
    62.         SomeAbstractClassSingletonFactory() {
    63.             implementation = create();
    64.         }
    65.  
    66.         public SomeAbstractClass getImpl() {
    67.             return implementation;
    68.         }
    69.  
    70.         protected abstract SomeAbstractClass create();
    71.     }
    72.  
    73.     public enum SomeAbstractClassLazySingletonFactory {
    74.         IMPL_A() {
    75.             @Override
    76.             public SomeAbstractClass create() {
    77.                 return new SomeAbstractClassImplA();
    78.             }
    79.         },
    80.         IMPL_B {
    81.             @Override
    82.             public SomeAbstractClass create() {
    83.                 return new SomeAbstractClassImplB();
    84.             }
    85.         },
    86.         IMPL_C() {
    87.             @Override
    88.             public SomeAbstractClass create() {
    89.                 return new SomeAbstractClassImplC();
    90.             }
    91.         };
    92.  
    93.         private SomeAbstractClass implementation;
    94.  
    95.         public SomeAbstractClass getImpl() {
    96.             if (implementation == null) {
    97.                 synchronized (this) {
    98.                     if (implementation == null) {
    99.                         implementation = create();
    100.                     }
    101.                 }
    102.             }
    103.             return implementation;
    104.         }
    105.  
    106.         protected abstract SomeAbstractClass create();
    107.     }
    108. }
    109.  
     
  6. Zasz

    Joined:
    Jun 20, 2015
    Messages:
    183
    Likes Received:
    52
    I appreciate the response, basically this is what I used to get what I wanted lol
    Code (Text):
    1.  
    2. public enum Yolo {
    3.  
    4.      
    5.         Test1("T1", new T1Call()), VerizonWireless("T2", new T2Call());   ;
    6.      
    7.         private String name; // Really not used right now
    8.         private AbstractThing ins;
    9.         Bosses(String names, Rooms r) {
    10.             name = names;
    11.             ins = r;
    12.        }
    13.      
    14.         public static AbstractThing createinstance(String bname){
    15.             for(int i = 0; i < values().length; i++){
    16.                 if(values()[i].name.equalsIgnoreCase(bname)){
    17.                     return values()[i].ins.newInstance();
    18.                 }
    19.             }
    20.             return null;
    21.         }
    22.     }
    23.  
    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.
     
  7. Overflow

    Joined:
    Mar 26, 2014
    Messages:
    33
    Likes Received:
    4
    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?
     
  8. Zasz

    Joined:
    Jun 20, 2015
    Messages:
    183
    Likes Received:
    52
    Exactly lol
     
  9. Overflow

    Joined:
    Mar 26, 2014
    Messages:
    33
    Likes Received:
    4
    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
     
  10. Zasz

    Joined:
    Jun 20, 2015
    Messages:
    183
    Likes Received:
    52

    Can you show me what you mean by chance?
     
  11. Overflow

    Joined:
    Mar 26, 2014
    Messages:
    33
    Likes Received:
    4
    From you other threads and some of the naming im assuming its for the dungeoneering bosses, and you want to create the room / boss handler?
     
  12. Zasz

    Joined:
    Jun 20, 2015
    Messages:
    183
    Likes Received:
    52
    Yeah, with what I displayed as my fix a few posts back works now, but I am always down to simplify + work better.
     
  13. Overflow

    Joined:
    Mar 26, 2014
    Messages:
    33
    Likes Received:
    4
    I'm guessing that your creating some form of room handler but this is what id suggest

    Code (Text):
    1. public class DGExample {
    2.  
    3.     public static abstract class RoomHandler {
    4.         public abstract void handle();
    5.     }
    6.  
    7.     public static abstract class BossRoomHandler extends RoomHandler {}
    8.  
    9.     public static class AsteaFrostwebHandler extends BossRoomHandler {
    10.  
    11.         @Override
    12.         public void handle() {
    13.             //Astea combat logic
    14.         }
    15.     }
    16.  
    17.     public static class IcyBonesHandler extends BossRoomHandler {
    18.  
    19.         @Override
    20.         public void handle() {
    21.             //Icy bones combat logic
    22.         }
    23.     }
    24.  
    25.     public enum BossType {
    26.         ASTEA_FROSTWEB("Astea frostweb", 466464) {
    27.             @Override
    28.             public BossRoomHandler create() {
    29.                 return new AsteaFrostwebHandler();
    30.             }
    31.         },
    32.         ICY_BONES("Icy bones") {
    33.             @Override
    34.             public BossRoomHandler create() {
    35.                 return new IcyBonesHandler();
    36.             }
    37.         };
    38.  
    39.         private final String name;
    40.         private final int[] ids;
    41.  
    42.         BossType(String name, int... ids) {
    43.             this.name = name;
    44.             this.ids = ids;
    45.         }
    46.  
    47.         public boolean valid() {
    48.             return !Npcs.getLoaded(ids).isEmpty();
    49.         }
    50.  
    51.         public abstract BossRoomHandler create();
    52.  
    53.         public static BossRoomHandler getHandler() {
    54.             for (BossType b : values()) {
    55.                 if (b.valid()) return b.create();
    56.             }
    57.             return null;
    58.         }
    59.  
    60.         public static BossRoomHandler getHandlerByName(String name) {
    61.             for (BossType b : values()) {
    62.                 if (b.name.equals(name)) return b.create();
    63.             }
    64.             return null;
    65.         }
    66.     }
    67. }
    68.  
     
  14. Zasz

    Joined:
    Jun 20, 2015
    Messages:
    183
    Likes Received:
    52
    cool, ill take a look and see what I may implement, thx
    --- Double Post Merged, Jun 26, 2015, Original Post Date: Jun 25, 2015 ---
    The overriding simplified a bit, thanks for helping remove unneeded code!
     

Share This Page

Loading...