二十三種設計模式-原型模式

原型模式

原型模式屬於對象的創建模式。通過給出一個原型對象來指明所有創建的對象的類型,然後用複製這個原型對象的辦法創建出更多同類型的對象。這就是原型模式的用意。

原型模式

這種形式涉及到三個角色:

  1. 客戶(Client)角色:客戶類提出創建對象的請求。

  2. 抽象原型(Prototype)角色:這是一個抽象角色,通常由一個Java接口或Java抽象類實現。此角色給出所有的具體原型類所需的接口。

  3. 具體原型(Concrete Prototype)角色:被複制的對象。此角色需要實現抽象的原型角色所要求的接口。

原型模式的優點
  原型模式允許在運行時動態改變具體的實現類型。原型模式可以在運行期間,由客戶來註冊符合原型接口的實現類型,也可以動態地改變具體的實現類型,看起來接口沒有任何變化,但其實運行的已經是另外一個類實例了。因爲克隆一個原型就類似於實例化一個類。

原型模式的缺點
  原型模式最主要的缺點是每一個類都必須配備一個克隆方法。配備克隆方法需要對類的功能進行通盤考慮,這對於全新的類來說不是很難,而對於已經有的類不一定很容易,特別是當一個類引用不支持序列化的間接對象,或者引用含有循環結構的時候。

原型模式即在當前類的基礎上創建出一個新的類,說到底就是怎麼克隆出一個對象,可以通過淺拷貝和深拷貝方式。

案例

1、測試demo

/**
 * @Author: jiangcw
 * @Date: 2019-6-29 下午 3:24
 * @Version 1.0
 *
 * 原型模式:創建型模式
 *
 * java中天生支持原型模式,通過實現Cloneable接口,重寫clone方法,實現拷貝
 * java中的clone方法默認的拷貝是淺拷貝,如果想實現深拷貝,需要重寫clone方法。
 *
 * 注:使用原型模式可以將當前對象的狀態複製到新創建的對象上。
 */
public class PrototypeBase {

    public static void main(String[] args) throws CloneNotSupportedException {
        M m = new M();
        m.elementA = new ElementA();
        m.elementB = new ElementB();
        M cloneObject = (M)m.clone();
        System.out.println(m.elementA == cloneObject.elementA);
        System.out.println(m.elementB == cloneObject.elementB);
        System.out.println(m == cloneObject);
        ElementA elementA = new ElementA();
        ElementB elementB = new ElementB();
        elementB.clone();
    }

    static class M implements Cloneable {
        private ElementA elementA;

        private ElementB elementB;

        @Override
        protected Object clone() throws CloneNotSupportedException {
            M m =  (M)super.clone();
            m.elementB = new ElementB();
            return m;
        }
    }

    static class ElementA implements Cloneable {

        String a;

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return new ElementA();
        }
    }

    static class ElementB implements Cloneable {
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
}

2、spring的prototype和singleton

在spring中一個bean可以是單例的,scope定義爲singleton即可。也可以定義爲原型模式,此時bean就是多例了,在需要使用相同的類型的bean的定義,都是重新創建一個狀態相同的bean。

關鍵類:AbstractBeanFactory,關鍵方法:doGetBean

protected <T> T doGetBean(
			final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
			throws BeansException {

		final String beanName = transformedBeanName(name);
		Object bean;

		// Eagerly check singleton cache for manually registered singletons.
		Object sharedInstance = getSingleton(beanName);
		if (sharedInstance != null && args == null) {
			if (logger.isDebugEnabled()) {
				if (isSingletonCurrentlyInCreation(beanName)) {
					logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
							"' that is not fully initialized yet - a consequence of a circular reference");
				}
				else {
					logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
				}
			}
			bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
		}

		else {
			// Fail if we're already creating this bean instance:
			// We're assumably within a circular reference.
			if (isPrototypeCurrentlyInCreation(beanName)) {
				throw new BeanCurrentlyInCreationException(beanName);
			}

			// Check if bean definition exists in this factory.
			BeanFactory parentBeanFactory = getParentBeanFactory();
			if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
				// Not found -> check parent.
				String nameToLookup = originalBeanName(name);
				if (args != null) {
					// Delegation to parent with explicit args.
					return (T) parentBeanFactory.getBean(nameToLookup, args);
				}
				else {
					// No args -> delegate to standard getBean method.
					return parentBeanFactory.getBean(nameToLookup, requiredType);
				}
			}

			if (!typeCheckOnly) {
				markBeanAsCreated(beanName);
			}

			try {
				final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
				checkMergedBeanDefinition(mbd, beanName, args);

				// Guarantee initialization of beans that the current bean depends on.
				String[] dependsOn = mbd.getDependsOn();
				if (dependsOn != null) {
					for (String dep : dependsOn) {
						if (isDependent(beanName, dep)) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
						}
						registerDependentBean(dep, beanName);
						try {
							getBean(dep);
						}
						catch (NoSuchBeanDefinitionException ex) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
						}
					}
				}

				// Create bean instance.
				// 如果是單例走下面的邏輯,保證一個類的實例是唯一的
				if (mbd.isSingleton()) {
					sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
						@Override
						public Object getObject() throws BeansException {
							try {
								return createBean(beanName, mbd, args);
							}
							catch (BeansException ex) {
								// Explicitly remove instance from singleton cache: It might have been put there
								// eagerly by the creation process, to allow for circular reference resolution.
								// Also remove any beans that received a temporary reference to the bean.
								destroySingleton(beanName);
								throw ex;
							}
						}
					});
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
				}
				// 如果是原型模式,會採用深度拷貝的方式,在注入實例的地方都會重新創建一個類的實例。
				else if (mbd.isPrototype()) {
					// It's a prototype -> create a new instance.
					Object prototypeInstance = null;
					try {
						beforePrototypeCreation(beanName);
						prototypeInstance = createBean(beanName, mbd, args);
					}
					finally {
						afterPrototypeCreation(beanName);
					}
					bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
				}

				else {
					String scopeName = mbd.getScope();
					final Scope scope = this.scopes.get(scopeName);
					if (scope == null) {
						throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
					}
					try {
						Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
							@Override
							public Object getObject() throws BeansException {
								beforePrototypeCreation(beanName);
								try {
									return createBean(beanName, mbd, args);
								}
								finally {
									afterPrototypeCreation(beanName);
								}
							}
						});
						bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
					}
					catch (IllegalStateException ex) {
						throw new BeanCreationException(beanName,
								"Scope '" + scopeName + "' is not active for the current thread; consider " +
								"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
								ex);
					}
				}
			}
			catch (BeansException ex) {
				cleanupAfterBeanCreationFailure(beanName);
				throw ex;
			}
		}

		// Check if required type matches the type of the actual bean instance.
		if (requiredType != null && bean != null && !requiredType.isInstance(bean)) {
			try {
				return getTypeConverter().convertIfNecessary(bean, requiredType);
			}
			catch (TypeMismatchException ex) {
				if (logger.isDebugEnabled()) {
					logger.debug("Failed to convert bean '" + name + "' to required type '" +
							ClassUtils.getQualifiedName(requiredType) + "'", ex);
				}
				throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
			}
		}
		return (T) bean;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章