Checks already instantiated singletons and also allows for an early + * reference to a currently created singleton (resolving a circular reference). + * @param beanName the name of the bean to look for + * @param allowEarlyReference whether early references should be created or not + * @return the registered singleton object, or {@code null} if none found + */ + protected Object getSingleton(String beanName, boolean allowEarlyReference) { + Object singletonObject = this.singletonObjects.get(beanName); + if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { + synchronized (this.singletonObjects) { + singletonObject = this.earlySingletonObjects.get(beanName); + if (singletonObject == null && allowEarlyReference) { + ObjectFactory> singletonFactory = this.singletonFactories.get(beanName); + if (singletonFactory != null) { + singletonObject = singletonFactory.getObject(); + this.earlySingletonObjects.put(beanName, singletonObject); + this.singletonFactories.remove(beanName); + } + } + } + } + return (singletonObject != NULL_OBJECT ? singletonObject : null); + } + + /** + * Return whether the specified singleton bean is currently in creation + * (within the entire factory). + * @param beanName the name of the bean + */ + public boolean isSingletonCurrentlyInCreation(String beanName) { + return this.singletonsCurrentlyInCreation.contains(beanName); + } + +} diff --git a/singleton/src/main/java/com/iluwatar/singleton/practice/SingletonInterface.java b/singleton/src/main/java/com/iluwatar/singleton/practice/SingletonInterface.java new file mode 100644 index 000000000000..dd21e8998d28 --- /dev/null +++ b/singleton/src/main/java/com/iluwatar/singleton/practice/SingletonInterface.java @@ -0,0 +1,12 @@ +package com.iluwatar.singleton.practice; + +/** + * 描述: + * + * @author biguodong + * Create time 2018-10-17 下午8:13 + **/ +public interface SingletonInterface { + + String doSomething(); +} diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/practice/AbstractStealingMethod.java b/template-method/src/main/java/com/iluwatar/templatemethod/practice/AbstractStealingMethod.java new file mode 100644 index 000000000000..2a9d54338580 --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/templatemethod/practice/AbstractStealingMethod.java @@ -0,0 +1,48 @@ +package com.iluwatar.templatemethod.practice; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 描述: 抽象模板类 + * + * @author biguodong + * Create time 2018-10-31 下午4:18 + **/ +public abstract class AbstractStealingMethod { + + private static final Logger logger = LoggerFactory.getLogger(AbstractStealingMethod.class); + + /** + * 挑选目标 + * @return + */ + protected abstract String pickedTarget(); + + /** + * 迷惑目标 + * @param target + */ + protected abstract void confusedTarget(String target); + + + /** + * 偷取 + * @param target + */ + protected abstract void stealTheItem(String target); + + /** + * 模板方法 + * 执行顺序 + * 1. 挑选目标 + * 2. 迷惑目标 + * 3. 偷取目标 + */ + public final void steal(){ + String target = pickedTarget(); + logger.info("选中目标 {}.", target); + confusedTarget(target); + stealTheItem(target); + } +} diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/practice/App.java b/template-method/src/main/java/com/iluwatar/templatemethod/practice/App.java new file mode 100644 index 000000000000..aabdfbcda0b1 --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/templatemethod/practice/App.java @@ -0,0 +1,20 @@ +package com.iluwatar.templatemethod.practice; + +/** + * 描述:模板方法使用类定义骨架,子类为空白部分提供实现。 + * + * @author biguodong + * Create time 2018-10-31 下午4:49 + **/ +public class App { + + public static void main(String[] args) { + HalfingThief halfingThief = new HalfingThief(new HitAndRunStealingMethod()); + + halfingThief.steal(); + + halfingThief.changeMethod(new SubtleStealingMethod()); + + halfingThief.steal(); + } +} diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/practice/HalfingThief.java b/template-method/src/main/java/com/iluwatar/templatemethod/practice/HalfingThief.java new file mode 100644 index 000000000000..765136430edb --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/templatemethod/practice/HalfingThief.java @@ -0,0 +1,24 @@ +package com.iluwatar.templatemethod.practice; + +/** + * 描述:半身人小偷使用 {@link AbstractStealingMethod} 偷东西 + * + * @author biguodong + * Create time 2018-10-31 下午4:26 + **/ +public class HalfingThief { + + private AbstractStealingMethod stealingMethod; + + public HalfingThief(AbstractStealingMethod stealingMethod) { + this.stealingMethod = stealingMethod; + } + + public void steal(){ + stealingMethod.steal(); + } + + public void changeMethod(AbstractStealingMethod method){ + this.stealingMethod = method; + } +} diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/practice/HitAndRunStealingMethod.java b/template-method/src/main/java/com/iluwatar/templatemethod/practice/HitAndRunStealingMethod.java new file mode 100644 index 000000000000..3a9f7ad61254 --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/templatemethod/practice/HitAndRunStealingMethod.java @@ -0,0 +1,45 @@ +package com.iluwatar.templatemethod.practice; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 描述: 直接干偷取方法 实现 of {@link AbstractStealingMethod} + * + * @author biguodong + * Create time 2018-10-31 下午4:25 + **/ +public class HitAndRunStealingMethod extends AbstractStealingMethod{ + + private static final Logger logger = LoggerFactory.getLogger(HitAndRunStealingMethod.class); + + /** + * 挑选目标 + * + * @return + */ + @Override + protected String pickedTarget() { + return "老妖精女人"; + } + + /** + * 迷惑目标 + * + * @param target + */ + @Override + protected void confusedTarget(String target) { + logger.info("从后面悄悄靠近{}", target); + } + + /** + * 窃取商品 + * + * @param target + */ + @Override + protected void stealTheItem(String target) { + logger.info("拿起手提包快速逃跑~"); + } +} diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/practice/SubtleStealingMethod.java b/template-method/src/main/java/com/iluwatar/templatemethod/practice/SubtleStealingMethod.java new file mode 100644 index 000000000000..1f5d88ca8c43 --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/templatemethod/practice/SubtleStealingMethod.java @@ -0,0 +1,44 @@ +package com.iluwatar.templatemethod.practice; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 描述:微妙的偷取方法 实现 of {@link AbstractStealingMethod} + * + * @author biguodong + * Create time 2018-10-31 下午4:23 + **/ +public class SubtleStealingMethod extends AbstractStealingMethod{ + + private static final Logger logger = LoggerFactory.getLogger(SubtleStealingMethod.class); + /** + * 挑选目标 + * + * @return + */ + @Override + protected String pickedTarget() { + return "商店老板"; + } + + /** + * 迷惑目标 + * + * @param target + */ + @Override + protected void confusedTarget(String target) { + logger.info("泪牛满面的接近{},并抱住他", target); + } + + /** + * 窃取商品 + * + * @param target + */ + @Override + protected void stealTheItem(String target) { + logger.info("在靠近的同时悄悄摸取{}的钱包~", target); + } +}