热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javaWeb应用程序中的静态层-Staticlayersinajavawebapplication

IambuildingasmallwebsiteforfunlearningusingafairlystandardWebServiceDataAccesslayer

I am building a small website for fun/learning using a fairly standard Web/Service/Data Access layered design.

我正在使用相当标准的Web /服务/数据访问分层设计构建一个有趣/学习的小型网站。

To save me from constantly having to create instances of my service layer/data access layer classes, I have made the methods in them all static. I shouldn't get concurrency issues as they use local variables etc and do not share any resources (things are simple enough for this at the moment).

为了避免不断创建服务层/数据访问层类的实例,我使其中的方法都是静态的。我不应该得到并发问题,因为他们使用局部变量等并且不共享任何资源(目前事情很简单)。

As far as I can see the only trade-off for this is that I am not really following a true OO approach, but then again it keeps the code much cleaner.

据我所知,唯一的权衡就是我并没有真正遵循真正的面向对象方法,但它再次使代码更加清晰。

Is there any reason this would not be a viable approach? What sort of problems might arise later on? Would it be better to have a "factory" class that can return me instances of the service and data layer classes as needed?

有什么理由这不是一个可行的方法吗?以后可能会出现什么样的问题?拥有一个可以根据需要返回服务和数据层类实例的“工厂”类会更好吗?

6 个解决方案

#1


4  

You know those rides at the amusement park where they say "please keep your hands and feet inside the ride at all times"? It turns out the ride is a lot more fun if you don't. The only real trade-off is that you're not really following a true keeping-your-hands-and-feet-inside-the-ride-at-all-times approach.

你知道那些在游乐园里的游乐设施,他们说“请随时把你的手脚放在车内”吗?事实证明,如果你不这样做,骑行会更有趣。唯一真正的权衡是你并没有真正遵循一个真正的保持 - 你的手和脚 - 在所有时间的骑行方法。

The point is this -- there is a reason you should follow a "true OO approach", just as there's a reason to keep your hands and feet inside the ride -- it's great fun until you start bleeding everywhere.

关键在于 - 有一个原因你应该遵循“真正的OO方法”,就像有理由将你的手脚放在车内 - 这是非常有趣的,直到你到处开始出血。

#2


4  

The way you describe it, this isn't the "wrong" approach per se but I don't really see the problem you're trying to avoid. Can't you just create a single instance of these business objects when the server starts up and pass them to your servlets as needed?

你描述它的方式,这本身并不是“错误的”方法,但我并没有真正看到你试图避免的问题。您是否只能在服务器启动时创建这些业务对象的单个实例,并根据需要将它们传递给您的servlet?

If you're ready to throw OO out the window you might want to check out the Singleton pattern as well.

如果您准备将OO抛出窗口,您可能还想查看Singleton模式。

#3


3  

Disadvantages:

  • You will be unable to write unit tests as you will be unable to write mock data access/business logic objects to test against.
  • 您将无法编写单元测试,因为您将无法编写模拟数据访问/业务逻辑对象进行测试。

  • You will have concurrency problems as different threads try to access the static code at the same time - or if you use synchronized static methods you will end up with threads queuing up to use the static methods.
  • 当不同的线程同时尝试访问静态代码时,您将遇到并发问题 - 或者如果使用同步静态方法,最终会排队使用静态方法的线程。

  • You will not be able to use instance variables, which will become a restriction as the code becomes more complex.
  • 您将无法使用实例变量,这将成为一个限制,因为代码变得更加复杂。

  • It will be more difficult to replace elements of the business or data access layers if you need to.
  • 如果需要,更换业务或数据访问层的元素将更加困难。

  • If you intend to write your application in this manner you would be better off using a language designed to work in this way, such as PHP.
  • 如果您打算以这种方式编写应用程序,最好使用以这种方式工作的语言,例如PHP。

You would be better off going for non-static business/data access layer classes by either:

通过以下任一方式,您最好选择非静态业务/数据访问层类:

  • Using the singleton pattern (creating a single instance of each class and sharing them among threads)...
  • 使用单例模式(创建每个类的单个实例并在线程之间共享它们)......

  • Or creating instances of the classes in each thread as and when they are needed.
  • 或者在需要时在每个线程中创建类的实例。

Keep in mind that each user/session connected to your application will be running in it's own thread - so your web application is inherently multi-threaded.

请记住,连接到应用程序的每个用户/会话都将在其自己的线程中运行 - 因此您的Web应用程序本质上是多线程的。

#4


2  

I don't really see the advantage to your design, and there are many things that could go wrong. You are saving a line of code, maybe? Here's some disadvantages to your approach:

我真的没有看到你的设计的优势,并且有很多事情可能会出错。你可以保存一行代码吗?这是您的方法的一些缺点:

  • You cannot easily replace implementations of your business logic
  • 您无法轻松替换业务逻辑的实现

  • You cannot defined instance variables to facilitate breaking up logic into multiple methods
  • 您无法定义实例变量以便将逻辑分解为多个方法

  • Your assumption that multi-threaded issues will not arise is almost certainly wrong
  • 您不会出现多线程问题的假设几乎肯定是错误的

  • You cannot easily mock them for testing
  • 你不能轻易地模仿他们进行测试

I really don't see that the omission of one line of code is buying you anything.

我真的没有看到遗漏一行代码就是为你买东西。

It's not really an "OO Design" issue, but more of an appropriateness. Why are you even using Java in such a procedural way? Surely PHP would be more appropriate to this kind of design (and actually save you time by not having to compile and deploy).

这不是一个“OO设计”问题,而是更合适的问题。为什么你甚至以这样的程序方式使用Java?当然,PHP更适合这种设计(实际上通过不必编译和部署来节省时间)。

I would just make your business layer non-static; it will make it so much easier for to maintain, change, and evolve your application.

我只是让你的业务层非静态;它将使维护,更改和改进您的应用程序变得更加容易。

#5


0  

You may have difficulty unit-testing your objects with this type of architecture. For example, if you have a layer of business objects that reference your static data access layer, it could be difficult to test the business layer because you won't be able to easily use mock data access objects. That is, when testing your business layer, you probably won't want to use the "real" methods in the data access layer because they will make unwanted changes to your database. If your data access layer was not static, you could provide mock data access objects to your business layer for testing purposes.

您可能难以使用此类架构对对象进行单元测试。例如,如果您有一层引用静态数据访问层的业务对象,则可能难以测试业务层,因为您将无法轻松使用模拟数据访问对象。也就是说,在测试业务层时,您可能不希望在数据访问层中使用“真实”方法,因为它们会对数据库进行不必要的更改。如果您的数据访问层不是静态的,则可以向业务层提供模拟数据访问对象以进行测试。

#6


-1  

I would think that you will have concurrency issues with all static methods with multiple users. The web layer will thread out concurrent users. Can all your static methods handle this? Perhaps, but won't they constantly be locked in queuing the requests in single file? I'm not sure, never tried your idea.

我认为对于具有多个用户的所有静态方法,您将遇到并发问题。 Web层将线程化并发用户。你的所有静态方法都可以处理吗?也许,但他们不会经常被锁定在单个文件中排队请求吗?我不确定,从未尝试过你的想法。


推荐阅读
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 如何搭建Java开发环境并开发WinCE项目
    本文介绍了如何搭建Java开发环境并开发WinCE项目,包括搭建开发环境的步骤和获取SDK的几种方式。同时还解答了一些关于WinCE开发的常见问题。通过阅读本文,您将了解如何使用Java进行嵌入式开发,并能够顺利开发WinCE应用程序。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • Tomcat/Jetty为何选择扩展线程池而不是使用JDK原生线程池?
    本文探讨了Tomcat和Jetty选择扩展线程池而不是使用JDK原生线程池的原因。通过比较IO密集型任务和CPU密集型任务的特点,解释了为何Tomcat和Jetty需要扩展线程池来提高并发度和任务处理速度。同时,介绍了JDK原生线程池的工作流程。 ... [详细]
  • 加密世界下一个主流叙事领域:L2、跨链桥、GameFi等
    本文介绍了加密世界下一个主流叙事的七个潜力领域,包括L2、跨链桥、GameFi等。L2作为以太坊的二层解决方案,在过去一年取得了巨大成功,跨链桥和互操作性是多链Web3中最重要的因素。去中心化的数据存储领域也具有巨大潜力,未来云存储市场有望达到1500亿美元。DAO和社交代币将成为购买和控制现实世界资产的重要方式,而GameFi作为数字资产在高收入游戏中的应用有望推动数字资产走向主流。衍生品市场也在不断发展壮大。 ... [详细]
  • 本文介绍了自动化测试专家Elfriede Dustin在2008年的文章中讨论了自动化测试项目失败的原因。同时,引用了IDT在2007年进行的一次软件自动化测试的研究调查结果,调查显示很多公司认为自动化测试很有用,但很少有公司成功实施。调查结果表明,缺乏资源是导致自动化测试失败的主要原因,其中37%的人认为缺乏时间。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文介绍了在多平台下进行条件编译的必要性,以及具体的实现方法。通过示例代码展示了如何使用条件编译来实现不同平台的功能。最后总结了只要接口相同,不同平台下的编译运行结果也会相同。 ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
author-avatar
小涵竹叶
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有