国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

利用UML序列圖設(shè)計(jì)Java應(yīng)用程序詳解

2019-11-18 12:20:06
字體:
供稿:網(wǎng)友

  java應(yīng)用程序由許多類所構(gòu)成,是Java實(shí)現(xiàn)面向?qū)ο髴?yīng)用程序的核心。類圖主要描述Java應(yīng)用程序中各種類之間的相互靜態(tài)關(guān)系,如類的繼續(xù)、抽象、接口以及各種關(guān)聯(lián)。要利用UML設(shè)計(jì)Java應(yīng)用程序,僅僅使用類圖來描述這些靜態(tài)關(guān)系,利用可視化工具,要實(shí)現(xiàn)Java應(yīng)用程序的代碼自動(dòng)生成,是遠(yuǎn)遠(yuǎn)不夠的。我們還必須描述各種類相互之間的協(xié)作關(guān)系、動(dòng)態(tài)關(guān)系,如時(shí)間序列上的交互行為。其中UML序列圖就是用來描述類與類之間的方法調(diào)用過程(或消息發(fā)送)是如何實(shí)現(xiàn)的。
  本文通過一個(gè)具體的應(yīng)用程序的設(shè)計(jì)與實(shí)現(xiàn)過程,具體說明了利用UML序列圖設(shè)計(jì)Java應(yīng)用程序,使得開發(fā)過程標(biāo)準(zhǔn)化、可視化,代碼編程簡單化。
  我們要設(shè)計(jì)的應(yīng)用程序FlooringClient是用來計(jì)算在一定面積的表面上貼上規(guī)格化的地板磚或墻紙所需要的地板磚或墻紙材料的長度和價(jià)錢。該程序涉及到三個(gè)類:FlooringClient、Surface以及Floor。其各自的類圖以及程序代碼分別如下
  
 利用UML序列圖設(shè)計(jì)Java應(yīng)用程序詳解(圖一)

  /*
  
  * 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);
  
  }
  
  }
  
  
 利用UML序列圖設(shè)計(jì)Java應(yīng)用程序詳解(圖二)

  /*
  * 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);
  
  }
  
  }
  
  
 利用UML序列圖設(shè)計(jì)Java應(yīng)用程序詳解(圖三)

  /*
  
  * 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;
  
  }
  
  }
  
 利用UML序列圖設(shè)計(jì)Java應(yīng)用程序詳解(圖四)

  以上三個(gè)類之間的類圖關(guān)系可以表示為如下圖:
   利用UML序列圖設(shè)計(jì)Java應(yīng)用程序詳解(圖一)
  以下我們來具體分析類FlooringClient是如何發(fā)送消息給其它類,而實(shí)現(xiàn)方法的調(diào)用過程。并如何用UML序列圖來描述這一序列過程。
  一、getNoOfMeters()方法
  讓我們來看看是如何發(fā)送消息getNoOfMeters()的。對(duì)象Flooring要計(jì)算出需要多少米的材料才能貼滿一定面積的表面,就需要對(duì)象Flooring與對(duì)象Surface之間相互作用。
  FlooringClient通過發(fā)送消息給getNoOfMeters()對(duì)象Flooring,在getNoOfMeters()方法的代碼中,F(xiàn)looring又發(fā)送消息給Surface而得到length和width。
  以上過程用UML序列圖描述如下圖:
  
 利用UML序列圖設(shè)計(jì)Java應(yīng)用程序詳解(圖五)

  UML序列圖描述了消息是如何在給對(duì)象間發(fā)送的。下面我們來具體解釋以上UML序列圖的含義,通過上述序列圖,我們得知有以下8個(gè)過程:
  1.   FlooringClient新建一個(gè)對(duì)象theSurface
  2.   FlooringClient新建一個(gè)對(duì)象theFlooring
  3.   FlooringClient發(fā)送一個(gè)消息給對(duì)象theFlooring,并以theSurface為變量
  4.   theFlooring發(fā)送一個(gè)消息getLength()給theSurface
  5.   theSurface發(fā)送一個(gè)回應(yīng)給theFlooring
  6.   theFlooring發(fā)送一個(gè)消息getWidth ()給theSurface
  7.   theSurface發(fā)送一個(gè)回應(yīng)給theFlooring
  8.   theFlooring發(fā)送一個(gè)回應(yīng)給FlooringClient
  
  二、getTotalPrice()方法
  在FlooringClient程序中,我們有如下語句:
  double price=theFlooring.getTotalPrice(theSurface);
  
  getTotalPrice()方法為:
  
  public double getTotalPrice(Surface aSurface) {
  
  return getNoOfMeters(aSurface) * price;
  
  }
  該過程用UML序列圖描述如下圖
  
 利用UML序列圖設(shè)計(jì)Java應(yīng)用程序詳解(圖六)

  三、同一個(gè)類的兩個(gè)對(duì)象之間的交互
  一個(gè)對(duì)象可以與同一個(gè)類的另一個(gè)對(duì)象交互從而完成程序所規(guī)定的任務(wù)。假如我們?cè)赟urface類中增加一個(gè)比較面積的方法。程序代碼為:
  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 –1;
  
  else return 1;
  
  }
  在主程序FlooringClient中,我們可以實(shí)現(xiàn)如下代碼
  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發(fā)生交互從而得到結(jié)果result。首先它計(jì)算出自己的面積,然后計(jì)算出surface2的面積,最后再比較它們兩個(gè)之間的面積的大小。
  以上過程用UML序列圖可以描述為下圖:
  
利用UML序列圖設(shè)計(jì)Java應(yīng)用程序詳解(圖七)

  以上具體說明了如何利用UML序列圖來描述各類之間的對(duì)象或同一類不同之間的對(duì)象相互之間的交互序列過程。是Java應(yīng)用程序面向?qū)ο笤O(shè)計(jì)過程中的一個(gè)重要方面。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 锡林浩特市| 石泉县| 涟源市| 九江县| 万盛区| 延吉市| 泾阳县| 理塘县| 天水市| 扎鲁特旗| 抚宁县| 宜阳县| 山东省| 姚安县| 磐安县| 同德县| 泌阳县| 灌云县| 布尔津县| 伊金霍洛旗| 石河子市| 长治市| 望城县| 抚顺县| 湄潭县| 东辽县| 郴州市| 清镇市| 五原县| 淮北市| 开平市| 永兴县| 隆回县| 布尔津县| 东乌| 陇西县| 平江县| 连山| 紫金县| 景宁| 霍城县|