3

I'm a little confused, I'm developing a program, the program consist in two parts, the server and the clients, there are groups, places, messages... stored in the server, and the clients has to connect with it. I've design the use cases diagram, the activity diagrams, and I have design the class diagram too.

The thing is that I want to implement the server in a mysql tables for storing the users, groups, places... users in groups... so I've designed a E-R diagram consisting in 6 tables, but the problem is that I think that my class diagram and my ER diagram looks too similar, I mean, I think I'm not doing things right because I have a class for each table practically, and when I have to extract all the users on my system, do I have to convert all the rows into objects at first and write in the database for each object modified?

The easy choice for me would be to base my whole application only in the database, and making a class to extract and insert data in it, but I have to follow the UML specification and I'm a little confused what to do with the class diagram, because the books I have read say that I have to create a class for each "entity" of my program.

Sorry for my bad English.

Thank you.

4

1 回答 1

2

我之前已经解决了几次这个问题,我已经通过这种方式解决了它:

1.)选择(继承自Vector,它使用标准类存储所有行,具体取决于您使用的语言)结构:

ID(以及用于识别您的选择的 ID)(选择的所有元素,它是您的 Vector 类的子类都是行)

行号:整数

列号:int

使用过滤器:字符串

usedGroupBy:字符串

usedHavingCaluse:字符串

usedOrderBy:字符串

表名:字符串

获取ID()

getRowNumber(): 整数

getColumnNumber(): 整数

getUsedFilter(): 字符串

getUsedGroupBy(): 字符串

getUsedHavingClause(): 字符串

getUsedOrderBy(): 字符串

获取表名():字符串

选择(表名:字符串,过滤器:字符串,groupBy:字符串,haveCaluse:字符串,orderBy:字符串,列:向量)

2.) 我有一个与数据库直接通信的类,我们称之为DataAccessLayer。让我们看看这个类的结构:

数据访问层

连接(...):布尔值

断开连接(...):布尔值

使用(数据库名称:字符串):布尔值

selectedData: Vector(其实这是一组Select的)

createSelect(tableName: String, filter: String, groupBy: String, havingCaluse: String, orderBy: String, columns: Vector): boolean(判断是否成功)

删除选择(ID):布尔值

插入(表名:字符串,列:向量,值:向量):布尔值

更新(tableName:字符串,columnsToSet:向量,值:向量,过滤器:字符串):布尔值

delete(tableName: String, filter: String): int(删除了多少行,如果发生异常则为-1,或者只是简单地将异常抛出到更高级别)

//创建/删除表/视图/数据库/约束也可以实现,我只是//懒惰这样做,因为我相信你已经理解了这个想法

数据访问层()

在前两个步骤发生后,您可以使用两个类处理任何数据库查询(实际上 Select 的功能也可以放入 DataAccessLayer 中,使两者中的一个类,但这样更优雅),但是,您可能想要为几张桌子处理一些额外的事情。解决方案很简单,每当你发现一个表难以与这些一起使用时,你只需要从 DataAccessLayer 继承并重新定义你想要重新定义的内容,所以在 DataAccessLayer 中你应该只使用 protected 和 public 修饰符而忘记 private 修饰符。因此,关系将是:

选择 1 <-> n DataAccessLayer

ClassInheritedFromDataAccessLayer 扩展 DataAccessLayer

前端使用 ClassInheritedFromDataAccessLayer1, ..., ClassInheritedFromDataAccessLayern, DataAccessLayer。

这样您的项目将: - 可管理 - 有序 - 易于计划 - 易于实施 - 易于修改 - 易于其他人理解

我希望这有帮助,

问候。

于 2010-06-02T04:26:37.770 回答