FreeOZ论坛

标题: 一个小程序:网络聊天室,Java版 - 从设计到实现 [打印本页]

作者: key    时间: 20-6-2009 21:42
标题: 一个小程序:网络聊天室,Java版 - 从设计到实现
简单说明:

这是一个用来做assessment的项目,项目其实不复杂,要求也不高。
不过是我自己画蛇添足地把它当实际项目做出来而已。

项目最后写成一个 2.5k 行的Java程序,我用了2.5天时间来写代码,
前面几天在写开发文档。因为时间问题,文档只完成了70%左右(文档不是要求),
也直接导致了后面的程序的质量。

总的来说,我最满意的地方是通过前期的分析,代码的重用性得到保证,扩展性也
还可以。基于这个项目,我可以整理出一个小的程序库,这也是我最想要的东西。

我自己不满意的地方主要是在设计的时候对线程的考虑过少,在实现的时候才发现
一些问题,但因为时间太紧,代码量不小,所以没有做得很好。

下面是我的一系列文档。有兴趣的朋友不妨自己玩一玩,不论是根据题目来实现,
还是根据我的文档来写代码,都可能是一件有趣的事。

如果你对于我的设计和实现有兴趣,不妨和我联系。
作者: key    时间: 20-6-2009 21:54
标题: Requirements
Server
The server application should have a window to show the names of the people currently using the application and the messages they send. Also, for convenience in testing, show the address of the server host. A button could be included to shut the server down.
[attach]68745[/attach]

Client
The client application should start up with a login window where the user enters the address of the server and a name.
[attach]68746[/attach]

After the client user logs on, the server window will show the user name and the client window will change to a different layout showing people and messages as well as a tet field for entering messages.

The following shows the windows for the server and two clients after two users have logged on and sending a few messages.
[attach]68747[/attach]

Note that: all these windows usually should run on different computer.

New User
When a new user logs in, there will be an updated list of user names in the server window and all client windows. The new user will see only the messages sent after they join the chat session.

When a user exits their client application, their name should be removed from the lists of people in the server and all remaining client applications.

Dialog boxes
Dialog boxes should be used to show users any problems. Some examples are:

A user does not enter a name or address in the login window.
The client application cannot connect to the server.
There is already someone in the chat session with the same name.

If the server application shut down when there are still clients running, each client application should inform their user with a dialog box and then terminate.

[ 本帖最后由 key 于 20-6-2009 21:55 编辑 ]
作者: key    时间: 20-6-2009 22:05
标题: Use Cases
Start Server
[attach]68748[/attach]
Process: The system administrator starts a chat room server.[Referring to requirement document Page 1, Server session.]
Pre-conditions:
       

Post-conditions:



Alternatives:



Close Server
[attach]68749[/attach]
Process: the System Administrator closes the chat room server, and the corresponding client will receive a message and close their window accordingly. [Referring to requirement document P3, Dialog boxes]
Pre-conditions:

Post-conditions:



Alternatives:
None

User Login
[attach]68750[/attach]
Process: A chat room user login the chat room. The chat room user gives the server’s hostname or IP address, as well as his/her chat name, then logs into the chat room. When the chat user logs in, the server will greed with him/her. [Referring to requirement document Page 1, 2 and 3]
Pre-conditions:




Post-conditions:




Alternatives:





Send message
[attach]68751[/attach]
Process: A chat room user send a message within a chatroom. This message displays on this user’s chat screen. The server gets the message and display on the server’s message screen. All the other clients get this message and display on their own screen.
Pre-conditions:

Post-conditions:

Alternatives:


Exit Chat Room
[attach]68752[/attach]
Process: A chatroom user exists the chatroom. The user name will remove from the server’s People list and so forth the other clients’ People lists.
Pre-conditions:

Post-conditions:


Alternatives:

作者: key    时间: 20-6-2009 22:08
标题: Conceptual Model
[attach]68753[/attach]

老实说,这个图也太concept了
作者: key    时间: 20-6-2009 22:12
标题: System Architecture
login
[attach]68754[/attach]
* ChatServDaemon represents a network server daemon, listening on a specific port and receiving message from the client side.
* NetworkController uses NetworkWriterThread and NetworkReaderThread to handle network events and operations. There are three types of NetworkController: ChatClientNetworkController, ChatServerNetworkController, ChatOnlineClientNetworkController.
** ChatClientNetworkController: to help the ChatClient to handle all network communication between client and server.
** ChatServerNetworkController: to help the ChatServerDaemon listening on a specific port, receiving connection message from the client.
** ChatOnlineClientNetworkController: to help the UserOnlineController to handle all back and forth message between chat client and chat server.
* ChatClient connects to ChatServDaemon via NetworkController, then composes a login message (ChatMessage(login)) with MessageController, and sends this login message via NetworkController to ChatServDaemon.
* ChatServDaemon receives the client login message and decodes with MessageController, then forwards to ChatServApp.
* ChatServApp is the centre of all server logic. ChatServApp validates the new comer with LoginController, and then checks whether there is a user name conflict with UserOnlinerController. If everything works fine, ChatServApp add a new user and a new ChatOnlineClientNetworkController to UserOnlineController, and lets the UserOnlinerController update other users with a NetworkController.

send message
[attach]68755[/attach]
* ChatClient composes a new chat message by MessageController, and then send to chat server via NetworkController (ChatClientNetworkController).
* The chat message reaches the server on ChatOnlineClientNetworkController, which will notify UserOnlineController about the message.
* UserOnlineController receives the message and decodes with MessageController, then forward the message object to ChatServApp. ChatServApp updates ChatServWin.
* UserOnlineController updates all other clients with the new message.
作者: key    时间: 20-6-2009 22:18
标题: Sequence Diagrams - Start Server
[attach]68756[/attach]
作者: key    时间: 20-6-2009 22:19
标题: Sequence Diagrams - Close Server
Server side
[attach]68757[/attach]

Client side
[attach]68758[/attach]
作者: key    时间: 20-6-2009 22:20
标题: Sequence Diagrams - User Login
client side
[attach]68761[/attach]
server side
[attach]68762[/attach]
update others
[attach]68763[/attach]
作者: key    时间: 20-6-2009 22:21
标题: Sequence Diagrams - Send Message
client side
[attach]68764[/attach]
server side
[attach]68765[/attach]
作者: key    时间: 20-6-2009 22:24
标题: Sequence Diagrams - Exit Chatroom
client side
[attach]68766[/attach]
server side
[attach]68767[/attach]
作者: key    时间: 20-6-2009 22:30
标题: class diagram - server
[attach]68770[/attach]
作者: key    时间: 20-6-2009 22:30
标题: class diagram - gui
[attach]68772[/attach]
作者: key    时间: 20-6-2009 22:31
标题: class diagram - Client Related Class Diagram
[attach]68773[/attach]
作者: key    时间: 20-6-2009 22:31
标题: class diagram - Message Class Diagram
[attach]68774[/attach]
作者: key    时间: 20-6-2009 22:32
标题: class diagram - network
[attach]68775[/attach]
作者: key    时间: 20-6-2009 22:35
标题: 有没有人希望我贴代码?
2.5k的代码,35个.java文件
作者: key    时间: 20-6-2009 22:42
标题: 程序运行结果
[attach]68779[/attach]
作者: NoChoice    时间: 21-6-2009 09:47
谢谢分享, 代码可以打个包放上来。

我以前上学的时候也做过一个路由器最短路经模拟也是用java写的,7个路由器在一个拓扑网络里面动态相互交换routing table,任何一个路由器上面的机器发信息到指定地址,路由器要负责找到最短路经,如果有路由器down了或者reset,其他路由器要更新routing table,并要求动态计算最短路经
作者: key    时间: 21-6-2009 10:40
原帖由 NoChoice 于 21-6-2009 09:47 发表
谢谢分享, 代码可以打个包放上来。

我以前上学的时候也做过一个路由器最短路经模拟也是用java写的,7个路由器在一个拓扑网络里面动态相互交换routing table,任何一个路由器上面的机器发信息到指定地址,路由器要 ...


这道是算法题吧,摸拟RIP?
我打算把代码发到google code之类的地方去,然后链接过来。
等我写完现在这份垃圾报告再说。
作者: procoder    时间: 21-6-2009 13:17
厉害呀
作者: felix100    时间: 21-6-2009 16:34
学习一下。
作者: NoChoice    时间: 21-6-2009 19:04
原帖由 key 于 21-6-2009 10:40 发表


这道是算法题吧,摸拟RIP?
我打算把代码发到google code之类的地方去,然后链接过来。
等我写完现在这份垃圾报告再说。


是rip模拟
期待lz连接
作者: MillerYang    时间: 22-6-2009 14:13
key太强大!
本帖做国内本科论文足以。。。
作者: key    时间: 22-6-2009 17:47
标题: 项目源代码已经上传Google Code
http://code.google.com/p/brave-netchat/

这套代码写得很匆忙,也没有时间做unit test,
所以多线程部分一定会有bug。
有兴趣的同学不妨帮我改一下。
作者: key    时间: 22-6-2009 17:49
原帖由 procoder 于 21-6-2009 13:17 发表
厉害呀


pro同学过奖了,
你的uml文档给了不少启示给我,谢谢先。

另外,conceptual model那里我不是太满意,
理论上似乎还需要指出entity之间的relationship吧?
作者: calous    时间: 16-12-2009 22:31
你是用来做CDR的?
作者: bthy    时间: 10-6-2010 19:54
程序应该是很简单的东西啊,这类型的作业基本属于本科第一个作业的内容,弄弄就好了呀? 楼主弄得文档这么全,厉害
作者: bthy    时间: 10-6-2010 20:11
不过MVC 和抽象基类还有EXCEPTION HANDLER到是分得很清楚, 很好的习惯  如果没看错,楼主的SOCKET 写得好有CORBA的味道哦. 有BASE,HELPER

[ 本帖最后由 bthy 于 10-6-2010 20:16 编辑 ]
作者: key    时间: 19-6-2010 07:56
这套东西的确有着眼MVC的分离,不过好象没有关于Exception handler部分吧?
我不会corba,哈哈。。。。想不到还有这个味着,就象黄蓉在压鬼岛做的羊腿一样。

我在这个课程设计中,最重要关注的是如何从sequence diagram变转到class diagram,一个是对应logical design,另一个则对应于physical design。
concept layer design用的是use cases diagram,还有一个叫system architecture的东西,是整体框架的考虑。
我喜欢activity diagram,在这里没有画出来。我通常从activity diagram开始画起,然后再考虑use cases。use cases的主要目的我觉得是系统模块的整理和定义,
当然是concept layer的定义了。

一直以来,如何平滑建立类图是我一直在思考的问题。我认为,拿起UML就直接类图,这和拿起电脑就直接coding没有质的区别。
我推崇的是传统的从概念到逻辑,再到物理实现的过程。就算是现在的OOA/OOD时代,这三步还是需要做的,只是这三个步骤之间的跨越间距比以前小很多而已。

Sequence diagram是一个很容易画出来的东西,我想做到的是,通过Sequence Diagram的进化,最后获得一个类的接口图,把主要的消息结构、类接口都定义出来,
然后再汇总在类图,以完成从逻辑设计到物理设计的进化。这个目的我想我在这套东西中已经做到了。

其实现在提介的DI/IoC概念又为这种逻辑设计到物理设计铺平了道路,我正在思考如何应用起来。
还有就是长期View页面实现的非程序员友好性(non-programmer-friendly)也让我们难以把OOA/OOD进行到底,
但GWT, Apache Click这类Web实现模型有可能帮我们解决这类问题。

对比Spring MVC,View层其实还有大量的代码无法让程序员来参与,或者参与得很痛苦。相比Apache Click,采用的是Page Component的概念来实现Web,
使得Web上的每一个东西都是程序员能控制的。而GWT走的路就更深远了,把整个界面实现都转化成程序语言。

有了这些系统工具的支撑,我们能做的事就更多了。

原帖由 bthy 于 10-6-2010 20:11 发表
不过MVC 和抽象基类还有EXCEPTION HANDLER到是分得很清楚, 很好的习惯  如果没看错,楼主的SOCKET 写得好有CORBA的味道哦. 有BASE,HELPER





欢迎光临 FreeOZ论坛 (https://www.freeoz.org/ibbs/) Powered by Discuz! X3.2