How to return an object from enum in Java? -


I have several classes that apply the same interface. Objects for all those classes should be instanced in a main class. I am trying to do it in a manner with which this work can be done in a brilliant way (I thought through the medium). Example code: -

  Public interface Intr {// some methods} Public class C1 applied int {// some implementation} Public class C2 applied intet {// some implementation} ... Public class ck Applys Intr {// some implementation} Public class main class {enum ModulesEnum {// some code here to return C1 to the object of Ck {back to new C1 ()}, second {new C2 ()},. .. KTH {new ck ()}; }}   

Now in some of the above examples, with which I can get examples of CK new object from class C1. Or any other better mechanism instead of enum will also be appreciated.

  enum moduleAnnam {FIRST (new c1 ()), second (new c2 ( )); // and private module on an int (intr) {this.obj = intr; } Private Intra obj; Public Intr getObj () {back obj; }}   

Hope that helps move an implementation in every enumeration. If you want to use the object, then use the recipient.

  ModulesEnum.FIRST.getObj ();   

If you have a intrace and its implementation, then package protected , you can make ModulesEnum Can implement the public for In this way, you can only implement one instance per instance, they can explicitly create singleton for use without pattern.

You can of course use the factory . You want many examples for every implementation.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -