GithubHelp home page GithubHelp logo

xinrui-component's Introduction

xinrui-component

代码解构利器-SWAK

背景

随着业务的稳步发展,不断有新的业务模式接入。
最初,我们可能在代码中添加一些开关来适配多种不同的实现。但这种方式好比给一个水管打洞,最终会发现到处”漏水“。
原有的系统在演进过程中,面临着诸多挑战。

if(A类型) {
    if(A1类型) {
        doSomething1();
    }else if(A2类型) {
        doSomething2();
    }
} else if(B类型) {
    doSomething3();
} else if(C类型) {
    if(C1类型) {
        doSomething4();
    }else if(C2类型) {
        doSomething5();
    }
}

组件优势

代码解构利器,核心聚焦与上述描述的业务场景,即一个业务动作可能会多种实现的场景。 在分离业务实现的同时,给了业务系统无限的扩展能力,同时大大较低了代码维护的复杂性和人力成本。

实现原理

ImportBeanDefinitionRegistrar、动态代理、策略模式

Get Started

从开发者角度来看,使用该插件非常简单,下面是全部的集成和使用的步骤,如果是仅仅开发,只需关注3、4两部分。
集成使用步骤描述如下:

  1. 引入maven包
<dependency>
    <groupId>com.guoke</groupId>
    <artifactId>star-common-swak</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>
  1. 在启动类,或者Configuration类上加上扫描注解,即要到哪些目录扫描@SwakInterface注解的扩展点接口
import com.gkht.component.swak.annotation.SwakScan;

@Configuration
@ComponentScan(basePackages = {"com.guoke.star.saas.order"}) 
@SwakScan(basePackages={"com.guoke.star.saas.order"}) // 这里需要定制自己的扫描路径
public class SwakConfig {
    // pass
}
  1. 在你需要扩展的接口类上添加注解
@SwakInterface   //生成代理类
public interface TestService {
    String sayHello(BaseParam param);
}
  1. 随便写自己的多个实现
@SwakBiz(bizcode="star", tags="peijia-mode")
public class TestServiceImpl implements TestService {
    @Override
    public String sayHello(String param) {
        return "star say: hello world," + param;
    }
}
@SwakBiz(bizcode="guoke", tags="maidi-mode")
public class BTestServiceImpl implements TestService {
    @Override
    public String sayHello(String param) {
        return "guoke say: hello world," + param;
    }
}
@SwakBiz   //默认实现,一个标准化流程两个扩展点,一个业务模型,可能一个扩展点走标准化流程,一个走扩展逻辑,所以每个扩展点需有默认实现
public class BTestServiceImpl implements TestService {
    @Override
    public String sayHello(String param) {
        return "normal say: hello world," + param;
    }
}
@RestController
public class SwakHelloWorldController {

    @Autowired //扩展点使用与正常注入 没有区别
    private TestService helloWorldSwakBiz;

    @RequestMapping("/hello")
    public String helloWorld(HelloWorldForm form ){
        return helloWorldSwakBiz.helloworld(form.getMessage());
    }
}

that's all

xinrui-component's People

Contributors

wolfcar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.