[Java Web] backend interface Version Control
This type of information is rare. According to the current project, I am confused about one idea for your reference only. You are welcome to discuss it.
// Action layer class Action {// currently available version Proxy version1_0 = new Proxy1_0 (); Proxy version1_1 = new Proxy1_1 (); void execute () {// obtain the version number from the client and the operation String version = "1.1"; int opt = 0; // select a different proxy switch (version) {case "1.0" based on the version number ": version1_0.proxy (opt); break; case "1.1": version1_1.proxy (opt); break ;}}} // version Proxy abstract class proxy {// The protected Service service class that actually processes the business; // The Proxy method does not allow override of public final void proxy (int opt) {switch (opt) {case 0: service. a (); break; case 1: service. B (); break; case 2: service. c (); break; case 3: service. d (); break;} abstract void setService (Service service Service );} // version Proxy implementation class only needs to inject different Service class Proxy1_0 extends Proxy {@ Override void setService (service Service service) {this. service = new Service1_0 () ;}} class Proxy1_1 extends Proxy1_0 {@ Override void setService (Service service) {this. service = new Service1_1 () ;}// the business layer defines all interfaces of the current version that can run abstract class Service {// 1.0 public void () {throw new RuntimeException ("version error");} // 1.0 1.1 public void B () {throw new RuntimeException ("version error ");} // 1.0 1.1 public void c () {throw new RuntimeException ("version error");} // 1.1 public void d () {throw new RuntimeException ("version error") ;}/ *** business layer implementation class inherits the * disabled interfaces one by one version: rewrite and throw an exception * interfaces of previous versions: do not override method * modify the interface of the previous version: rewrite Method * New Interface of this version: add a method to the Service and throw an exception. Only the */class Service1_0 extends Service {@ Override public void a () is rewritten in this class () {// do 1.0 a} @ Override public void B () {// do 1.0 B} @ Override public void c () {// do 1.0 c} class Service1_1 extends service=0 {@ Override public void a () {throw new RuntimeException ("version error");} @ Override public void c () {// do 1.1 c} @ Override public void d () {// do 1.1 d }}