zoukankan      html  css  js  c++  java
  • Object Builder中的Locator究竟是不是采用Composite的模式之我见

    声明:

     前两天正好在看CAB的代码,看到ObjectBuilder,今天看到Terrylee的文章,

    想对此问题进行讨论,发表一下我的看法。

    概述:

    组合模式的作用是将几个单一的对象组成一个复杂的对象,

    复杂的对象又能组成更复杂的对象.

    而客户端并不需要知道其调用对象的细节,因为它们的接口是一致的.

    首先:

     我们来研究一下,客户端如何使用Locator的。

    Logging LibararyLocatorNameTypeFactoryBase中用到了定位器。

    locator中加入了一个ILifetimeContainer的对象。

     这里是他的注解:The use of a  IReadWriteLocator enables singletons for the types that support them.

    需要通过定位器来支持单模式。

     

    protected LocatorNameTypeFactoryBase(IConfigurationSource configurationSource)

                  
    {

                         
    this.configurationSource = configurationSource;

     

                         locator 
    = new Locator();

                         lifetimeContainer 
    = new LifetimeContainer();

                         locator.Add(
    typeof(ILifetimeContainer), lifetimeContainer);

                  }


     

    同时我们可以看到在LogWriteCustomFactory中有取得的代码

    public object CreateObject(IBuilderContext context, string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)

                  
    {

                         LogWriterStructureHolder structureHolder

                                
    = (LogWriterStructureHolder)LogWriterStructureHolderCustomFactory.Instance.CreateObject(context, name, configurationSource, reflectionCache);

     

                         LogWriterStructureHolderUpdater structureHolderUpdater 
    = new LogWriterStructureHolderUpdater(configurationSource);

                         LogWriter createdObject 
    = new LogWriter(structureHolder, structureHolderUpdater);

                         structureHolderUpdater.SetLogWriter(createdObject);

     

                         
    // add the writer to the locator, if necessary.

                         
    if (context.Locator != null)

                         
    {

                                ILifetimeContainer lifetime 
    = context.Locator.Get<ILifetimeContainer>(typeof(ILifetimeContainer), SearchMode.Local);

     

                                
    if (lifetime != null)

                                
    {

                             context.Locator.Add(
    new DependencyResolutionLocatorKey(typeof(LogWriter), name), createdObject);

                                       lifetime.Add(createdObject);

                                }


                         }


     

     

                         
    return createdObject;

                  }


    这里我们可以清楚地看到,

    Context.Locator.Add(new DependencyResolutionLocatorKey(typeof(LogWriter,name),createdObject);

    Lifetime.Add(createdObject);

     

    Locator可以由ILifeTimeContainer,DependencyResolutionLocatorKey组成,同样的ILifeTimeContainer可以添加对象。相对于复合(Composite)的设计模式,从LocatorIlifeTimeContainer来看都是由IEnumerable对象继承同时拥有。如果把Locator看作Composite,Compotent,他并不能实现内部简单对象的统一,查看DependecyResolutionLocatorKey的定义。

    如果说Locator的内部是由Composite模式组成见下图:

    我觉得不是很确切,Locator并不是由几个单一对象组合,而是直接从ReadWriteLocator中继承的。

    最主要,我觉得Locator本身不能满足Composite的最重要的特性,就是由多个简单对象组合成复杂的对象,接口保持一致。

     

  • 相关阅读:
    不能成为专业软件测试人员的10大理由
    不能成为专业软件测试人员的10大理由
    团队建设三境界
    团队建设三境界
    团队建设三境界
    界面测试总结
    界面测试总结
    codevs 1220 数字三角形
    codevs 1214 线段覆盖
    codevs 2181 田忌赛马
  • 原文地址:https://www.cnblogs.com/wanghualiang/p/349200.html
Copyright © 2011-2022 走看看