基础篇-动物管理系统

这是一个使用Java编程语言实现的动物管理系统,采用三层架构设计,包括Controller、Service和Dao层。系统通过Console控制台交互,支持狗狗和猪猪的管理,包括添加、删除、修改和查看操作。Dao层通过工厂模式和配置文件动态获取Dao实现,实现了 Dao 和 Service 的解耦。此外,系统利用泛型提高代码复用性,并使用反射和接口增强灵活性。
摘要由CSDN通过智能技术生成

动物管理系统

学习目的:

  • 将封装、接口、多态、反射、泛型多个知识点串通进行实现一个动物管理系统的小项目
    通过Console控制台进行键盘录入来操作内存中的数据,了解实体数据在业务中处理流程

功能:

程序架构:
在这里插入图片描述

三层架构 :Controller>Service>Dao

代码结构:

EntryApplication

// 方法的入口,此主页会将键盘录入的选择标记传入Controller层
public class AnimalEntry {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
    
        Scanner scanner = new Scanner(System.in);

        A:
        while (true) {
            System.out.println  ("------------------------欢迎来到动物管理系统------------------------");
            System.out.println("请输入您的选择: 1.狗狗🐕管理  2.猪猪🐖管理  3.退出");
            switch (scanner.nextInt()) {
                case 1:
                    new AnimalController().animalSystem(1);

                    break;
                case 2:
                    new AnimalController().animalSystem(2);
                    break;
                case 3:
                    System.out.println("--------管理系统关闭,下次再见--------");
                    System.exit(0);
                default:
                    System.out.println("你输入的选择有误,请重新录入");
                    break A;
            }
        }
    }
}

Controller

// 将控制层共性展示内容进行抽取,通过用户的录入数字类判断其类型
public class WelcomeString {

    static Scanner scanner = new Scanner(System.in);
    
    static public void welcomeStr(Integer selectType) {
        String typeStr = selectType == 1 ? "🐕狗狗" : "🐖猪猪";
        System.out.println("系统提示:正在加载......");
        System.out.println("----------------------欢迎来到 《" + typeStr + "》 管理系统----------------------");
        System.out.println("请输入您的选择: 1.添加" + typeStr + "  2.删除" + typeStr + "  3.修改" + typeStr + "  4.查看" + typeStr + "  5.退出");
}
// 任何CRUD操作都将先询问Dao层获取是否能访问的标记
public class AnimalController {

    private Boolean isNoExists(Integer id) throws NoSuchFieldException {
        Boolean result = animalServiceTemplate.selectById(id) == null;
        if (!result) {
            System.out.println("输入的Id已经存在,请重新输入");
        }
        return result;
    }

    private Boolean isExists(Integer id) throws NoSuchFieldException {
        Boolean result = animalServiceTemplate.selectById(id) == null;
        if (result) {
            System.out.println("输入的Id不存在存在,请重新输入");
        }
        return result;
    }
}

Service

// 使用泛型将实现将可以处理同属性实体类
public interface AnimalServiceTemplate<T> {

    T selectById(Integer id) throws NoSuchFieldException;

    T[] selectAll();

    Boolean insert(T t);

    Boolean update(Integer id,T t) throws NoSuchFieldException, IllegalAccessException;

    Boolean delete(Integer id);

	Integer getCount();
}

注意:使用泛型若进行强制转换需要instanceof保留字健壮性效验

DaoFactroy

// 使用工厂模式进行Dao与Service层的解耦,工厂将通过配置文件获取真实对象
public class DaoFactory {
    public static AnimalsDaoTemplate getReflectionDaoImpl(){
    
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("application.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String className = properties.getProperty("class");
        Object o = null;
        try {
             o = Class.forName(className).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (o instanceof AnimalsDaoImpl){
            return  (AnimalsDaoImpl) o;
        }

        if (o instanceof OtherAnimalsDaoImpl){
            return (OtherAnimalsDaoImpl)o;
        }
        return null;
    }

}

注意:配置资源需要在src目录下

class=com.xy2.dao.impl.AnimalsDaoImpl
# 或使用class=com.xy2.dao.impl.OtherAnimalsDaoImpl

利用类加载器的I/O流读功能取src下配置文件,并通过Class类将String类型内容全包名的对象进行实例化,得到真实Dao

Dao

// 数据访问层通用接口模板
public interface AnimalsDaoTemplate<T> {

    Object[] select();

    T selectById(int id) throws NoSuchFieldException;

    Integer selectCount();

    Boolean insert(T t);

    Boolean update(Integer id,T t) throws NoSuchFieldException, IllegalAccessException;

    Boolean delete(int id);

}

注意:使用泛型若进行强制转换需要instanceof保留字健壮性效验

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值