惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Kain

Microsoft.Practices.Unity 的一个线程安全Bug浅析 NET Reflector 7发布,其 不再免费 自定义EF4 Model 代码生成 - Kain .net 4.0 中对多线程新特性(四)--任务和任务工厂 .net 4.0 中对多线程新特性(三) .net 4.0 中对多线程新特性(二) .net 4.0 中对多线程新特性(一) Flex&.Net开篇 - Kain SqlSever N层表数据查询效率 [读书笔记]Start-up fatigue(启动杂役) 马上又要过中秋和国庆了! 一个小问题 C#2.0 新的关键字 yield 我们的游戏! 不知道有没有同在学习aspnetforums的 郁闷! 关于面试 关于DataGride的Key事件 开心就好!
(抽象)工厂的另一种实现方式
Kain · 2010-08-19 · via 博客园 - Kain

工厂模式是在设计模式中比较容易理解和掌握的一种模式,其使用非常的普遍。在项目实践中个人对常用的工厂模式做了一个调整,整个实现有点像工厂模式和抽象工厂模式的混合体,这样做的好处在于结合工厂模式的易用和抽象工厂的灵活。具体的实现可能如下:

在这个Case中有2个类,2个接口。其中IServiceFactory定义了工厂的职责GetService<T>,ServiceFactory实现IServiceFactory接口(注意接口是显示实现,这个很重要,因为类的静态方法和实例方法签名不能相同)。

 ServiceFactory.cs

代码

 1     public class ServiceFactory:IServiceFactory
 2     {
 3         private static IServiceFactory _inc;
 4 
 5         private ServiceFactory()
 6         { 
 7 
 8         }
 9         static ServiceFactory()
10         { 
11 
12             _inc = Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.Current.GetInstance<IServiceFactory>();
13             if (_inc == null)
14 
15                 _inc = new ServiceFactory();
16         }
17 
18         public static T GetService<T>()
19         {
20             return _inc.GetService<T>();
21         }
22 
23         T IServiceFactory.GetService<T>()
24         {
25             return default(T);
26         }
27     }

 作为ServiceFactory的调用者在使用上和普通的工厂没有任何区别,利用IOC容器可以非常方便的用Mok对象来降低对具体业务的依赖性,极大的了方便单元测试。同时我们也可以通过条件编译在发布的时候去掉对IoC容器的依赖。

Test:

代码

 1  IUnityContainer container;
 2         [Test]
 3         public void CalculateTest()
 4         {
 5             Setup();
 6 
 7             var factoryMock = new Mock<IServiceFactory>();//Mock Factory
 8             var calcMock = new Mock<ICalculateService>();//Mock Service
 9             calcMock.Setup(c => c.Sum(It.IsAny<int>(), It.IsAny<int>())).Returns<intint>((x, y) => x + y); //Mock Calculate Sum Method
10             factoryMock.Setup(c => c.GetService<ICalculateService>()).Returns(calcMock.Object); //Mock GetService<T> Method
11             
12             container.RegisterInstance<IServiceFactory>(factoryMock.Object);//向容器中注入Mock的Factory
13 
14             var calc = ServiceFactory.GetService<ICalculateService>();
15 
16             Assert.IsNotNull(calc);
17             int a = 1;
18             int b = 2;
19             int experct = a + b;
20             int actual = calc.Sum(a, b);
21             Assert.IsTrue(experct == actual);
22         }
23 
24         public void Setup()
25         {
26             container = new UnityContainer();
27             Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);
28         }