利用UML类图设计Java应用程序详解(二)

类别:Java 点击:0 评论:0 推荐:
在第一部分中,我们实现了5个类。在本部分中,我们接着说明如何利用UML类图来设计余下的各个类。为减少篇幅,本部分着重讲解UML类图及应用,对Java实现代码不再详细描述。

  六、CGPoint类
  CGPoint类说明了如何利用非抽象类扩展抽象类。CGPoint类是CGObject的子类,CGPoint类扩展了CGObject类,CGPoint类没有再它所继承的变量中增加变量,它所声明的方法只有构造函数和要求它实现的抽象方法。其类图如下:
  
  Java实现代码为:
  // CGPoint.java
  public class CGPoint extends CGObject {
  // Method declarations
  public CGPoint(int x, int y,char ch) {
  location = new Point(x,y);
  drawCharacter = ch;
  }
  public CGPoint(int x, int y) {
  this(x,y,'+');
  }
  public CGPoint(Point p) {
  this(p.getX(),p.getY(),'+');
  }
  public CGPoint(Point p,char ch) {
  this(p.getX(),p.getY(),ch);
  }
  public void display(PrintCGrid grid) {
  grid.setCharAt(drawCharacter,location);
  }
  public void describe() {
  System.out.print("CGPoint "+String.valueOf(drawCharacter)+" ");
  System.out.println(location.toString());
  }
  }
  七、CGBox类
  CGBox类也扩展了CGObject类。CGBox类提供了在网格上显示矩形的附加变量。CGBox类的类图如下:
  
  相应的代码为:
  // CGBox.java
  public class CGBox extends CGObject {
  // Variable declarations
  protected Point lr; // Lower right corner of a box
  // Method declarations
  public CGBox(Point ulCorner, Point lrCorner,char ch) {
  location = ulCorner;
  lr = lrCorner;
  drawCharacter = ch;
  }
  public CGBox(Point ulCorner, Point lrCorner) {
  this(ulCorner,lrCorner,'#');
  }
  public void display(PrintCGrid grid) {
  int width = lr.getX() - location.getX() + 1;
  int height = lr.getY() - location.getY() + 1;
  Point topRow = new Point(location);
  Point bottomRow = new Point(location.getX(),lr.getY());
  for(int i=0; i<width; ++i) {
  grid.setCharAt(drawCharacter,topRow);
  grid.setCharAt(drawCharacter,bottomRow);
  topRow = topRow.add(1,0);
  bottomRow = bottomRow.add(1,0);
  }
  Point leftCol = new Point(location);
  Point rightCol = new Point(lr.getX(),location.getY());
  for(int i=0;i>height;++i){
  grid.setCharAt(drawCharacter,leftCol);
  grid.setCharAt(drawCharacter,rightCol);
  leftCol = leftCol.add(0,1);
  rightCol = rightCol.add(0,1);
  }
  }
  public void describe() {
  System.out.print("CGBox "+String.valueOf(drawCharacter)+" ");
  System.out.println(location.toString()+" "+lr.toString());
  }
  }
  八、CGText类
  CGText类是CGObject中的第三个子类。其类图与代码分别如下:
  
  // CGText.java
  public class CGText extends CGObject {
  // Variable declarations
  String text;
  // Method declarations
  public CGText(Point p,String s) {
  location = p;
  drawCharacter = ' ';
  text = s;
  }
  public void display(PrintCGrid grid) {
  Point p = new Point(location);
  for(int i=0;i<text.length();++i){
  grid.setCharAt(text.charAt(i),p);
  p = p.add(1,0);
  }
  }
  public void describe() {
  System.out.println("CGText "+location.toString()+" "+text);
  }
  }>
  以下是CGObject类、CGPoint类、CGBox类、CGText类及Point类之间的相互关系。注意CGObject类是抽象类,其类名用斜体表示。
  
  九、KeyboardInput类
  KeyboardInput类扩展了Java API的DataInputStream类,用来提供获取用户键盘输入的一系列常用的简单方法。其类图设计为:
  
  代码为:
  import java.lang.System;
  import java.io.DataInputStream;
  import java.io.InputStream;
  import java.io.IOException;
  //KeyboardInput.java
  public class KeyboardInput extends DataInputStream {
  public KeyboardInput(InputStream inStream) {
  super(inStream);
  }
  public char getChar() throws IOException {
  String line = readLine();
  if(line.length()==0) return ' ';
  return line.charAt(0);
  }
  public String getText() throws IOException {
  String line = readLine();
  return line;
  }
  public int getInt() throws IOException {
  String line = readLine();
  Integer i = new Integer(line);
  return i.intValue();
  }
  public Point getPoint() throws IOException {
  System.out.print(" x-coordinate: ");
  System.out.flush();
  int x = getInt();
  System.out.print(" y-coordinate: ");
  System.out.flush();
  int y = getInt();
  return new Point(x,y);
  }
  }
  十、CDrawApp类
  主程序由CDrawApp类所构成。它包含main()方法,main()方法建立类CDraw的对象,然后调用该对象的run()方法。其中CDraw类属于内部类,当然你也可以将它单独作为一个类文件编辑、编译,其效果是一样的。
  其中类与内部类之间的关系,用关联关系来表达,外部类用一个十字交叉圆圈表示,箭头指向内部类。如下图所示:
  
  其代码实现为:
  import java.lang.System;
  import java.io.DataInputStream;
  import java.io.IOException;
  //CDrawApp.java
  class CDrawApp {
  public static void main(String args[]) throws IOException {
  CDraw program = new CDraw();
  program.run();
  }
  }
  class CDraw {
  // Variable declarations
  static KeyboardInput kbd = new KeyboardInput(System.in);
  BorderedPrintCGrid grid;
  // Method declarations
  CDraw() {
  grid = new BorderedPrintCGrid();
  }
  void run() throws IOException {
  boolean finished = false;
  do {
  char command = getCommand();
  switch(command){
  case 'P':
  addPoint();
  System.out.println();
  break;
  case 'B':
  addBox();
  System.out.println();
  break;
  case 'T':
  addText();
  System.out.println();
  break;
  case 'U':
  grid.deleteLastObject();
  System.out.println();
  break;
  case 'C':
  grid.clearGrid();
  System.out.println();
  break;
  case 'S':
  grid.show();
  break;
  case 'X':
  finished = true;
  default:
  System.out.println();
  }
  } while (!finished);
  }
  char getCommand() throws IOException {
  System.out.println("CDrawApp P - Add a Point U - Undo Last Add");
  System.out.print ("Main Menu B - Add a Box C - Clear Grid");
  System.out.println(" X - Exit CDrawApp");
  System.out.print (" T - Add Text S - Show Grid ");
  System.out.print (" Enter command: ");
  System.out.flush();
  return Character.toUpperCase(kbd.getChar());
  }
  void addPoint() throws IOException {
  System.out.println("Add Point Menu");
  System.out.println(" Location:");
  Point p = kbd.getPoint();
  System.out.print(" Character: ");
  System.out.flush();
  char ch = kbd.getChar();
  if(ch==' ') ch = '+';
  CGPoint cgp = new CGPoint(p,ch);
  cgp.addToGrid(grid);
  }
  void addBox() throws IOException {
  System.out.println("Add Box Menu");
  System.out.println(" Upper Left Corner:");
  Point ul = kbd.getPoint();
  System.out.println(" Lower Right Corner:");
  Point lr = kbd.getPoint();
  System.out.print(" Character: ");
  System.out.flush();
  char ch = kbd.getChar();
  if(ch==' ') ch = '#';
  CGBox box = new CGBox(ul,lr,ch);
  box.addToGrid(grid);
  }
  void addText() throws IOException {
  System.out.println("Add Text Menu");
  System.out.println(" Location:");
  Point p = kbd.getPoint();
  System.out.print(" Text: ");
  System.out.flush();
  String text = kbd.getText();
  CGText cgt = new CGText(p,text);
  cgt.addToGrid(grid);
  }
  }
  主程序CDrawApp类与相应类之间的关系为:
  
  按照本文次序分别编译以上的10个大类,然后运行主程序CdrawApp即可。在程序运行时请注意:当选择增加点、框或者文本串后,选择Show Grid才能出现网格,并显示结果。
  本文通过一个具体的程序开发过程,详细说明了如何使用UML类图来设计Java应用程序,使得程序开发可视化,文档标准化,便于相互协作与管理,是Java应用程序开发的方向。

本文地址:http://com.8s8s.com/it/it16281.htm