Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

第1回 チキチキ『( ゜ェ゜)・;'.、ゴフッ』 - シングルトンパターン(Java)

4.678 Aufrufe

Veröffentlicht am

Veröffentlicht in: Technologie
  • Sex in your area is here: ❤❤❤ http://bit.ly/2F7hN3u ❤❤❤
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • Dating for everyone is here: ❶❶❶ http://bit.ly/2F7hN3u ❶❶❶
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

第1回 チキチキ『( ゜ェ゜)・;'.、ゴフッ』 - シングルトンパターン(Java)

  1. 1. (Java)@j5ik2o
  2. 2. @j5ik2o Scala 6 5 Java Scala DDD( ) (4,5,6 )
  3. 3. Singleton ) 1GoF
  4. 4. newHoge a = new Hoge(); // 1Hoge b = new Hoge(); // 2 NG 1
  5. 5. public class Cache { // public static final Cache INSTANCE = newCache(); private Cache() { } // new public void put(String key, Object value){ ... } public Object get(String key) { ... }}Cache.INSTANCE.put(key, value)Object value = Cache.INSTANCE.get(key)
  6. 6. public class Cache { private static final Cache INSTANCE = new Cache(); private Cache() { } public static Cache getInstance() { return INSTNACE; } public void put(String key, Object value){ ... } public Object get(String key) { ... }}Cache.getInstance().put(key, value)Object value = Cache.getInstance().get(key)
  7. 7. public class Cache { private static ThreadLocal<Cache> threadLocal =new ThreadLocal<Cache>() { @Override protected Cache initialValue() { return new Cache(); } }; public static Cache getInstnace() { return threadLocal.get(); } public Object get(String key) { /* ... */ } public void put(String key, Object value) { /* ... */ }}
  8. 8. 1:Effective Java 3public class CacheClient { // public void process() { getCache().put(...); // ... } // Cache Cache getCache() { return Cache.getInstance(); // ↑ // private // new Cache() { ... } // ... }}
  9. 9. public class SingletonCache implements Cache { private static SingletonCache instance; public static SingletonCache getInstnace() { return instance; } @Override public Object get(String key) { /* ... */ } @Override public void put(String key, Object value) { /* ... */ }}public class CacheClient { // ... Cache getCache() { return new Cache(){ /* */ }; }}
  10. 10. 2: P40-41 singleton
  11. 11. 3: readObject w( o )w!!!Serializable transient ... readResolve (GC) ... private Object readResolve() throws ObjectStreamException { return INSTANCE; // }
  12. 12. enumpublic enum Cache { INSTANCE; public vod put(String key, Objectvalue) { /* ... */ } public Object get(String key) {/* ...*/ }}Cache.INSTANCE.put(key, value);
  13. 13. enum Jad ...public final class Cache extends Enum<Cache>{ public static final EnumSingleton INSTANCE; private static final Cache $VALUES[]; static { INSTANCE = newEnumSingleton("INSTANCE", 0); $VALUES = (new Cache[] { INSTANCE }); } private Cache(String s, int i){ super(s, i); } // values, valueOf ...}
  14. 14. (´ ∀ )public class Singleton { private static Singleton instance; public synchronized static SingletongetInstance() { if (instance == null) { instance = new Singleton(); } return instance; }}
  15. 15. ( д ) ?!public class Singleton { // private static class InstanceHolder { private static final Singleton INSTANCE = newSingleton(); } public static Singleton getInstance() { return InstanceHolder.INSTANCE; }}
  16. 16. ..(llil!´Д`lil)n{…??public class Singleton { private static volatile Singleton instance public static Singleton getInstance() { Singleton result = instance; // (1) if (result == null) { // (2) => synchronized (Singleton.class) { // (3) : result = instance; // (4) if (result == null) { // (5) result = instance = new Singleton(); // (6) } } // : } return result;}
  17. 17. getInstnace ” ”
  18. 18. Scala objectobject Cache { def put(key:String, value:Any):Unit= ... def get(key:String):Any = ...}Cache.put(key, value)val value = Cache.get(key)
  19. 19. object Singletonpublic final class Cache$ implements ScalaObject{ public static final Cache$ MODULE$ = newCache$(); private Cache$() { /* ... */ } public void put(String key, Object value) { /* ...*/ } public Object get(String key) { /* ... */ }}Cache$.MODULE$.put(key, value)Object value = Cache$.MODULE$.get(key)

×