java 封装server并且一个server下多个implements使用枚举进行管理获取

package com.java.en;

import com.java.server.Trench;
import com.java.server.ipl.Trench1Lplm;
import com.java.server.ipl.TrenchLplm;

public enum TrenchEnum {
   trench1("trench", new TrenchLplm()),
   trench2("trench2", new Trench1Lplm())
   ;

   private String name;
   private Trench trench;

   TrenchEnum(String name, Trench trench) {
      this.name = name;
      this.trench = trench;
   }
   public Trench getTrench() {
      return trench;
   }

   public String getName() {
      return name;
   }

   public static Trench getValue(String name) {
      for (TrenchEnum ele : values()) {
         if (ele.getName().equals(name)) return ele.getTrench();
      }
      return null;
   }
}

package com.java.server;

public interface Trench {
    void start();
}

package com.java.server.ipl;

import com.java.server.Trench;

public class Trench1Lplm implements Trench {
    @Override
    public void start() {
        System.out.println("bbbbbb");
    }
}

package com.java.server.ipl;

import com.java.server.Trench;

public class TrenchLplm implements Trench {
    @Override
    public void start() {
        System.out.println("aaaaa");
    }
}
String str="trench";
        TrenchEnum.getValue(str).start();
打印输出数据:aaaaa

之前的webscoket没有灵感进行server枚举封装的来看这人

你可能感兴趣的:(java,jvm,开发语言)