`
javasee
  • 浏览: 960533 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Java的简单之道

阅读更多

Java应该变得更简单——或者说,更易用。目标是没有争议的,但达到目标的途径却颇可商榷。在持久层和业务层,EJB向人们证明:在J2EE领域,Sun没有能力像微软之于.NET那样提出一揽子完整的解决方案;力图一次性解决所有问题的EJB到最后只能是一种可供参考的选择——如果不是更糟的话。

Java的简单之道不在这个方向上。充分利用Java语言本身的简单性,充分发挥OOD的威力,充分借助Open Source社群的能量,用多个框架或者类库——每一个只专注一个目的,“do only one thing and do it well”——的组合来完成任务,这才是Java走向简单的正道。

最近我们经常说,Hibernate简化了持久层,Spring、PicoContainer简化了组件的组装。让我们看看,它们是如何帮助Java变得简单的吧。

————————

Simpler Java

November 2003

Discuss this Article


Introduction

Many Java vendors and open-source projects focus on complex tools, built to solve complex problems, often under the umbrella of "enterprise" Java development. Witness the explosion of complexity in J2EE, EJB, and XML. True, there’s often a tradeoff between simplicity and power, but some key architects in the Java community are working to swing the pendulum from monolithic frameworks back toward cleaner, simpler ones. A number of new projects are helping to buck the trend toward complexity, choosing instead to build lighter, focused frameworks that can power simpler applications.

In this article, we’ll examine a few frameworks, and look at the basic principles that they use to embrace simplicity. We’ll look at a lightweight container and a persistence framework. Then, we’ll look at how the larger J2EE vendors are supporting the paradigm shift.


Starting over

A few notable projects use very little of J2EE as a foundation. They are basically attempting to build a simplified notion of what it means to be a persistent object, or a container. Lightweight containers like Pico and Spring provide fewer services, but with a much less invasive architecture than EJB. Hibernate, a persistence framework, provides a persistence alternative for EJB container managed persistence (CMP).


Transparent persistence

Java Specification Request (JSR) 12 was one of the earliest requests for the Java Community Process (JCP). It was a request for transparent persistence in the form of JDO, and expressed the idea that EJB entity beans were not going to cut it. For a while, JDO faltered, but it’s seen a resurgence in the last couple of months with endorsements from the SunOne and JBoss application server development groups. With two strong JDO products in Kodo and Lido, you could say that transparent persistence has arrived.

Along the same lines, Hibernate is an open source project, founded by Gavin King, that provides transparent persistence for Java applications. I must confess that though I give an introductory Hibernate session at several conferences, I did not predict the early success of Hibernate, and guessed that it would be a bit player at best. After all, it’s proprietary; it has relatively modest development resources compared to many of the commercial OR-mapping juggernauts; and it’s an open source project, which can often work against it in larger IT departments. Still, Hibernate has burst onto the scene with tremendous early momentum, providing a surprisingly rich and fast framework.

Unlike JDO or EJB, it’s not based on byte code enhancement or code generation. It uses reflection. It has a small footprint, and runs with very low resource contention, aside from the database itself. I believe that Hibernate is successful primarily for three reasons: it’s focused, simple, and fast. Let’s look at some of the principles that have made the Hibernate project successful.

  • Do one thing, and do it well. Hibernate is an object-relational mapping framework, solely for Java applications on relational databases through JDBC. That’s the lion’s share of the Java persistence community. It’s also a focused problem. By limiting the problem domain to relational databases, Hibernate is able to make some pragmatic decisions that can dramatically impact performance. For example, since the designers knew that they would be using SQL over JDBC, they could take advantage of special algorithms and extensions for high-performance outer joins. Many other persistence frameworks are not limited to relational database back ends. Hibernate effectively trades a looser coupling for higher performance.

  • Strive for simplicity. The Hibernate team has invested heavily in effective documentation, and also in good examples. They also have a simple API. I was able to get my first application running in under fifteen minutes. I notice Hibernate’s simplicity in many places. Its methods are well named, and the abstraction layers are clear. I can work with pure Java beans, instead of building complex components. Ant integration and XDoclet support is already included. A framework should strive to provide the simplest support possible for its users.

  • Pick your battles. If you decide to build simpler applications or frameworks, you’re going to have to decide where to focus your attention. You can’t adopt every design pattern, or make every performance enhancement. Some of my early Hibernate reservations, I’ll admit, were related to reflection. Gavin King and his team rightfully assumed that the reflection overhead would be minimal compared to the performance of the SQL queries that Hibernate would generate. Instead of optimizing reflection, the team focused on the number and efficiency of queries by delaying updates, rolling many SQL statements into one, detecting "no-ops" (like an insert immediately followed by an update and a delete), and so on. The result is a surprisingly fast framework, with the transparency afforded by reflection.

Through simplicity and better focus, the Hibernate team has settled into a nice, rapid release rhythm, and it’s served them well. The user community is growing, and they’ve just completed the second major release. But persistence isn’t the only place that the trend toward simplicity is taking root.


Lightweight Containers

Lightweight containers strive to provide services for the simplest components possible. Spring and Pico are probably the most popular. If you’ve not seen them before, you may be tempted to skip this section, believing that the last thing we need is yet another container. Don’t. Lightweight containers like Spring are spreading rapidly, because they can solve problems that other containers can’t. While these frameworks are not as well known as Hibernate, at least one of them may be soon. I’m starting to hear the familiar buzz of the early stages of a successful open source project from the Spring camp. The most interesting aspects, to me, are the transparency of the objects, clean and pluggable services, and the light footprint. All of these goals lead to a simple, effective framework:

  • Strive for transparency. When you’re building a solution for developers, the simplest solutions are usually the best ones. Spring is not invasive. In fact, the objects that you snap into the container are pure POJOs, plain old Java objects, or JavaBeans, to be more precise. Objects do not depend on the container. They can run outside of the container for better testability, or address other objects as POJOs.

  • Allow for extension. Spring supports some simple services out of the box, like JTA and JDBC layers for transaction and database support. Spring also provides a nicely layered API that allows rapid extension by third parties. For example, you can already use both Hibernate and JDO objects within a Spring container.

  • Keep a small footprint. Spring’s simplicity lets it keep a small footprint. As such, it can be used in ways that you might not expect to use your average J2EE container. It starts quickly enough to be included in tests, even if you plan to run many of them with each build. You can use Spring on the client, or within a mobile device.

Since lightweight containers work with POJOs and clearly separate concerns of various services, they are well-suited for the next generation of program paradigms like aspect oriented programming (AOP). Both Hibernate and Spring present simplified abstractions. How do we know if the abstractions will be enough?


Leaking abstractions

It’s clear that cleaner, simplified abstractions are coming back, but it’s not enough to be simple. All abstractions leak. We’ll never be able to capture all of the complexity of a native database API in a simplified framework, and a lightweight container will never be able to do everything that a heavyweight container does. For this reason, it’s important to expose the abstraction layer immediately below the new one. For example, Hibernate has no notion of stored procedures. Instead, it gives you full access to the JDBC connection, so that you can call stored procedures outside of Hibernate.

Also, an adaptable, pluggable architecture, with loose coupling in appropriate areas, lets you snap out inappropriate services, and snap in ones that are more robust. For example, Spring allows pluggable persistence and transactions.

So far, we’ve mentioned a couple of open source projects. You may be interested in seeing what the major J2EE vendors are doing to support a simpler Java paradigm.


Alternative approaches

Of course, building simpler frameworks from the ground up is only one way to gain simplicity. The Java community is also trying other approaches. Some are trying whole new programming paradigms, like model driven architecture (MDA), or aspect oriented programming (AOP). Others are trying a more incremental approach, like letting tools shield us from some of the complexity.


Simplifying tools

Some vendors, especially those with a significant investment in J2EE, might view lightweight containers, Hibernate, and frameworks like them as too radical a departure from the status quo. They would rather not burn J2EE to the ground to start over. Instead, many of them would rather use a smarter set of tools to insulate developers from complexity. Sun’s Rave and BEA’s Workshop favor this approach.

Taking a page from Visual Studio’s book, BEA and Sun have built tools to shield inexperienced developers from the complexities of J2EE. The hope is that smarter tools with aggressive code generation can shield the most common J2EE developers from that level of detail.

The ability to build loosely coupled applications, especially through Web services, is a fundamental capability of BEA’s Workshop. They intend to provide a series of simplified abstractions that allow users to be much more productive. To succeed with Workshop, the challenge for BEA is twofold. They must abstract away most of the J2EE complexity for their users, and still allow enough of the underlying abstractions to peek through so that their customers can use Workshop in ways beyond the intentions of the original design team.


Which approach wins?

Will either approach step up and slay the complexity dragon? In truth, for the foreseeable future, we’re likely to see many more attempts to battle complexity. We’re simply getting to the upper bound of what most modern application development teams can handle. But we won’t be limited to any single approach. Vendors with larger investments in existing Java enterprise frameworks will tend to favor better tools that free their customers from tedious details. Other efforts, like Hibernate and Spring, will attempt to take a more radical and fundamental approach. Still others will try to make the case that Java has taken us as far as it can, so we need to move on to the next language or paradigm like C# or AOP. The playing field has been set. Let the games begin.


Resources


About the Author

Bruce Tate is a consultant in Austin, Texas. His books include best-selling Bitter Java, and Bitter EJB. He is a regular speaker at industry conferences, including TheServerSide Symposium, and the No Fluff, Just Stuff Java Symposium series. He has 15 years of development experience, and is the founder and president of J2Life, which offers consulting and services in the areas of Java persistence, developer marketing, and the application development process.

分享到:
评论

相关推荐

    Java自学之道(完整版)

    Java自学之道是一本专为想要自我提升Java编程技能的学习者设计的指南。它涵盖了从基础知识到高级概念的全面内容,旨在帮助读者构建坚实的技术基础,理解Java语言的核心特性,并掌握实际开发中的应用技巧。 首先,从...

    java课设华容道小游戏

    针对以上要求,开发一个小型的华容道小游戏,满足如下功能要求:1、设计一个华容道游戏界面;2、创建对象,代表华容道中的人物;...要求界面简洁美观,布局合理,操作简便,简单易用,任何人可轻松操作。

    java java简单的聊天程序

    本项目名为“Java简单聊天程序”,旨在提供一个基础的、易于理解的示例,帮助初学者掌握Java网络编程的基本概念和技术。在这个简单的聊天程序中,我们将探讨以下几个关键知识点: 1. **Socket编程**:Java的Socket...

    单元测试之道Java版.pdf(高清)

    根据提供的文件信息,“单元测试之道Java版.pdf(高清)”主要介绍了在Java开发环境中进行单元测试的方法、策略及最佳实践等内容。以下将基于该文件的标题、描述、标签以及部分内容来详细阐述相关的知识点。 ### ...

    Java华容道(小游戏)

    【Java华容道小游戏开发详解】 Java华容道是一款基于编程技术实现的传统智力游戏,它以三国时期的历史故事为背景,玩家需要通过合理操作棋盘上的曹操、关羽等人物,帮助曹操从棋盘的一端逃至另一端。这个游戏对Java...

    Java自学之道

    ### Java自学之道——关键知识点详解 #### 第0章 JDK安装与环境配置 - **JDK配置的重要性**:Java开发工具包(JDK)是编写Java程序的基础。正确的JDK配置能够确保开发者能够在自己的计算机上顺利地编译和运行Java...

    Java 游戏 华容道

    Java 游戏开发是软件开发领域的一个分支,它专注于创建基于Java编程语言的交互式娱乐体验,例如华容道这样的经典益智游戏。华容道,源于中国古代的棋盘游戏,挑战玩家通过移动棋子来帮助曹操从困局中逃出,具有很高...

    Java华容道小游戏

    Java华容道小游戏是一款基于Java编程语言开发的简单游戏,主要体现了Java的图形用户界面(GUI)设计和事件处理能力。在本项目中,开发者利用Java的Swing库或者JavaFX库创建了游戏界面,让玩家能够通过鼠标操作进行...

    Java 自学之道

    第0章 JDK安装和最简单的环境变量配置方法 第1章 Java基础概念 一、 Java基础语法 1、Helloworld.java 2、标识符 3、关键字 4、常量 5、变量 6、语句 6.1 if语句和switch语句 6.1.2 if语句举例 6.1.3 switch语句 ...

    java 修炼之道

    java 修炼之道 ,学习不能凭一时的冲动,而是每天的坚持,做好一件事,往往没有那么简单,想成为自己心中的榜样,必须拥有一技之长~

    Java自学之道内容-闵开慧

    《Java自学之道》是闵开慧撰写的一本适合初学者入门的Java编程指南。书中通过生动的生活实例将抽象的Java理论变得通俗易懂,旨在帮助读者快速掌握Java基础知识,并提供了大量的面试练习题以增强实践能力。 在Java...

    华容道java课程设计

    《华容道Java课程设计详解》 华容道游戏,源于我国古代的一种棋类游戏,以其独特的解谜性质和深厚的文化底蕴,深受人们喜爱。在IT教育领域,以Java编程语言进行华容道的课程设计,不仅可以提升学生的编程技能,还能...

    java自学之道

    第0章 JDK安装和最简单的环境变量配置方法 第1章 Java基础概念 一、 Java基础语法 1、Helloworld.java 2、标识符 3、关键字 4、常量 5、变量 6、语句 6.1 if语句和switch语句 6.1.2 if语句举例 6.1.3 switch语句 ...

    java图形界面游戏华容道

    Java图形界面游戏华容道是一款非常适合初学者进行实践的项目,它将编程语言与游戏开发相结合,让学习者在实际操作中理解Java图形用户界面(GUI)的基本原理和设计思路。下面将详细介绍这个项目的相关知识点。 首先...

    Java自学之道大纲-闵开慧

    《Java自学之道》是闵开慧撰写的一本适合初学者的Java编程教程,它通过生动的实例和面试题目,帮助读者快速掌握Java编程基础。以下是该书内容的详细概述: 一、基础篇 1. JDK安装与环境变量配置:这是学习Java的第...

    基于Java的华容道小游戏设计与实现

    华容道,源自中国传统的智力玩具,挑战玩家通过移动棋盘上的棋子,帮助曹操从起点移动到终点,其规则简单而策略复杂,深受各年龄段玩家喜爱。 本项目的核心知识点主要集中在以下几个方面: 1. **Java基础**:项目...

    JAVA nio的一个简单的例子

    在这个“JAVA nio的一个简单的例子”中,我们将探讨如何使用Java NIO进行简单的服务器-客户端通信,并计算字符串的哈希值。 在传统的BIO模型中,每个连接都需要一个线程来处理,当并发连接数量增加时,系统会创建...

    单元测试之道Java版使用JUnit

    单元测试是软件开发过程中的重要环节,它旨在验证代码的各个独立模块是否按照预期...通过阅读《单元测试之道Java版使用JUnit》这本书,你可以深入了解如何有效地使用JUnit进行单元测试,以及如何构建强大的测试框架。

Global site tag (gtag.js) - Google Analytics