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

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 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的特点之一就是能够在变量声明中使用,这是很重要的一大特色。