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?
有什么理由这不是一个可行的方法吗?以后可能会出现什么样的问题?拥有一个可以根据需要返回服务和数据层类实例的“工厂”类会更好吗?
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方法”,就像有理由将你的手脚放在车内 - 这是非常有趣的,直到你到处开始出血。
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
Disadvantages:
您将无法编写单元测试,因为您将无法编写模拟数据访问/业务逻辑对象进行测试。
当不同的线程同时尝试访问静态代码时,您将遇到并发问题 - 或者如果使用同步静态方法,最终会排队使用静态方法的线程。
您将无法使用实例变量,这将成为一个限制,因为代码变得更加复杂。
如果需要,更换业务或数据访问层的元素将更加困难。
如果您打算以这种方式编写应用程序,最好使用以这种方式工作的语言,例如PHP。
You would be better off going for non-static business/data access layer classes by either:
通过以下任一方式,您最好选择非静态业务/数据访问层类:
使用单例模式(创建每个类的单个实例并在线程之间共享它们)......
或者在需要时在每个线程中创建类的实例。
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应用程序本质上是多线程的。
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:
我真的没有看到你的设计的优势,并且有很多事情可能会出错。你可以保存一行代码吗?这是您的方法的一些缺点:
您无法轻松替换业务逻辑的实现
您无法定义实例变量以便将逻辑分解为多个方法
您不会出现多线程问题的假设几乎肯定是错误的
你不能轻易地模仿他们进行测试
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.
我只是让你的业务层非静态;它将使维护,更改和改进您的应用程序变得更加容易。
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.
您可能难以使用此类架构对对象进行单元测试。例如,如果您有一层引用静态数据访问层的业务对象,则可能难以测试业务层,因为您将无法轻松使用模拟数据访问对象。也就是说,在测试业务层时,您可能不希望在数据访问层中使用“真实”方法,因为它们会对数据库进行不必要的更改。如果您的数据访问层不是静态的,则可以向业务层提供模拟数据访问对象以进行测试。
-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层将线程化并发用户。你的所有静态方法都可以处理吗?也许,但他们不会经常被锁定在单个文件中排队请求吗?我不确定,从未尝试过你的想法。