高度抽象代码方法总结

目录

  • 通过抽象方法给子类实现,从而达到高度抽象的目的,这里针对不同类而言
  • 通过传递匿名内部类的形式将需要执行的内容传递给公共抽象类对应方法,这里适合类里面有多个方法需要抽象
  • future模式,异步执行,结果处理抽象, 具体实现各个子类实现
  • 反射,动态代理抽象

通过抽象方法给子类实现,从而达到高度抽象的目的,这里针对不同类而言

public class Test {
    public static void main(String[] args) {
        TestService testService = new TestService();
        testService.getResp(new Object());
    }

    private static class TestService extends BaseService {
        public Object getResp(Object req) {
            return service(req);
        }


        @Override
        protected void validateRequest(Object o) {
            System.out.println("来自TestService的验证");
        }
    }
}

public abstract class BaseService {

    /**
     * 总体是通过抽象方法给子类实现,从而达到高度抽象的目的
     */
    public final Response service(Request request) {
        System.out.println("调用公共的service");
        // 各个子类实现
        validateRequest(request);
        // 各个子类实现,具体业务逻辑, 此处省略getBiz获取各个子类实际业务逻辑类
        /*Response response = getBiz().biz(request);*/
        System.out.println("结束调用公共的service");
        // 只是展示
        return null;
    }

    protected abstract void validateRequest(Request request);

    /*protected abstract Biz getBiz();*/
}
  • 输出
调用公共的service
来自TestService的验证
结束调用公共的service

通过传递匿名内部类的形式将需要执行的内容传递给公共抽象类对应方法,这里适合类里面有多个方法需要抽象

public class Provider extends AbstractProvider {

    // 获取请求
    public void get(final String key) {
        executeCommand(key, () -> System.out.println("执行了获取指令, key:" + key), null);
    }

    // 判断是否存在
    public void exists(final String key) {
        executeCommand(key, () -> System.out.println("执行了判断是否存在指令, key:" + key), null);
    }

    public static void main(String[] args) {
        Provider provider = new Provider();
        provider.get("ss");
        provider.exists("sss");
    }
}

public abstract class AbstractProvider {

    protected  void executeCommand(String key, Command command, T defaultValue) {
        command.execute();
    }
}

public interface Command {
    void execute();
}

future模式,异步执行,结果处理抽象, 具体实现各个子类实现

  • 先看下异步响应之后的调用时序图

    异步响应时序图.png
  • 这里在发送完请求之后,还会调用再次调用RequestFuture的compose方法,为RequestFuture增加listener,这里的listener作用是调用各个ResponHandleAdapter实现类,也就是各个请求的实现类。这样就把解析响应交回各个子类。时序图为: 1.1.1是future模式,1.1.2是为ReuqestFuture添加listener以便回调各个子类实现的onSuccess

    时序图.png
  • 具体看代码
public class SendJoinGroupRequest extends AbstractRequestSend implements ResponseHandleAdapter {
    public void sendJoinGroupRequest(String s){
        send(s).compose(this);
    }

    @Override
    public void onSuccess(String s) {
        System.out.println("SendJoinGroupRequest执行结果: " + s);
    }

    public static void main(String[] args) {
        SendJoinGroupRequest sendJoinGroupRequest = new SendJoinGroupRequest();
        sendJoinGroupRequest.sendJoinGroupRequest("测试发送JoinGroup检测请求");
        // 写SendHeartbeatRequest 类似的
       /* SendHeartbeatRequest sendHeartbeatRequest = new SendHeartbeatRequest();
        sendHeartbeatRequest.sendHeartbeat("测试发送心跳检测请求");*/
    }
}

public abstract class AbstractRequestSend {

    /**
     * 模拟异步执行任务
     */
    ExecutorService executorService = Executors.newSingleThreadExecutor();

    public RequestFuture send(String s) {
        // 这里是Future模式
        RequestFuture requestFuture = new RequestFuture();
        executorService.execute(new AsyncExeRemoteInvoke(requestFuture, s));
        return requestFuture;
    }

    private static class AsyncExeRemoteInvoke implements Runnable {
        private RequestFuture requestFuture;
        private String s;

        public AsyncExeRemoteInvoke(RequestFuture requestFuture, String s) {
            this.requestFuture = requestFuture;
            this.s = s;
        }

        @Override
        public void run() {
            // 模拟远程调用耗时
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String res = s + "远程调用结束";
            requestFuture.onComplete(res);
        }
    }
}

public interface ResponseHandleAdapter {
    void onSuccess(String s);
}

public class RequestFuture {
    private List requestFutureListenerList;

    public RequestFuture() {
        this.requestFutureListenerList = new ArrayList<>();
    }

    public void onComplete(String s){
        // 假设只执行成功的方法
        fireSuccess(s);
    }

    private void fireSuccess(String s) {
        // 假设只执行成功的方法
        requestFutureListenerList.forEach(requestFutureListener -> requestFutureListener.onSuccess(s));
    }

    private void addListener(RequestFutureListener requestFutureListener){
        requestFutureListenerList.add(requestFutureListener);
    }

    /**
     * 相当于给RequestFuture addListener
     */
    public void compose(ResponseHandleAdapter responseHandleAdapter){
        addListener(new RequestFutureListener() {
            @Override
            public void onSuccess(String s) {
                // 交回给各个请求处理类处理
                responseHandleAdapter.onSuccess(s);
            }

            @Override
            public void onFail() {
                // 暂时不处理
            }
        });
    }


}

public interface RequestFutureListener {
     void onSuccess(String s);

     void onFail();
}
  • 如果需要实现链式调用,即调用了各个ResponseHandleAdapter 处理结果之后,还需要继续回调,那么可以修改
  1. RequestFuture的compose方法为
public RequestFuture compose(ResponseHandleAdapter responseHandleAdapter){
    // 这里实现将结果传播出去的关键代码,执行完各个responseHandleAdapter后,
    // 链式的执行调用compose方法,调用compose方法会添加一个listener
    final RequestFuture future = new RequestFuture();
    addListener(new RequestFutureListener() {
        @Override
        public void onSuccess(String s) {
            // 交回给各个请求处理类处理
            responseHandleAdapter.onSuccess(s, future);
        }

        @Override
        public void onFail() {
            // 暂时不处理
        }
    });
    return future;
}
  1. 修改ResponseHandleAdapter
public interface ResponseHandleAdapter {
    void onSuccess(String s, RequestFuture future);
}
  1. 修改SendJoinGroupRequest
public class SendJoinGroupRequest extends AbstractRequestSend implements ResponseHandleAdapter {
    public RequestFuture sendJoinGroupRequest(String s){
        return send(s).compose(this);
    }

    @Override
    public void onSuccess(String s, RequestFuture future) {
        System.out.println("SendJoinGroupRequest执行结果: " + s);
        //将结果传播出去
        future.onComplete(s);
    }

    public static void main(String[] args) {
        SendJoinGroupRequest sendJoinGroupRequest = new SendJoinGroupRequest();
        RequestFuture future = sendJoinGroupRequest.sendJoinGroupRequest("测试发送JoinGroup检测请求");
        future.addListener(new RequestFutureListener() {
            @Override
            public void onSuccess(String s) {
                System.out.println("我是SendJoinGroupRequest传播出来的,请求为:" + s);

            }

            @Override
            public void onFail() {

            }
        });

       /* SendHeartbeatRequest sendHeartbeatRequest = new SendHeartbeatRequest();
        RequestFuture future1 = sendHeartbeatRequest.sendHeartbeat("测试发送心跳检测请求");
        future1.addListener(new RequestFutureListener() {
            @Override
            public void onSuccess(String s) {
                System.out.println("我是SendHeartbeatRequest传播出来的,请求为:" + s);
            }

            @Override
            public void onFail() {

            }
        });*/
    }
}
  1. 修改执行执行结果为
SendJoinGroupRequest执行结果: 测试发送JoinGroup检测请求远程调用结束
我是SendJoinGroupRequest传播出来的,请求为:测试发送JoinGroup检测请求远程调用结束

反射,动态代理抽象

  • 这种是更高一级的抽象

版权声明:
作者:cc
链接:https://www.techfm.club/p/23772.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>