栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA Spring 简单策略工厂模式小案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA Spring 简单策略工厂模式小案例

        可以使用ApplicationContextHolder的getApplicationContext().getBean()方法加载类方法实现策略分配

直接上代码

1、定义返回类型,满足业务需求的对象。

        a)提取公共属性到父类

@Data
public class SchemaStatParserVO {

//定义具体的属性

}

        b)定义子类,定制化需求对象

public class ClickSchemaStatParserVO extends SchemaStatParserVO{

    //子类特色属性定义

}
public class MysqlSchemaStatParserVO extends SchemaStatParserVO{
    
//定义子类属性
}

2、定义公共类,定制公共逻辑接口。

        a)接口定义

public interface ISQLStatementParseService {

    SchemaStatParserVO SQLParser(String type);

}

        b)实现定义

@Service
public class HiveStatementParseServiceImpl implements ISQLStatementParseService {


    @Override
    public SchemaStatParserVO SQLParser(String type) {
        //定制化子类实现属性
        return null;
    }
@Service
public class MysqlStatementParseServiceImpl implements ISQLStatementParseService {


    @Override
    public SchemaStatParserVO SQLParser(String type) {
        //定制化子类实现属性
        return null;
    }

3、创建策略工厂类

        

import com.yezhongye.plus.cache.DatabaseType;
import com.yezhongye.plus.config.ApplicationContextHolder;
import com.yezhongye.plus.service.ISQLStatementParseService;
import com.yezhongye.plus.service.impl.ClickStatementParseServiceImpl;
import com.yezhongye.plus.service.impl.HiveStatementParseServiceImpl;
import com.yezhongye.plus.service.impl.MysqlStatementParseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class SQLStatementParseFactory {

    @Autowired
    private ApplicationContextHolder applicationContextHolder;

    public ISQLStatementParseService getSQLStatementParser(Integer databaseType){

        switch (databaseType){
            case 1:
                return applicationContextHolder.getApplicationContext().getBean(MysqlStatementParseServiceImpl.class);
            case 2:
                return applicationContextHolder.getApplicationContext().getBean(ClickStatementParseServiceImpl.class);
            case 3:
                return applicationContextHolder.getApplicationContext().getBean(HiveStatementParseServiceImpl.class);
            default:
                return null;
        }
    }

}

4、测试调用

        a)测试类创建

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@MapperScan("com.yezhongye.plus.mapper")
public class DemoApplicationTests {
	

}
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yezhongye.plus.DemoApplicationTests;
import com.yezhongye.plus.bean.ClickSchemaStatParserVO;
import com.yezhongye.plus.bean.Employee;
import com.yezhongye.plus.bean.SchemaStatParserDTO;
import com.yezhongye.plus.cache.DatabaseType;
import com.yezhongye.plus.factory.SQLStatementParseFactory;
import com.yezhongye.plus.mapper.EmployeeMapper;
import com.yezhongye.plus.service.EmployeeService;
import com.yezhongye.plus.service.ISQLStatementParseService;
import com.yezhongye.plus.utils.FastJsonUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;


public class EmployeeTest extends DemoApplicationTests {


    @Autowired
    private SQLStatementParseFactory sqlStatementParseFactory;


    @Test
    public void sqlParseTest(){
ISQLStatementParseService clickService =  sqlStatementParseFactory.getSQLStatementParser(1);

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1019829.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号