模块化Java:声明式模块化
本文是模块化Java系列文章的第4篇,介绍的是声明式模块化。文中描述了组件如何以声明的方式来定义并组织在一起,而无需让代码依赖于OSGI API。
作者 张逸 发布于 2009年3月27日 下午9时49分
利用C# 3.0提供的扩展方法技术,可以为已经编译好的程序集类型增加新的方法,从而应对新的扩展。除了在可扩展性方面所具有的优势之外,如果能够合理地结合泛型与类型推断,扩展方法还可以有效降低代码的重复,提高程序的可重用性。例如,这样的方法实现:
public class CustomerDAL
{
public IEnumerable
{
return from customer
in context.Customer
where customer.RoleName.Equals(roleName)
select customer;
}
}
调用如下方法,可能抛出NullReferenceException异常:
Customer customer = new CustomerDAL().FindCustomers(Role.Admin).First();
我们需要对返回结果进行验证,如果返回为null,则可以抛出自定义异常,或者创建一个空对象,例如:
public IEnumerable
{
IEnumerable
in context.Customer
where customer.RoleName.Equals(roleName)
select customer;
if (customers == null)
{
throw new MyException("Cann't find the customers.");
}
return customers;
}
如果系统有许多方法都需要对返回结果进行验证,则这样的验证逻辑就会充斥在各个方法体中,既不利于重用,也会对未来的修改造成极大的阻碍。当然,我们可以引入Null Object模式来替代对null值的判断逻辑,但这种方式仍然需要为多种类型定义不同的Null Object类型。
Craig Andera在其博客文章中提出使用扩展方法对调用进行验证。他写道:
NullReferenceException异常会抛出,但是我们希望有更具体的异常信息。因此,我们编写了如下的扩展方法:
public static T OrThrow
(this T obj, Exception e) {
if (obj == null) {
throw e;
}
return obj;
}
利用OrThrow
Customer customer = new CustomerDAL().FindCustomers(Role.Admin).OrThrow(new MyException("Can't find Customer")).First();
Craig Andera提出:
OrThrow扩展方法对于你所要调用的类型而言是通用的,并且它返回了该类型,所以你可以将其插入到表达式链中,而不会丢失智能感应功能。并且因为类型推断功能,实际上并不需要指定具体的类型。
也就是说,OrThrow扩展方法可以应用到任何类型上,因此它可以在各种类型上重用非空验证甚至是调用验证。借鉴这一思想,我们还可以利用此方法默认实现对象实例的创建,以避免抛出NullReferenceException异常,例如:
public static T Instance
{
if (obj == null)
{
obj = new T();
}
return obj;
}
由于Instance扩展方法中的类型参数T需要创建实例,因此必须添加new()约束。所以该扩展方法存在一定的局限,例如无法应用在之前的IEnumerable
{
public List Foo()
{
return null;
}
}
通过Instance扩展方法,可以安全地调用List
Console.WriteLine(new ListObject().Foo().Instance().Count);
控制台打印出来的结果为0。如果没有Instance扩展方法,则会抛出NullReferenceException异常。
作为C# 3.0增加的新特性,扩展方法在大量项目中得到了广泛地应用,但绝不仅仅是提高可扩展性这么简单。在进行项目开发时,若能适当地考虑使用扩展方法,说不定会带来出奇制胜的效果。
第一 我在我的公共函数里面写了
public static T Instance(this T obj) where T:new()
{
if (obj == null)
{
obj = new T();
}
return obj;
}
但是报错
Constraints are not allowed on non-generic declarations
public static T Instance<T>(this T obj) where T:new()
{
if (obj == null)
{
obj = new T();
}
return obj;
}
我的必须这样才通得过,大家有按文章上的那种通过的吗?该怎么写?</t>
博客原文:
public static T OrThrow<T>(this T obj, Exception e) {
if (obj == null) {
throw e;
}
return obj;
}
下面回复
That should be
public static T Instance<T>(this T obj) where T: new()
The <T> indicates that the method is generic on
InfoQ的编辑啊</t></t></t>
thank you
搞定!谢谢了 !。。。估计编辑是忘记了:)
文中的代码段依次为:
1.
public class CustomerDAL
{
public IEnumerable<Customer> FindCustomers(string roleName)
{
return from customer
in context.Customer
where customer.RoleName.Equals(roleName)
select customer;
}
}
2.
public IEnumerable<Customer> FindCustomers(string roleName)
{
IEnumerable<Customer> customers = from customer
in context.Customer
where customer.RoleName.Equals(roleName)
select customer;
if (customers == null)
{
throw new MyException("Cann't find the customers.");
}
return customers;
}
3.
public static T OrThrow<T>(this T obj, Exception e) {
if (obj == null) {
throw e;
}
return obj;
}
4.
public static T NullObject<T>(this T obj) where T:new()
{
if (obj == null)
{
obj = new T();
}
return obj;
}
5.
public class ListObject
{
public List<string> Foo()
{
return null;
}
}</string></t></t></customer></customer></customer>
在我的博客中,代码是正确的。
www.agiledon.com/post/2009/03/Use-Extension-Met...
本采访是在伦敦举行的QCon2009上记录的,Ian Robinson和Jim Webber探讨了如何将Web作为整合平台以及REST在理论上和实践中的好处。
项目管理对于项目成败至关重要,但实践中每个项目都有自己的独特性,没有现成的解决方案可以套用。书中从应对实际风险的角度出发,讲述了从项目启动、项目规划到项目结束的整个管理流程,展示了作者的思考过程。本迷你书从原书中精选出5个章节。
在这个演讲中,Fred将会揭示敏捷的一些外在因素,并会重点关注敏捷获得成功的内在原因。从案例研究和真实的项目经验来看,Fred认为:工具、管理体系都不能让你变得敏捷。敏捷的成功,植根于士气高涨、充分授权的工作者身上,他们能够以不同以往的方式思考问题。
Eben Hewitt的新书《Java SOA Cookbook》从Java实现的角度讨论了面向服务架构。Eben在书中讨论了SOA基础、工具、最佳实践和SOA治理等主题。
Mark Richards的新书《Java消息服务》第二版覆盖了JMS的许多主题, 包括发布和订阅模式以及点对点模式,消息过滤和事务等。InfoQ与Mark谈论了跟他的新作。
6 条回复
关注此讨论 回复