惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
U
Unit 42
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
IT之家
IT之家
MyScale Blog
MyScale Blog
V
Visual Studio Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
InfoQ
博客园 - 司徒正美

博客园 - 有点懒惰的大青年

shell脚本变量记录 Python格式化字符串 Python中的浅拷贝和深拷贝 CompletableFuture.runAsync来进行异步任务和ThreadPoolExecutor来执行有什么区别? 用一个实际业务场景演示ON和WHERE的正确使用方式 idea中查看源码 异或运算 使用Hutool对时间的花式操作 合并K个升序列表 比较器 spring boot中使用RedissonClient实现分布式锁 idea同时启动application,启用不同端口 @PostConstruct用法 ApplicationContext 事件发布与监听机制详解 java自带命令jps介绍 关于maven中标签的说明 mysql的跨库查询 linux命令ll显示结果的含义 Files类的使用 java环境变量设置 java值传递和引用传递 kafka的集群与可靠性 kafka的消费全流程 关于kafka 达梦数据库执行计划介绍 关于MQ 用位运算实现加减乘除(3)
枚举类的设计模式
有点懒惰的大青年 · 2026-03-08 · via 博客园 - 有点懒惰的大青年

一、TimeUnit设计模式

当像时间种类这样的,自身属性很少,类似这种时间单位的,又有很多种不同类型的,看到TimeUnit的实现方式,可以参考来实现。下面是我自己的模拟代码,模拟jdk源码中的TimeUnit设计模式:

AnimalCategory,动物类型枚举类:

package com.test.enums;

import lombok.extern.slf4j.Slf4j;

/**
 * 动物类型枚举类
 */
@Slf4j
public enum AnimalCategory {

    LION {
        public void bellow(String msg) { log.info(this.name() + "的低吼:" + msg); }
        int runningSpeed() { return 60; }
        String getFamily() { return "猫科"; }
        String getEater() { return "肉食"; }
    },

    TIGER {
        public void bellow(String msg) { log.info(this.name() + "的低吼:" + msg); }
        int runningSpeed() { return 70; }
        String getFamily() { return "猫科"; }
        String getEater() { return "肉食"; }
    },

    WOLF {
        public void bellow(String msg) { log.info(this.name() + "的低吼:" + msg); }
        int runningSpeed() { return 50; }
        String getFamily() { return "犬科"; }
        String getEater() { return "杂食"; }
    };

    /**
     * 科属,猫科还是犬科等
     */
    abstract String getFamily();

    /**
     * 食性,肉食或素食或杂食
     */
    abstract String getEater();

    /**
     * 动物的吼叫声
     */
    public void bellow(String msg) {
        throw new AbstractMethodError();
    }

    /**
     * 动物的奔跑时速
     */
    abstract int runningSpeed();
}

TestAnimalsCategory,测试不同的动物物种的表现:

package com.test.enums;

import lombok.extern.slf4j.Slf4j;

/**
 * 测试不同的动物物种的表现
 */
@Slf4j
public class TestAnimalsCategory {
    public static void main(String[] args) {
        log.info("{}的奔跑速度为{}km/h,是{}动物,食性为{}",
                AnimalCategory.LION.name(), AnimalCategory.LION.runningSpeed(),
                AnimalCategory.LION.getFamily(), AnimalCategory.LION.getEater());
        AnimalCategory.LION.bellow("你在哪?");


        log.info("{}的奔跑速度为{}km/h,是{}动物,食性为{}",
                AnimalCategory.TIGER.name(), AnimalCategory.TIGER.runningSpeed(),
                AnimalCategory.TIGER.getFamily(), AnimalCategory.TIGER.getEater());
        AnimalCategory.TIGER.bellow("她在哪?");

        log.info("{}的奔跑速度为{}km/h,是{}动物,食性为{}",
                AnimalCategory.WOLF.name(), AnimalCategory.WOLF.runningSpeed(),
                AnimalCategory.WOLF.getFamily(), AnimalCategory.WOLF.getEater());
        AnimalCategory.WOLF.bellow("月色不错?");
    }
}

打印结果:

13:30:51.062 [main] INFO com.test.enums.TestAnimalsCategory - LION的奔跑速度为60km/h,是猫科动物,食性为肉食
13:30:51.066 [main] INFO com.test.enums.AnimalCategory - LION的低吼:你在哪?
13:30:51.066 [main] INFO com.test.enums.TestAnimalsCategory - TIGER的奔跑速度为70km/h,是猫科动物,食性为肉食
13:30:51.066 [main] INFO com.test.enums.AnimalCategory - TIGER的低吼:她在哪?
13:30:51.066 [main] INFO com.test.enums.TestAnimalsCategory - WOLF的奔跑速度为50km/h,是犬科动物,食性为杂食
13:30:51.066 [main] INFO com.test.enums.AnimalCategory - WOLF的低吼:月色不错?

--