V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
zhix
V2EX  ›  Java

基于 enum 的单例模式是不是一种反模式?

  •  
  •   zhix · Oct 16, 2017 · 4114 views
    This topic created in 3120 days ago, the information mentioned may be changed or developed.

    我一般习惯用 enum 来实现单例

    enum Singleon {
    	
        INSTANCE;
    }
    

    同样会使用 enum 来实现工具类

    enum Utils {
    
    	;
    }
    

    这样做算不算一种反模式?

    5 replies    2017-10-19 17:42:22 +08:00
    johnniang
        1
    johnniang  
       Oct 16, 2017 via Android
    你应该去反编译一下代码看看 enum 的真身
    zonghua
        2
    zonghua  
       Oct 16, 2017 via iPhone
    枚举没了面向对象的一些特性
    flyingghost
        3
    flyingghost  
       Oct 16, 2017
    无论技术上可不可行,我觉得和设计初衷、和字面意思、和直觉相违背的 geek 用法都是以可读性 /可维护性为代价的奇技淫巧。
    yidinghe
        5
    yidinghe  
       Oct 19, 2017
    反正 enum 不是这么用的,所以是反模式。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2507 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 08:40 · PVG 16:40 · LAX 01:40 · JFK 04:40
    ♥ Do have faith in what you're doing.