如何设计Java应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3213617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to design Java application?
提问by Dushyanth
What are the general guidelines and best practices to keep in mind while designing Java application [Simple console apps to J2EE apps]?.
在设计 Java 应用程序 [从简单控制台应用程序到 J2EE 应用程序] 时要记住的一般准则和最佳实践是什么?
Hi
你好
I recently completed Java programming tutorial from Sun and practised core java (I have previous programming experience). Now I understand the basics of Inheritance, Abstraction , Polymorphism,Encapsulation
我最近完成了 Sun 的 Java 编程教程并练习了核心 Java(我之前有编程经验)。现在我了解了继承、抽象、多态、封装的基础知识
Now i am writing Java code without much difficulty, but am not sure of application design. This is my main problem: "DESIGNING" the application. Say if i have given a task to create an application in Java, What should I start up with? How to think about? Any formal/informal guidelines I should follow while developing class hierarchies? I am really confused (abstract class or interface or sub class..?). Should I start by model everything, before writing code?
现在我正在编写 Java 代码并没有太多困难,但我不确定应用程序设计。这是我的主要问题:“设计”应用程序。假设我给了一个任务来用 Java 创建一个应用程序,我应该从什么开始?怎么考虑?在开发类层次结构时,我应该遵循任何正式/非正式的指导方针吗?我真的很困惑(抽象类或接口或子类..?)。在编写代码之前,我应该先建模一切吗?
It would be very useful for people like me to have a SET OF GENERAL GUIDELINES/BEST PRACTICES, which we can follow while start developing a new java application.
对于像我这样的人来说,拥有一套通用指南/最佳实践将非常有用,我们可以在开始开发新的 Java 应用程序时遵循这些指南。
Please provide me some guidelines/thoughts/books/resources/tools I should read or Use
请为我提供一些我应该阅读或使用的指南/想法/书籍/资源/工具
Thanks in advance Scott
提前致谢 斯科特
回答by Péter T?r?k
It is difficult to give really general advice as there are so many different Java apps on different domains. However, one absolutely recommended book is Domain Driven Designby Eric Evans. See also Wikipediafor a short intro on it.
由于不同领域有许多不同的 Java 应用程序,因此很难给出真正通用的建议。然而,绝对推荐的一本书是Eric Evans 的领域驱动设计。另请参阅Wikipedia以获取有关它的简短介绍。
General advice:
一般建议:
- don't try to design everything up front- do a reasonably good design which enables you to start coding, then refactor as your understanding of the problem domain and the implementation deepens
- try to divide difficult problems into smaller parts/steps/modules which you can tackle one by one
- try to think in terms of objects with well defined responsibilities, which (more or less) model the problem domain and cooperate to solve a problem / handle a task
- becoming good at design requires practice, first and foremost; don't be afraid to make mistakes. However, when you do, analyze them and learn from them as much as you can
- learn design patterns, but don't be overzealous - use them only when they really solve a problem and make your code cleaner
- 不要试图预先设计所有东西- 做一个相当好的设计,使您能够开始编码,然后随着您对问题域和实现的理解加深而进行重构
- 尝试将困难的问题分成更小的部分/步骤/模块,您可以一一解决
- 尝试根据具有明确职责的对象来思考,这些对象(或多或少)为问题域建模并合作解决问题/处理任务
- 擅长设计首先需要实践;不要害怕犯错。但是,当您这样做时,请尽可能多地分析它们并从中学习
- 学习设计模式,但不要过分热情——只有在它们真正解决问题并使你的代码更清晰时才使用它们
回答by Ayubinator
Start by looking up UML Class diagrams that will get the ball rolling in the right direction, then take a look at Gang of Four design patterns. That is an excellent first step.
首先查找将使球朝着正确方向滚动的 UML 类图,然后查看四人组设计模式。这是一个很好的第一步。
http://en.wikipedia.org/wiki/Class_diagram
http://en.wikipedia.org/wiki/Class_diagram
http://en.wikipedia.org/wiki/Design_Patterns
http://en.wikipedia.org/wiki/Design_Patterns
Lastly, I would pour over good open source code like the Spring Framework
最后,我会倾注像 Spring Framework 这样优秀的开源代码
回答by ring bearer
Welcome to stack overflow. If you are good with Java, read Head First Design Patterns.and Head First Object-Oriented analysis and design- er, may be in reverse order :)
欢迎使用堆栈溢出。如果您擅长 Java,请阅读Head First Design Patterns。和 Head First 面向对象的分析和设计- 呃,可能是相反的顺序:)
回答by Jean-Philippe Caruana
I'd recommend emergent design with TDD.
我建议使用 TDD 进行紧急设计。
I think there is nothing specific to Java design : if you already know about Object Design, you're ready to go !
我认为没有什么特定于 Java 设计:如果您已经了解对象设计,那么您就可以开始了!
回答by Benoit Courtine
There are various paradigms (very often 3 letters acronyms) :
有各种范式(通常是 3 个字母的首字母缩写词):
- DDD : Domain Driven Design
- SDD : Serviice Driven Design
- MDA : Model Driven Architecture (code and architecture is extracted from the UML model)
- TDD : Test Driven Development (validation tests are implemented before the application)
- DDD:领域驱动设计
- SDD : 服务驱动设计
- MDA : Model Driven Architecture(代码和架构是从UML模型中提取出来的)
- TDD:测试驱动开发(在应用程序之前实施验证测试)
With theses key-words, you'll find a lot of informations on the web.
有了这些关键词,你会在网上找到很多信息。
In J2EE, I would say that the SDD is the most used (it is now very "normalized", even if I'm not sure it is the best solution) : service (software "intelligence") > domain (bean objects used for persistance) > DAO (persistance).
在 J2EE 中,我会说 SDD 是最常用的(它现在非常“规范化”,即使我不确定它是最好的解决方案):服务(软件“智能”)> 域(bean 对象用于持久性)> DAO(持久性)。
Now DDD is becoming more and more used : the conception is refocused on domain objects, which are taking the "software intelligence" layer.
现在 DDD 越来越多地被使用:这个概念重新聚焦在领域对象上,这些对象正在采取“软件智能”层。
回答by Mustafa Zengin
Do not start coding immediately without having any design. But this does not mean that you should design all before coding. Because from my previous experiences, I could not have any design which does not need corrections. Especially if you are new in a programming language your design will change according to the features of the language you are using and the libraries available. My advice is to have a general design which is based on the most important aspects of object oriented design such as inheritance, polymorphism, encapsulation etc. Starting from this general design and the needs you encounter while programming, revise your design accordingly.
不要在没有任何设计的情况下立即开始编码。但这并不意味着您应该在编码之前设计所有内容。因为根据我之前的经验,我不可能有任何不需要修正的设计。特别是如果您是编程语言的新手,您的设计将根据您使用的语言的特性和可用的库而改变。我的建议是有一个基于面向对象设计最重要方面的通用设计,例如继承、多态、封装等。从这个通用设计和你在编程时遇到的需求开始,相应地修改你的设计。
As much as you get experienced in the language, your first general design will fit in a much more efficient way to your program.
只要你在语言方面有经验,你的第一个通用设计就会以更有效的方式适应你的程序。
Although most of the people say that one object oriented design should be able to be written in any object oriented language, it is not that easy to have a very good generic design like that. To be more realistic, disregarding the language that is used for implementing a particular design is not a good way as far as I am concerned.
虽然大多数人都说一个面向对象的设计应该能够用任何面向对象的语言编写,但拥有这样一个非常好的通用设计并不容易。更现实地说,就我而言,无视用于实现特定设计的语言并不是一个好方法。
回答by JRL
Well odds are what you'll be doing has already been done before - at least something similar. Luckily, there's lots of open-source stuff nowadays. So if you really have no idea, one thing you can do is download several open-source applications that do the same thing and study them. It should give you a good start.
很可能你将要做的事情之前已经做过——至少是类似的事情。幸运的是,现在有很多开源的东西。所以如果你真的不知道,你可以做的一件事就是下载几个做同样事情的开源应用程序并研究它们。它应该给你一个良好的开端。
回答by Aravind Yarram
In my opinion it all boils down to meeting the below
在我看来,这一切都归结为满足以下条件
- easy to understand
- easy to maintain and evolve
- multiple developers able to contribute to the project (mostly in parallel)
- 容易理解
- 易于维护和发展
- 多个开发人员能够为项目做出贡献(主要是并行的)
To achieve the above there are certain guidelines and principles that are suggested by experts based on experience which are
为了实现上述目标,专家根据经验提出了一些指导方针和原则,这些指导方针和原则是
- Follow layered architecture
- Follow the SOLID principleswithin and across layers. All the design patterns are one way or the other help achieve these principles only. SRP: Single Responsibility Principle, OCP: Open Closed Principle, LSP: Liskov Substitution Principle, ISP: Interface Segregation Principle, DIP: Dependency Inversion Principle
- DRY and KISS principles
- 遵循分层架构
- 在层内和层间遵循SOLID 原则。所有的设计模式都是以一种方式或另一种方式帮助实现这些原则。SRP: Single Responsibility Principle, OCP: Open Closed Principle, LSP: Liskov Substitution Principle, ISP: Interface Segregation Principle, DIP: Dependency Inversion Principle
- DRY 和 KISS 原则
These guidelines and principles are independent of any programming paradigm or language. However, OOP languages help implement these easier.
这些指导方针和原则独立于任何编程范式或语言。但是,OOP 语言有助于更轻松地实现这些。
回答by James from CppDepend Team
I really advice you to take a look at GRASP principles, it gives you a good design basis skills.
我真的建议你看看GRASP 原则,它给你一个很好的设计基础技能。