• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    利用UML序列圖設計Java應用程序詳解

    發布: 2007-5-25 11:35 | 作者: 未知 | 來源: 系統分析之窗 | 查看: 40次 | 進入軟件測試論壇討論

    領測軟件測試網

    利用UML序列圖設計Java應用程序詳解 

    作者: 龔赤兵
    From ZDNet China
    Tuesday, April 15 2003 10:48 AM 

    UML已成為面向對象設計的標準圖形化工具,在UML定義的各種圖中,本文主要講解序列圖。 

    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)<precision) return 0;

    else if(area1>area2) return –1;

    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應用程序面向對象設計過程中的一個重要方面。


    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>