Java variables: implementation methods After declaration?

Consider a simple example below of a method implementation in Enum. One of the problems with this method is that when you have many instances of enum, you can no longer visually see them all at once, as a list. That is, if we had many toys, I would like to see "DOLL, SOLDIER, TEDDUBER, TRAIN AND ETK" together in one long list, and then after this list I could implement any necessary methods, for example. methods that are abstract in the listing itself.

Is there any way to do this? Or do you need to implement methods when you declare individual instances of enum, as in the example below?

public enum Toy { DOLL() { @Override public void execute() { System.out.println("I'm a doll."); } }, SOLDIER() { @Override public void execute() { System.out.println("I'm a soldier."); } }; //abstract method public abstract void execute(); } 
+6
source share
3 answers

One of the ways that comes to mind is to leave the implementation of abstract methods to separate implementation classes, for example:

 interface ToyBehaviour { void execute(); } public enum Toy { DOLL(new DollBehaviour()), SOLDIER(new SoldierBehaviour()); private final ToyBehaviour behaviour; Toy(ToyBehaviour impl) { behaviour = impl; } public void execute() { behaviour.execute(); } } class DollBehaviour implements ToyBehaviour { public void execute() { System.out.println("I'm a doll."); } } 

This setting allows you to create classes of behavior in separate files in case your implementation is complex enough to guarantee separation.

In the event that the implementation is simple enough to include it in one enum class, you can put the interface and behavior classes as children of the enum class:

 public enum Toy { // enum values DOLL(new DollBehaviour()), SOLDIER(new SoldierBehaviour()); private final ToyBehaviour behaviour; Toy(ToyBehaviour impl) { behaviour = impl; } public void execute() { behaviour.execute(); } // behaviour interface interface ToyBehaviour { void execute(); } // behaviour implementation (sub)classes static class DollBehaviour implements ToyBehaviour { public void execute() { System.out.println("I'm a doll."); } } // etc ... } 

I would choose the first implementation myself if the hierarchy of the implementation classes is very trivial.

+5
source

If you want more compact listing declarations, the only ways I can do this are:

if you can build your methods from initializer variables:

 public enum Toy { DOLL("doll"),SOLDIER("soldier"); private Toy(String name){ this.name=name;} public void execute(){ System.out.println("I'm a "+name );} } 

or, which is a little more complicated, the same with functions, if the behavior is more complex -

 abstract class SomeToyMethod { abstract void execute(); public SomeToyMethod DOLL_METHOD = new SomeToyMethod(){ public void execute(){ System.out.println("I'm a doll");}) public SomeToyMethod SOLDIER_METHOD = new SomeToyMethod(){ public void execute(){ System.out.println("I'm a soldier");}) public enum Toy { DOLL(SomeToyMethod,DOLL_METHOD),SOLDIER(SomeToyMethod.SOLDIER_METHOD); private Toy(SomeToyMethod method){ this.method=method;} public void execute(){ method.execute();} } 
+4
source

You can try something like:

 public enum Toy { DOLL, SOLDIER, ANOTHER_TOY; public static void execute(Toy toy) { switch(toy) { case DOLL: System.out.println("I'm a doll."); break; case SOLDIER: System.out.println("I'm a soldier."); break; case ANOTHER_TOY: System.out.println("I'm another toy."); break; } } } 

Not very pretty, but it supports listing declarations together.

+2
source

Source: https://habr.com/ru/post/912259/


All Articles