Differences between ibuilderpolicy and ibuilderstrategy in objectbuilder

Source: Internet
Author: User

Frankfei at 2007/07/31


I may know something about objectbuilder. The strategy mode is used in it, and the gof has the following sentence: Policy, also known as policy, the problem arises. In objectbuilder, what is the difference between ibuilderpolicy and ibuilderstrategy? Why does the system introduce these two concepts at the same time?

Let's take a look at the class builder:


Public   Class Builder: builderbase < Builderstage >


Public Builder ()

: This ( Null )



Public Builder (ibuilderconfigurator < Builderstage > Aggregator)


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 (Aggregator ! =   Null )

Configurator. applyconfiguration ( This );




From aboveCodeWe can see that when we use new Builder (), all ibuilderstrategy will be automatically added to strategylist (strategies), that is, when we instantiate a builder, all the strategies (Strategy) it has been added to the strategylist. That is to say, the use of strategy is handled by objectbuilder and does not need to beProgramUser intervention. There are nine strategies in objectbuilder. Every time a program user does not need to use all of them, only one or more of them may be used. How can this problem be controlled? In this case, objectbuilder introduces the policy.

There are many specific strategies in the strategies directory of objectbuilder. You will find that basically every specific strategy corresponds to a policy. For example, typemappingstrategy corresponds to typemappingpolicy and singletonstrategy corresponds to singletonpolicy, propertysetterstrategy corresponds to propertysetterpolicy.

Objectbuilder calls the dobuildup method when buildup is an object, as follows:

Public   Class Builderbase < Tstageenum > : Ibuilder < Tstageenum >


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;



From the code above, we can see that in dobuildup, objectbuilder creates a specific strategy from the first object in the responsibility chain. When creating a specific strategy, the specific strategy contains a reference to the policy. If the corresponding policy is not empty, the corresponding policy is processed. See the following code:

Public   Class Typemappingstrategy: builderstrategy


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 );



Therefore, when buildup is an object for the first time, objectbuilder will execute all the strategies on the responsibility chain in sequence, check whether there is a corresponding policy in strategy, and then perform corresponding processing. At this point, we can further understand the application of the responsibility chain model.

Here, I think you should have some knowledge about strategy and policy in objectbuilder. The above is just my personal understanding. Please advise if it is inappropriate!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.