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++;
[1] [2] 下一页
|
|