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

推荐订阅源

V2EX - 技术
V2EX - 技术
博客园 - 司徒正美
F
Fortinet All Blogs
D
Docker
aimingoo的专栏
aimingoo的专栏
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
U
Unit 42
The Register - Security
The Register - Security
Martin Fowler
Martin Fowler
IT之家
IT之家
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
T
Tenable Blog
S
Security Affairs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
W
WeLiveSecurity
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
TaoSecurity Blog
TaoSecurity Blog
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
雷峰网
雷峰网
Forbes - Security
Forbes - Security
Recent Announcements
Recent Announcements
N
News | PayPal Newsroom
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
P
Palo Alto Networks Blog
C
Cybersecurity and Infrastructure Security Agency CISA
S
Schneier on Security
Attack and Defense Labs
Attack and Defense Labs
Latest news
Latest news
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
Last Week in AI
Last Week in AI
Scott Helme
Scott Helme
A
Arctic Wolf
L
LINUX DO - 最新话题
A
About on SuperTechFans
K
Kaspersky official blog
博客园 - Franky

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

用一个实际业务场景演示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的低吼:月色不错?

--