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

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

博客园 - Cavingdeep

用metaclass实现AOP风格的Profiler Singleton implementation using metaclass 初试IronPython与.NET的集成 Refactoring as a way to improve the existing design 用metaclass来实现AOP 新兴XML处理方法VTD-XML介绍 Performance Tips I DCG 2.0.72 (Beta1) 发布了 NUnit发布2.2.3兼容.NET 2.0 RTM 如果你想拥有一个可嵌入式模板引擎…… 改进ASP语法打造更高效的模板语言II 改进ASP语法打造更高效的模板语言 XML的特征以及一些用途 Release of DbHelper 1.2.1 深入XML系列技术 DbHelper at Tigris SQLite系列 集合的初始容量与性能 DbHelper basic usage
不该用Generics实现Abstract Factory的理由
Cavingdeep · 2006-03-24 · via 博客园 - Cavingdeep

最近在与Ninputer争执.NET与Java的泛型特点,他的意见是.NET的Generics要更强(实现层面与语法层面两个层面),我的意见是Java的Generics的语法层面要更强(语法层面的强主要是指易用性)。他让我看了一篇用.NET Generics实现的Abstract Factory的文章http://blog.joycode.com/ninputer/archive/2006/01/12/70313.aspx,说如果用Java就会遇到很多麻烦。因为我也是初学Java的Generics,所以比较感兴趣这个话题,看了一下文章后做了一些实现后得出了以下一些结论。

该不该使用Generics实现Abstract Factory?

我的意见是不应该,理由如下:

  1. 具体创建对象的时候,也就是在ConcreteFactory中,不是所有情况都是简单地一个new操作就算是创造对象了,就像它的名字所示那样,具体对象具体操作,这里不能泛化,一定要根据具体类型来实现对象创建。
  2. 就像许多人提到的,到底用多少个type variable才算合适呢?AbstractFactory<A, B>, AbstractFactory<A, B, C> ??? 简单地说,Abstract Factory是不能够定量的,目前.NET与Java的泛型都不能实现这点。

Java的泛型真的那么弱吗?如果要实现定量的Abstract Factory真的很难吗?

我的答案是否,实际上,我觉得用Java的语法无论在实现上的阅读与易用性都要比.NET的语法强。下面是我给出的Abstract Factory的实现,虽然我说过,用Generics实现Abstract Factory并不合适。

interface Processor {}

interface Ram {}

class PentiumProcessor implements Processor {}

class DDRRam implements Ram {}

interface AbstractFactory<PA, PB> {
   PA createA();
   PB createB();
}


class IntelFactory implements AbstractFactory<PentiumProcessor, DDRRam> {
   
public PentiumProcessor createA() {
      
// 一切特化且可在这里实现。
      return new PentiumProcessor();
   }


   
public DDRRam createB() {
      
// 一切特化且可在这里实现。
      return new DDRRam();
   }

}


// 使用
AbstractFactory<? extends Processor, ? extends Ram> factory =
   
new IntelFactory();
      
Processor processor 
= factory.createA();
Ram ram 
= factory.createB();

以上代码并没有运用不必要的TypeToken,而且也没有实例化时特化的问题,但是仍然没有解决Abstract Factory的量的问题。可以看出,这段代码非常的简洁,我觉得这段代码真正突出了Generics的优点:泛化类型,而没有做不应该做的过多的事情(比如泛化本来应该是特化的东西)。

注意Java的Generics语法是可以使用wildcard的(?),这是.NET所没有的。wildcard的特点之一就是能够在变量声明中使用,这是很重要的一大特色。