ObjectBuilder中IBuilderPolicy和IBuilderStrategy之区别
Posted on 2007-07-31 17:01 FrankFei 阅读(2182) 评论(4) 编辑 收藏 举报FrankFei at 2007/07/31
对ObjectBuilder有所了解的可能知道,里面使用了Strategy模式,而在GOF中有这样一句:策略又称做政策(Policy),那问题就来了,在ObjectBuilder中,IBuilderPolicy和IBuilderStrategy到底有什么区别呢?系统为什么会同时引入这两个概念呢?
我们先来看一下类Builder:
{
public Builder()
: this(null)
{
}
public Builder(IBuilderConfigurator<BuilderStage> configurator)
{
Strategies.AddNew<TypeMappingStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<SingletonStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<ConstructorReflectionStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<PropertyReflectionStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<MethodReflectionStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<CreationStrategy>(BuilderStage.Creation);
Strategies.AddNew<PropertySetterStrategy>(BuilderStage.Initialization);
Strategies.AddNew<MethodExecutionStrategy>(BuilderStage.Initialization);
Strategies.AddNew<BuilderAwareStrategy>(BuilderStage.PostInitialization);
Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
if (configurator != null)
configurator.ApplyConfiguration(this);
}
}
从上面的代码我们可以看出,当我们new Builder()时,会自动把所有的IBuilderStrategy加入到StrategyList(Strategies)中,即当我们实例化一个Builder时,所有的策略(Strategy)已经被加入到StrategyList中了,也就是说,对于策略(Strategy)的使用,是由ObjectBuilder本身处理的,不需要程序使用者干预。那ObjectBuilder中共有九个策略(Strategy),程序使用者每一次不一定需要全部使用,可能只使用其中的一个或多个,那该如何控制呢?这时ObjectBuilder就引入了Policy。
在ObjectBuilder的Strategies目录下有很多具体策略(Strategy),仔细查看你会发现,基本上每个具体的Strategy都对应一个Policy,比如TypeMappingStrategy对应TypeMappingPolicy,SingletonStrategy对应SingletonPolicy,PropertySetterStrategy对应PropertySetterPolicy等。
ObjectBuilder在BuildUp一个对象时,会调用DoBuildUp这个方法,如下:
{
private object DoBuildUp(IReadWriteLocator locator, Type typeToBuild, string idToBuild, object existing,
PolicyList[] transientPolicies)
{
IBuilderStrategyChain chain = strategies.MakeStrategyChain();
ThrowIfNoStrategiesInChain(chain);
IBuilderContext context = MakeContext(chain, locator, transientPolicies);
IBuilderTracePolicy trace = context.Policies.Get<IBuilderTracePolicy>(null, null);
if (trace != null)
trace.Trace(Properties.Resources.BuildUpStarting, typeToBuild, idToBuild ?? "(null)");
object result = chain.Head.BuildUp(context, typeToBuild, existing, idToBuild);
if (trace != null)
trace.Trace(Properties.Resources.BuildUpFinished, typeToBuild, idToBuild ?? "(null)");
return result;
}
}
从上面的代码中我们可以看出,ObjectBuilder在DoBuildUp时,是从责任链上的第一个对象开始创建具体策略(Strategy),在创建具体策略(Strategy)时,具体策略(Strategy)中包含了对Policy的引用,如果对应的Policy不为空,则处理相应的Policy,看如下代码:
{
public override object BuildUp(IBuilderContext context, Type t, object existing, string id)
{
DependencyResolutionLocatorKey result = new DependencyResolutionLocatorKey(t, id);
ITypeMappingPolicy policy = context.Policies.Get<ITypeMappingPolicy>(t, id);
if (policy != null)
{
result = policy.Map(result);
TraceBuildUp(context, t, id, Properties.Resources.TypeMapped, result.Type, result.ID ?? "(null)");
Guard.TypeIsAssignableFromType(t, result.Type, t);
}
return base.BuildUp(context, result.Type, existing, result.ID);
}
}
所以在第一次BuildUp一个对象时,ObjectBuilder会依次执行完责任链上的所有策略(Strategy),同时查看Strategy有没有对应的Policy,然后做相应的处理。此时我们还可以再加深理解一下责任链模式的应用。
写到这里,我想你应该对ObjectBuilder中的Strategy和Policy有所了解了,以上只是我个人理解,不当之处请指教!