[Java Web] backend interface Version Control

Source: Internet
Author: User

[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 }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.