Frankfei at 2007/08/01
Objectbuilder provides many policies and can even be expanded as needed. How can we use the provided policies correctly? This articleArticleI will first introduce singletonpolicy.
In our actual project development, we often encounter the situation of creating the same class multiple times. If we use the general method to create a new class, every time we create a new class, an instance is generated, and there is no problem with small systems, but when the system is large, you should think carefully.
Let's take a look at the following example. myconcreteclass is an entity class, and using Microsoft. Practices. objectbuilder; must be added ;.
Static Void Main ( String [] ARGs)
{
Ireadwritelocator locator;
Builder = New Builder ();
Yylist policylist = New Policylist ();
Isingletonpolicy singletonpolicy = New Singletonpolicy ( True );
Policylist. Set < Isingletonpolicy > (Singletonpolicy, Typeof (Myconcreteclass ), " Myclassid " );
Locator = New Locator ();
Lifetimecontainer lifetime = New Lifetimecontainer ();
Locator. Add ( Typeof (Ilifetimecontainer), lifetime );
Myconcreteclass myconcreteclass1 = Builder. buildup < Myconcreteclass > (Locator, " Myclassid " , Null , Policylist );
Myconcreteclass myconcreteclass2 = Builder. buildup < Myconcreteclass > (Locator, " Myclassid " , Null , Policylist );
}
I have written so much above, to a large extent, it is similar to myconcreteclass myconcreteclass1 = new myconcreteclass (); myconcreteclass myconcreteclass2 = new myconcreteclass (); why is it so complicated? Now let's assume that myconcreteclass has a field: Public int age.CodeChanged:
Static Void Main ( String [] ARGs)
{
Ireadwritelocator locator;
Builder = New Builder ();
Yylist policylist = New Policylist ();
Isingletonpolicy singletonpolicy = New Singletonpolicy ( True );
Policylist. Set < Isingletonpolicy > (Singletonpolicy, Typeof (Myconcreteclass ), " Myclassid " );
Locator = New Locator ();
Lifetimecontainer lifetime = New Lifetimecontainer ();
Locator. Add ( Typeof (Ilifetimecontainer), lifetime );
Myconcreteclass myconcreteclass1 = Builder. buildup < Myconcreteclass > (Locator, " Myclassid " , Null , Policylist );
Myconcreteclass1.age = 28 ;
Myconcreteclass myconcreteclass2 = Builder. buildup < Myconcreteclass > (Locator, " Myclassid " , Null , Policylist );
Console. writeline (myconcreteclass2.age. tostring ());
}
Running the above Code, we found that myconcreteclass2.age. tostring () is also 28. Therefore, myconcreteclass1 and myconcreteclass2 are actually the same instance (that is, the singleton mode has taken effect ). Therefore, when using singletonpolicy, you must be clear about the above conditions for the reference type for proper use.
Careful readers may ask this question. If I have two classes, myconcreteclass and myconcreteclass1, which all inherit from myabstractclass, do I need to copy the above Code, then, change all the myconcreteclasses to myconcreteclass1? Although this can be done, it is obviously not a good method. Therefore, a policy: typemappingpolicy is introduced in objectbuilder. The following code uses typemappingpolicy:
Static Void Main ( String [] ARGs)
{
Ireadwritelocator locator;
Builder = New Builder ();
Yylist policylist = New Policylist ();
Itypemappingpolicy typemappingpolicy = New Typemappingpolicy ( Typeof (Myconcreteclass ), " Myclassid " );
Isingletonpolicy singletonpolicy = New Singletonpolicy ( True );
Policylist. Set < Itypemappingpolicy > (Typemappingpolicy, Typeof (Myabstractclass ), " Myclassid " );
Policylist. Set < Isingletonpolicy > (Singletonpolicy, Typeof (Myabstractclass ), " Myclassid " );
Locator = New Locator ();
Lifetimecontainer lifetime = New Lifetimecontainer ();
Locator. Add ( Typeof (Ilifetimecontainer), lifetime );
Myabstractclass myconcreteclass1 = Builder. buildup < Myabstractclass > (Locator, " Myclassid " , Null , Policylist );
Myabstractclass myconcreteclass2 = Builder. buildup < Myabstractclass > (Locator, " Myclassid " , Null , Policylist );
}
Now if I want to create a token, I just need to change the above itypemappingpolicy typemappingpolicy = new typemappingpolicy (typeof (signature), "myclassid"); To itypemappingpolicy typemappingpolicy = new typemappingpolicy (typeof ), "myclassid"); then, is this more in line with some object-oriented principles!
Here we will talk about typemappingpolicy and singletonpolicy. The above is just my personal understanding. Please advise if it is inappropriate!