java中的设计模式 [英] Design patterns in java

查看:90
本文介绍了java中的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的初学者。我想学习我所学到的知识并创建一个简单的库存系统,更具体地说是一个资产管理系统,可以跟踪IT硬件类型(笔记本电脑,工作站,显示器等),然后为其分配值。我不认为这应该太难,但我坚持一件事。使用设计模式时,我不确定使用哪种模式。我认为这对我来说是一个有趣的项目。



我认为装饰模式很明显可以使用。这样我就可以创建一个对象(例如笔记本电脑)并用品牌,操作系统,内存,高清大小等来装饰它。我可以使用工厂模式,以便封装每个特定对象,这样如果我正在创建(添加)一台显示器,我不要求用户添加OS / RAM / HD尺寸等。



您认为这样做的选择是什么样的?你能提供的任何帮助或建议都会很棒。



我尝试过的事情:



还没有,因为我还在规划阶段

I am a relative beginner to programming. I want to take what I have learned and create a simple inventory system, more specifically an asset management system that could track IT hardware types (laptop, workstation, monitor, etc) and then assign values to it. I don't think this should be too difficult but I am stuck on one thing. When using design patterns, I am not sure which ones to use. I think this would be a fun project for me to work on.

I think a decorator pattern would be an obvious one to use. That way I could create an object (laptop for example) and decorate it with brand, OS, RAM, HD size, etc. I could maybe use a factory pattern so that each specific object is encapsulated so that if I am creating (adding) a monitor, I am not requiring the user to add the OS/RAM/HD size etc.

What do you think would be a good selection of patterns for this? Any help or suggestions you could offer would be great.

What I have tried:

Nothing yet as I am still in the planning phases

推荐答案

简答:做任何你想做的事,只是不要过度设计软件,这变得一团糟,管理不那么有用。



我首先要问的是,谁将使用该软件?消费者在自己的机器上,或者是一群消费API的用户,或者我的网络应用程序?所以第一件事就是桌面应用程序或网络应用程序?



你谈到的模式是创建对象,然后提供它们。这仅仅是库存管理系统的一部分。您需要一堆提供者模式,控制反转(或至少依赖注入)来解耦服务和模块,这将有助于您以后更好地测试软件。面向服务的应用程序架构将帮助您,以便在Web应用程序上托管它时,服务可以独立于其他模块进行扩展/缩小。



java - 什么是依赖注入? - 堆栈溢出 [ ^ ]我记得一个非常有用的库是 google / guice:Guice(发音为'juice')。 [ ^ ],您可以考虑将其检出。



Short answer: Do whatever you want to, just don't over engineer the software, that is becomes a mess to manage and less useful.

The first thing I would ask myself is, who is going to use the software? A consumer on his own machine, or a bunch of users consuming the API, or my web app? So the first thing comes in, is it a desktop app, or a web app?

The patterns you talk about are to create the objects, and then provide them over. That is merely a part of the "inventory management system". You would require a bunch of provider patterns, inversion of control (or dependency injection at least) to decouple the services and modules, that will help you in order to better test the software later on. The service oriented architecture of the application will help you in case you are going to host it on the web app, the services can scale up/down independent of other modules.

java - What is Dependency Injection? - Stack Overflow[^] I remember a really useful library was google/guice: Guice (pronounced 'juice').[^], you can consider checking it out.

引用:

我可以使用工厂模式,以便每个特定对象是封装的,如果我正在创建(添加)一个监视器,我不要求用户添加OS / RAM / HD大小等。

I could maybe use a factory pattern so that each specific object is encapsulated so that if I am creating (adding) a monitor, I am not requiring the user to add the OS/RAM/HD size etc.

我认为你可以从服务或其他东西中选择硬件组件,知道其他硬件组件有什么关系 - 如果他们选择8 GB的RAM,那么选择64位CPU,32位将无法工作以及更多的东西。只是一个工厂不会工作,可能会使事情变得复杂,并包含一堆 if ... else ,就像我刚提到的那样。在这种情况下,类似数据库的服务将是有用的,它将能够关联资源并构建组件。它不是关于模式,而是关于业务逻辑。



因此,简而言之,它是一个很长的列表,您可以检查和实现这些模式,但请记住,只有实现这一点才能使软件受益。不要添加每个模式,只会使它变得复杂。

I think you can just select the hardware component from a service or something, that knows what other hardware components relate—if they choose a RAM of 8 GB, go for a 64-bit CPU, 32-bit won't work and much more stuff. Just a factory will not work and might make things complicated, and contain a bunch of if...else, like the one I just mentioned. In this case, a database-like service would be useful that will be able to related the resources and build up the components. It is less about the pattern, more about the business logic.

So, in a nutshell, it is a long list of the patterns that you can check and implement, but remember, only implement if that benefits the software. Do not add every pattern, that will only make it complex.


这篇关于java中的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆