J2EE应用中的OO设计建议(一)——OO Design Recommondations for J2EE Applications

       在J2EE工程开发中,你常常会遇到这样的情况:我的每个对象类的代码在独立的层次上来看都是很漂亮的代码,可是结果呢,J2EE工程糟糕的让自己不得不放弃这个工程。J2EE自身覆盖了很多经典设计,但是糟糕的实现代码仍然会引起项目失败。不幸的是仍然有很多开发者只专注于J2EE的API而真正忽略好的代码设计的重要性。首要的是遵循good coding practice。

      重要 OO design is more important than any particular implementation technology (such as J2EE, or even Java). Good programming practices and sound OO design underpin good J2EE applications. Bad Java code is bad J2EE code.(OO设计比起具体技术上的实现更重要)

     设计模式给good coding带来了一些前人总结的OO Design模式,这些模式在J2EE以及OO Design中如何出现的呢,以及在什么情况下引入。

    重要 Take every opportunity to learn from the good (and bad) code of others, inside and outside your organization. Useful sources in the public domain include successful open source projects and the code in the core Java libraries. License permitting, it may be possible to decompile interesting parts of commercial products. A professional programmer or architect cares more about learning and discovering the best solution than the buzz of finding their own solution to a particular problem.(时刻借鉴学习他人的优秀代码设计)

通过接口实现松耦合

  Gang of Four在他们的经典著作中《设计模式-可重用的代码设计》一书中提倡OO设计第一个原则是"Program to an interface, not an implementation(基于接口编程)"。

   重要 Program to interfaces, not classes. This decouples interfaces from their implementations. Using loose coupling between objects promotes flexibility. To gaim maximum flexibility, declare instance variables and method parameters to be of the least specific type required.(接口和实现分离,获得灵活性,实例字段和方法参数的类型尽可能普通会带来代码更大的灵活性)

   虽然接口编程会带来一些编码上的复杂性,但是它所换来的是更易于维护,将接口和实现上的解耦合与这一点复杂性相比,意义更大。

    接口编程的优点:

  1. 实现类的变化修改不会影响调用类代码
  2. 实现类的高度灵活性
  3. 更易于测试。对接口的定义使桩应用以及并团队能够并行工作。

   基于接口的编程实现J2EE的可移植性,接口编程与reflection for configuration(映射配置)一起使用使得编程更有效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值