编写人生
写写代码,写写人生
随笔- 241  文章- 0  评论- 659 
博客园  首页  新随笔  联系  管理  订阅 订阅
ObjectBuilder 学习笔记
        [TestMethod]
        
public void CanCreateInstances()                                                    //测试是否能够创建实例
        {
            Builder builder 
= new Builder();                                                //对象建造器
            Locator locator = CreateLocator();                                              //定位器

            ConstructorPolicy policy 
= new ConstructorPolicy();                             //建立一个构造策略
            policy.AddParameter(new ValueParameter<int>(12));                               //为策略加入参数
            builder.Policies.Set<ICreationPolicy>(policy, typeof(SimpleObject), null);      //将此策略加入建造器

            SimpleObject m1 
= builder.BuildUp<SimpleObject>(locator, null, null);           //建造对象
            SimpleObject m2 = builder.BuildUp<SimpleObject>(locator, null, null);

            Assert.IsNotNull(m1);
            Assert.IsNotNull(m2);
            Assert.AreEqual(
12, m1.IntParam);
            Assert.AreEqual(
12, m2.IntParam);
            Assert.IsTrue(m1 
!= m2);
        }

        [TestMethod]
        
public void CanCreateSingleton()
        
{
            Builder builder 
= new Builder();
            Locator locator 
= CreateLocator();

            ConstructorPolicy policy 
= new ConstructorPolicy();
            policy.AddParameter(
new ValueParameter<int>(12));
            builder.Policies.Set
<ICreationPolicy>(policy, typeof(SimpleObject), null);
            
//为SimpleObject添加单实例策略。
            builder.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(SimpleObject), null);  

            SimpleObject m1 
= builder.BuildUp<SimpleObject>(locator, null, null);
            SimpleObject m2 
= builder.BuildUp<SimpleObject>(locator, null, null);

            Assert.AreSame(m1, m2);
        }

 

posted on 2006-03-14 20:41 编写人生 阅读(461) 评论(0) 编辑 收藏
刷新评论刷新页面返回顶部
程序员问答社区,解决您的IT难题
博客园首页博问新闻闪存程序员招聘知识库
Copyright ©2012 编写人生