泛型模板化设计DEMO

2024-10-09 11:18:28 浏览数 (1)

泛型模板化设计DEMO

1. 定义Result泛型类

代码语言:javascript复制
package com.example.core.mydemo.java.fanxing;

public class Result<T> {
    T response;

    public T getResponse() {
        return response;
    }

    public void setResponse(T response) {
        this.response = response;
    }

}

2.定义了程序的主要实现流程

代码语言:javascript复制
package com.example.core.mydemo.java.fanxing;

public abstract class BaseService<T> {
    /**
     * 定义了程序的主要实现流程-骨架
     * @return
     */
    public Result handle(BaseRequest request) throws Exception{
        Result result = new Result();
        method1();
//        result.setResponse(method2());

        /**
         * request与response对象的绑定关系
         */
        Class<T> responseClass = request.getResponseClass();
        T rspt = null;
        rspt = responseClass.newInstance();

        rspt = method2();
        result.setResponse(rspt);
        return result;
    }

    /**
     * 返回值-泛型,根据不同的业务返回不同的响应类型
     * @return
     */
    private T method2() {
        T response = initResponse();//获取子类初始化的响应实例
        System.out.println("BaseService method2");
        return response;
    }

    /**
     * 公共处理业务
     */
    private void method1() {
        System.out.println("BaseService method1");
    }

    /**
     * 响应类型-泛型,提供出去给具体实现类进行初始化
     * @return
     */
    protected abstract T initResponse();
}

3.请求基类和返回基类

代码语言:javascript复制
package com.example.core.mydemo.java.fanxing;

public interface BaseRequest<T extends BaseResponse> {

    Class<T>  getResponseClass();

}


package com.example.core.mydemo.java.fanxing;

public class BaseResponse {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

4.第一个实现类

代码语言:javascript复制
package com.example.core.mydemo.java.fanxing;

public class ChinaRequest implements BaseRequest<ChinaResponse>{

    @Override
    public Class<ChinaResponse> getResponseClass() {
        return ChinaResponse.class;
    }

}

package com.example.core.mydemo.java.fanxing;

import java.util.List;

public class ChinaResponse extends BaseResponse{
    List<String> province;

    public List<String> getProvince() {
        return province;
    }

    public void setProvince(List<String> province) {
        this.province = province;
    }

}

package com.example.core.mydemo.java.fanxing;

import java.util.Arrays;

public class ChinaServiceImpl extends BaseService<ChinaResponse>{

    /**
     * 实现抽象方法,初始化返回值类型
     * @return
     */
    @Override
    protected ChinaResponse initResponse() {
        ChinaResponse chinaResponse = new ChinaResponse();
        chinaResponse.setName("中国");
        chinaResponse.setProvince(Arrays.asList("湖南","山东"));
        return chinaResponse;
    }

}

5.第二个实现类

代码语言:javascript复制
package com.example.core.mydemo.java.fanxing;

public class AmericaRequest implements BaseRequest<AmericaResponse>{

    @Override
    public Class<AmericaResponse> getResponseClass() {
        return AmericaResponse.class;
    }
}

package com.example.core.mydemo.java.fanxing;

import java.util.List;

public class AmericaResponse extends BaseResponse{
    List<String> state;

    public List<String> getState() {
        return state;
    }

    public void setState(List<String> state) {
        this.state = state;
    }

}

package com.example.core.mydemo.java.fanxing;

import java.util.Arrays;

public class AmericaServiceImpl extends BaseService<AmericaResponse> {
    /**
     * 实现抽象方法,初始化返回值类型
     * @return
     */
    @Override
    protected AmericaResponse initResponse() {
        AmericaResponse americaResponse = new AmericaResponse();
        americaResponse.setName("美国");
        americaResponse.setState(Arrays.asList("纽约","华盛顿"));
        return americaResponse;
    }

}

6.测试类

代码语言:javascript复制
package com.example.core.mydemo.java.fanxing;

import com.example.core.mydemo.json2.GsonUtils;
import com.google.gson.Gson;

/**
 * output:
 * BaseService method1
 * BaseService method2
 * {"province":["湖南","山东"],"name":"中国"}
 * BaseService method1
 * BaseService method2
 * {"state":["纽约","华盛顿"],"name":"美国"}
 */
public class FanxingTest {
    public static void main(String[] args) {
        BaseService<ChinaResponse> baseService = new ChinaServiceImpl();
        ChinaRequest chinaRequest = new ChinaRequest();
        Result<ChinaResponse> result = null;
        try {
            result = baseService.handle(chinaRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ChinaResponse chinaResponse = result.getResponse();
        System.out.println(new Gson().toJson(chinaResponse));

        BaseService<AmericaResponse> americaService = new AmericaServiceImpl();
        AmericaRequest americaRequest = new AmericaRequest();
        Result<AmericaResponse> americaResult = null;
        try {
            americaResult = americaService.handle(americaRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        AmericaResponse americaResponse = americaResult.getResponse();
        System.out.println(new Gson().toJson(americaResponse));
    }
}

0 人点赞