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

推荐订阅源

WordPress大学
WordPress大学
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
AI
AI
Webroot Blog
Webroot Blog
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
B
Blog RSS Feed
小众软件
小众软件
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
W
WeLiveSecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Troy Hunt's Blog
云风的 BLOG
云风的 BLOG
P
Privacy International News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Vercel News
Vercel News
Y
Y Combinator Blog
P
Proofpoint News Feed
V2EX - 技术
V2EX - 技术
AWS News Blog
AWS News Blog
F
Fortinet All Blogs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
V2EX
MongoDB | Blog
MongoDB | Blog
SecWiki News
SecWiki News
The Register - Security
The Register - Security
博客园_首页
T
Threat Research - Cisco Blogs
Hacker News - Newest:
Hacker News - Newest: "LLM"
Recorded Future
Recorded Future
V
Vulnerabilities – Threatpost
I
InfoQ
雷峰网
雷峰网
C
Check Point Blog

博客园 - 学习.NET

听说微软要收购 winrar 请教泛型方法重载的一个小问题 用guid类型的字段作主键,用char(36)好还是用uniqueidentifier好? 批量插入与更新 李斯与赵高 MSDN Magazine 微软首席架构师Anders Hejlsberg谈C#的一些东西。 迷惘中 实现简单多层表头的办法 如何实现数据转移?不知这个叫法对不对。 什么是多套帐?如何实现? 代码复用 下载VS.NET2005 Beta1 人就是人 何时需要非规范化 复习基础知识:数据规范化 连接本机时,localhost的意思 正在出差,很长时间没来。 whidbey
批判工厂方法模式
学习.NET · 2006-04-21 · via 博客园 - 学习.NET

之所以取这个标题,纯粹是为了吸引眼球,:)

本人没用过patterns,没有实践经验,所以特来问一个问题。
工厂方法模式中,有两个类体系,一个是产品类体系,如IProduct、ConcreteProductA、ConcreteProductB、ConcreteProductC,具体产品类全部实现IProduct接口。另一个是工厂类体系,如IFactory、ConcreteFactoryA、ConcreteFactoryB、ConcreteFactoryC,对应的具体工厂类用于“生产”对应的具体产品类。

在使用时我们一般这样写:
IFactory f = new ConcreteFactoryA();
IProduct p = f.CreateProduct();
p.ExecuteFunction();// 客户代码中使用IProduct所提供的功能。

如果出现了新品种的产品,我们就先建立新产品及其对应的工厂类,然后这样修改使用代码:
IFactory f = new ConcreteFactoryD();// 仅改这一个地方
IProduct p = f.CreateProduct();
p.ExecuteFunction();

客户端代码虽然不再对具体产品类产生依赖了,但却对具体工厂类产生依赖了。

假设我们不用工厂方法模式,则这样写代码:
IProduct p = ConcreteProductA();
p.ExecuteFunction();
显然,不用工厂方法模式的代码少了一行。请教各位,工厂方法模式给我带来的好处什么?难道是多写了一行代码?

如果让 客户端代码 对 具体产品类和具体工厂类 均不产生依赖,而是依赖于抽象产品类和抽象工厂类,这样在更换具体产品类时客户代码就不用修改。如果语言平台提供了反射功能,可以将客户代码的修改转换为配置文件的修改:
IFactory f = (IFactory)Assembly.Load(“AssemblyNameFromConfig”).CreateInstance(“ConcreteFactoryA”);
IProduct p = f.CreateProduct();
p.ExecuteFunction();// 客户代码中使用IProduct所提供的功能。

假设我们不用工厂方法模式,则这样写代码:
IProduct p = (IProduct)Assembly.Load(“AssemblyNameFromConfig”).CreateInstance(“ConcreteFactoryA”);
p.ExecuteFunction(); // 这种用法是不是就是provider model?
显然,用工厂方法模式的代码还是多写了一行,请教各位,难道这是工厂方法模式给我带来的好处?这里是不是基于抽象编程即可?我感觉自己没有找到真正使用工厂方法模式的场景,请大家帮我找一找。