java应用程序由许多类所构成,是java实现面向对象应用程序的核心。类图主要描述java应用程序中各种类之间的相互静态关系,如类的继承、抽象、接口以及各种关联。要利用uml设计java应用程序,仅仅使用类图来描述这些静态关系,利用可视化工具,要实现java应用程序的代码自动生成,是远远不够的。我们还必须描述各种类相互之间的协作关系、动态关系,如时间序列上的交互行为。其中uml序列图就是用来描述类与类之间的方法调用过程(或消息发送)是如何实现的。
本文通过一个具体的应用程序的设计与实现过程,详细说明了利用uml序列图设计java应用程序,使得开发过程标准化、可视化,代码编程简单化。
我们要设计的应用程序flooringclient是用来计算在一定面积的表面上贴上规格化的地板砖或墙纸所需要的地板砖或墙纸材料的长度和价钱。该程序涉及到三个类:flooringclient、surface以及floor。其各自的类图以及程序代码分别如下
/*
* flooringclient.java
*
*/
class flooringclient {
public static void main(string[] args){
surface thesurface=new surface("margaret's floor",5,6);
flooring theflooring=new flooring("fitted carpet",24.50,5);
double noofmeters=theflooring.getnoofmeters(thesurface);
double price=theflooring.gettotalprice(thesurface);
system.out.println("you need "+noofmeters+" meters,price$ "+price);
}
}
/*
* surface.java
*
*/
class surface {
private string name; // for identification purposes
private double length;
private double width;
public surface(string initname, double initlength, double initwidth) {
name = initname;
length = initlength;
width = initwidth;
}
public string getname() {
return name;
}
public double getlength() {
return length;
}
public double getwidth() {
return width;
}
public double getarea() {
return width * length;
}
public double getcircumference() {
return 2 * (length + width);
}
}
/*
* flooring.java
*
*/
class flooring {
private static final double limit = 0.02; // limit for one more width
private string name; // for identification purposes
private double price; // price per meter
private double widthofflooring; // meter
public flooring(string initname, double initprice, double initwidth) {
name = initname;
price = initprice;
widthofflooring = initwidth;
}
public string getname() {
return name;
}
public double getpriceperm() {
return price;
}
public double getwidth() {
return widthofflooring;
}
/*
* we are going to calculate the amount which is needed to cover one surface.
* the flooring is always placed crosswise relative to the length of the surface.
* if you want to find the amount the other way, you have to change
* width and length in the surface argument.
*/
public double getnoofmeters(surface asurface) {
double lengthsurface = asurface.getlength();
double widthsurface = asurface.getwidth();
int noofwidths = (int)(lengthsurface / widthofflooring);
double rest = lengthsurface % widthofflooring;
if (rest >= limit) noofwidths++;
return noofwidths * widthsurface;
}
public double gettotalprice(surface asurface) {
return getnoofmeters(asurface) * price;
}
}
以上三个类之间的类图关系可以表示为如下图:
以下我们来详细分析类flooringclient是如何发送消息给其它类,而实现方法的调用过程。并如何用uml序列图来描述这一序列过程。
一、getnoofmeters()方法
让我们来看看是如何发送消息getnoofmeters()的。对象flooring要计算出需要多少米的材料才能贴满一定面积的表面,就需要对象flooring与对象surface之间相互作用。
flooringclient通过发送消息给getnoofmeters()对象flooring,在getnoofmeters()方法的代码中,flooring又发送消息给surface而得到length和width。
以上过程用uml序列图描述如下图:
uml序列图描述了消息是如何在给对象间发送的。下面我们来详细解释以上uml序列图的含义,通过上述序列图,我们得知有以下8个过程:
1. flooringclient新建一个对象thesurface
2. flooringclient新建一个对象theflooring
3. flooringclient发送一个消息给对象theflooring,并以thesurface为变量
4. theflooring发送一个消息getlength()给thesurface
5. thesurface发送一个回应给theflooring
6. theflooring发送一个消息getwidth ()给thesurface
7. thesurface发送一个回应给theflooring
8. theflooring发送一个回应给flooringclient
二、gettotalprice()方法
在flooringclient程序中,我们有如下语句:
double price=theflooring.gettotalprice(thesurface);
gettotalprice()方法为:
public double gettotalprice(surface asurface) {
return getnoofmeters(asurface) * price;
}
该过程用uml序列图描述如下图
三、同一个类的两个对象之间的交互
一个对象可以与同一个类的另一个对象交互从而完成程序所规定的任务。如果我们在surface类中增加一个比较面积的方法。程序代码为:
public int compareareas(surface theothersurface){
final double precision=0.00001;
double area1=getarea();
double area2=theothersurface.getarea();
if (math.abs(area2-area1)
else if(area1>area2) return ?c1;
else return 1;
}
在主程序flooringclient中,我们可以实现如下代码
surface surface1=new surface("a",5,4);
surface surface2=new surface("b",4,4);
int result=surface1.compareareas(surface2);
if (result<0) system.out.println(surface1.getname()+"is the smaller one");
else if (result>0) system.out.println(surface2.getname()+"is the smaller one");
else system.out.println("the surface has the same area");
从以上程序中可以看出,surface1与surface2发生交互从而得到结果result。首先它计算出自己的面积,然后计算出surface2的面积,最后再比较它们两个之间的面积的大小。
以上过程用uml序列图可以描述为下图:
以上详细说明了如何利用uml序列图来描述各类之间的对象或同一类不同之间的对象相互之间的交互序列过程。是java应用程序面向对象设计过程中的一个重要方面。
闽公网安备 35060202000074号