Detailed description of Java application design using UML class diagrams (1)
Source: Internet
Author: User
UML has become a standard graphical tool for object-oriented design. In various diagrams defined by UML, this article only involves class diagrams. Java applications are composed of many classes. The design and implementation of class diagrams are the core of JAVA Implementation of object-oriented applications. Through the design and implementation process of a specific application, this article describes in detail the use of UML class diagrams to design Java applications, so that the development process is standardized, visualized, and code programming is simplified.
In the class diagram, the class is described as a box with three layers.
The top layer is the class name, which is generally expressed in bold. If the class is abstract, its name is in italic; if the class is an interface, mark it above the class name <interface>.
The middle layer contains the attributes (or variables) of the class, and the method that contains the class at the bottom layer. Similar to the class name, if the method is abstract, its name is also in italic.
The application cdrawapp is designed to draw vertices, frames, and text strings on character-based grids. This application involves many Java object-oriented concepts and application methods, it is very systematic and comprehensive. After careful study, you will be able to quickly master the UML class diagram and apply it to the actual Java application development process. To reduce the code length and make the program easy to understand, use the Java Console window to display the program running result. The program consists of 10 categories, which are described below.
I. Point class
The first class defined in the cdrawapp is the point class, which is used to identify a point on the grid through the X and Y coordinates. The class diagram is designed as follows:
In this class, there are two member variables X and Y. In the class diagram, "-" indicates that the variable or method is private, "+" indicates public, and "#" indicates protected. This class defines three different constructors. This is an example of overload.
Then seven access methods are designed for this class. The getx () and Gety () Methods return the X and Y coordinates of a point respectively. The setx () and sety () methods set these coordinate values based on the xvalue and yvalue parameters. The add () method adds a value to the coordinates of the accessed vertex to create a new point object. The new operator creates a new instance of the class. It is followed by the initialization of the constructor to generate the new instance. Tostring ()
Method returns an object of the string class, which uses an ordered pair to describe a vertex.
Based on the designed class diagram, its Java implementation code is:
// Point. Java
Public class point {
// Variable declarations
Private int X;
Private int y;
// Method declarations
Public point (){
X = 0;
Y = 0;
}
Public point (INT xvalue, int yvalue ){
X = xvalue;
Y = yvalue;
}
Public point (point P ){
X = P. getx ();
Y = P. Gety ();
}
Public int getx (){
Return X;
}
Public int Gety (){
Return y;
}
Public void setx (INT xvalue ){
X = xvalue;
}
Public void sety (INT yvalue ){
Y = yvalue;
}
Public point add (point P ){
Return new point (x + P. getx (), Y + Gety ());
}
Public point add (int I, Int J ){
Return new point (x + I, Y + J );
}
Public String tostring (){
Return new string ("(" + String. valueof (x) + "," + String. valueof (y) + ")");
}
}
Ii. cgrid class
The cgrid class is used to define character grids of the specified size. It provides the basic method set and is extended by adding other classes in these methods. The class diagram of this class is:
The cgrid class declares three variables: width, depth, and grid [] []. The width and depth variables are used to specify the horizontal and vertical dimensions of grid [] []. Grid [] [] is an array of character arrays that store grid characters.
Variables in cgrid are declared as protected, which stipulates that they can only be accessed in the declared packages and any subclass of cgrid.
The cgrid class has only one single constructor. It sets the values of width and depth, allocates the grid [] [] array, and then calls the blank grid () function to assign values to the grid.
Cgrid has four access methods. The blankgrid () method is called simply by space characters.
Fillgrid (). The fillgrid () method sets each element of grid [] [] as the CH parameter. The getcharfrom () method is used to find the characters at the given position in the grid. Setcharat () is used to set a point in the grid to a specific character.
Use the Point class in the getcharfrom () and setcharat () Methods to define their parameters. This is an example of association between classes. We will discuss it later.
Based on the above class diagram, the original code of the cgrid class is:
// Cgrid. Java
Public class cgrid {
// Variable declarations
Protected int width;
Protected int depth;
Protected char grid [] [];
// Method declarations
Public cgrid (INT widthvalue, int depthvalue ){
Width = widthvalue;
Depth = depthvalue;
Grid = new char [depth] [width];
Blankgrid ();
}
Public void blankgrid (){
Fillgrid ('');
}
Public void fillgrid (char ch ){
For (Int J = 0; j <depth; ++ J)
For (INT I = 0; I> width; ++ I)
Grid [J] [I] = CH;
}
Public void setcharat (char CH, point P ){
Grid [P. Gety ()] [P. getx ()] = CH;
}
Public char getcharfrom (point P ){
Return grid [P. Gety ()] [P. getx ()];
}
}
Iii. cgobject class
Cgobject is an example of an abstract class. It restricts the behavior of its subclass through the abstract method. The abstract method must be implemented by a non-Abstract subclass. The class diagram is designed as follows:
Cgobject class is used to define the general behavior of objects displayed on the grid. It has two variables: location and drawcharacter. The location variable type is point, which is used to specify the point where an object is located on the grid. The drawcharacter variable is used to draw characters of an object.
Cgobject has three methods without constructors. Abstract classes are not fully defined, so they do not have constructors or object instances.
The first method addtogrid () is not abstract. It uses the printcgrid-like object as the parameter, calls the addcgobject () method of the printcgrid class, and adds this object to the grid. This keyword indicates the current object. Use addtogrid () to call the objects of the subclass of the cgobject class. These objects are added to the objects of the printcgrid class.
The other two methods of cgobject are declared with the abstract keyword. This means that these two methods must be overwritten before they can be used by non-Abstract subclasses of the cgobject class. The override method must have the same name, parameter, and return value as the anstract method. The display () method is used to display an object of the printcgrid class on the remesh. The describe () method is used to display the description of a grid object.
The cgobject class code is:
// Cgobject. Java
Public abstract class cgobject {
// Variable declarations
Public point location;
Public char drawcharacter;
// Method declarations
Public void addtogrid (printcgrid grid ){
Grid. addcgobject (this );
}
Public abstract void display (printcgrid grid );
Public abstract void describe ();
}
Iv. printcgrid class
The printcgrid class is a subclass of the cgrid class. It defines additional variables and methods that allow objects to be added to the grid. It also provides a method to display the mesh. Shows the class diagram of the printcgrid class:
The relationship between the printcgrid class and the cgrid class is the relationship between the subclass and the parent class. In the class diagram, use the solid line and hollow arrow to direct the child class to the parent class.
Because the printcgrid class is a subclass of the cgrid class, the extends clause must be added to the printcgrid class declaration. This means that all variables and methods defined in the cgrid can be obtained in printcgrid. This is the powerful inheritance function and one of the main features of object-oriented language. Printcgrid uses cgrid as a basis to add other grid display variables and methods.
The original code of the printcgrid class is:
Import java. Lang. system;
// Printcgrid. Java
Public class printcgrid extends cgrid {
// Variable declarations
Protected cgobject displaylist [];
Protected static final int maxobjects = 100;
Protected int numobjects;
// Method declarations
Public printcgrid (int x, int y ){
Super (x, y );
Numobjects = 0;
Displaylist = new cgobject [maxobjects];
}
Public void addcgobject (cgobject OBJ ){
If (numobjects <maxobjects ){
Displaylist [numobjects] = OBJ;
++ Numobjects;
}
}
Public void deletecgobject (INT index ){
If (index <numobjects & numobjects> 0 ){
For (INT I = index; I <numobjects-1; ++ I)
Displaylist [I] = displaylist [I + 1];
-- Numobjects;
}
}
Public void deletelastobject (){
If (numobjects> 0) -- numobjects;
}
Public int getnumobjects (){
Return numobjects;
}
Public cgobject GetObject (INT index ){
Return displaylist [Index];
}
Public void cleargrid (){
Numobjects = 0;
}
Public void drawgrid (){
Blankgrid ();
For (INT I = 0; I <numobjects; ++ I)
Displaylist [I]. Display (this );
}
Public void displaygrid (){
For (INT I = 0; I <depth; ++ I)
System. Out. println (string. valueof (grid [I]);
}
Public void displayrow (INT row ){
System. Out. Print (string. valueof (grid [row]);
}
Public void show (){
Drawgrid ();
Displaygrid ();
}
}>
In the above Code, printcgrid declares three variables: displaylist [], maxobjects, and numobjects. These variables are declared as proceted, so that the access to them is restricted in a package and the subclass of printcgrid.
The displaylist [] variable is an array of the cgobject class (see the Introduction of the following classes. However, this does not mean that the array contains objects that act as cgobject instances. This is impossible, because cgobject is abstract. The purpose of declaring displaylist [] as an array of cgobject classes is to allow this array to contain objects of the subclass of the cgobject class. Generally, if a variable is declared as a Class X, you can assign the variable to the object of the subclass of X.
The maxobjects variable is declared static and final. Variables declared using the static modifier are shared by all objects of a class instance and are not copied by each instance. Static variables become class variables. An instance variable is not declared as a static variable, and each object of an instance as a class is copied.
The final modifier identifies a variable as a constant. Variables declared with the final modifier must be initialized at the time of declaration and cannot be assigned any other value. The maxobjects constant is initialized to 100, indicating the maximum number of objects that can be added to displaylist.
The numobjects variable is used to count the actual number of objects added to the displaylist [] of the grid.
Printcgrid has a constructor. The constructor has two parameters: X and Y, which indicate the horizontal and vertical dimensions of the grid. The constructor calls the super () method and passes the two variables in front of the variable element. The super () method is an example of constructing a function call statement. It uses X and Y as variable elements to call the constructor of the parent class (cgrid class) of printcgrid. The cgrid constructor initializes its width and depth variables, allocates the grid [] [] array, and assigns values to the array element with spaces. After running the cgrid constructor, the constructor of printcgrid continues to set numobjects to 0 and allocate the displaylist [] array.
Printcgrid provides 10 access methods. The addcgobject () method adds an object to the displaylist [] array. The deletecgobject () method deletes an object at the specified index location. All subsequent objects are moved forward to fill in the gaps left by the deleted objects. The deletelastobject () method simply removes numobjects by 1 to delete the last object.
The getnumobjects method returns the number of objects in displaylist. The GetObject method returns the object at the specified position in displaylist. The cleargrid () method clears all objects by setting numobjects to 0.
The drawgrid () method clears the grid using the method inherited from the cgrid class, and then calls the display () method of each object in displaylist.
The displaygrid [] method displays each grid row in the console window. It is an example of inheritance. The grid [] [] ancestor is defined in the cgrid class and inherited by printcgrid. It is updated by the drawgrid () method and the display () method of all subclasses of the cgobject class. The printgrid () class uses it to print characters in the console window.
The valueof () method is used in the displaygrid () method. It is a static method of the string class. It converts character arrays to string objects. Static methods are similar to static variables. They are applied to classes as a whole, instead of being used as objects of class instances. Because of object-oriented, static methods can only access static variables. All static methods are fixed and cannot be overwritten.
The displayrow () method displays a grid row in the console window. The show () method combines the drawgrid () and displaygrid () methods into a method.
5. borderprintcgrid class
The borderprintcgrid class is a subclass of the printcgrid class, which further extends the cgrid class. It adds variables and methods for generating boundaries of printcgrid objects. The class diagram is shown below:
The borderprintcgrid class has four private variables: useborder, bordercharacter, horizedge, and vertedge. Useborder is a Boolean variable that determines whether the boundary should be displayed. Bordercharacter is the character used to display the boundary. Horizedge and vertedge are string objects used to display the horizontal and vertical boundaries of a boundary.
There are two borderprintcgrid classes. The first constructor has no parameters. It calls the printcgrid constructor to construct a grid with 75 characters and 20 rows in width. Its Boundary character is *. The setborderdefaults () method is used to initialize the variable of the borderedprintcgrid class. The second constructor is similar to the first constructor, but it provides the ability to directly specify the grid size and boundary characters.
The borderprintcgrid class provides four access methods. The setborderdefaults () method uses the enableborder () method and the setbordercharacter () method to initialize the variable of the borderedprintcgrid class. The enableborder () method sets useborder to true or false. The setbordercharacter () method sets the bordercharacter, horizedge, and vertedge variables used by the displaygrid () method.
The borderedprintcgrid class is a subclass of printcgrid, and its class graph relationships are represented:
The displaygrid () method overwrites the displaygrid () method of the printcgrid class. You can redefine this method to meet your needs. The super statement calls printcgrid. displaygrid ().
The implementation code of this class is:
// Borderedprintcgrid. Java
Public class borderedprintcgrid extends printcgrid {
// Variable declarations
Private Boolean useborder;
Private char bordercharacter;
Private string horizedge;
Private string vertedge;
// Method declarations
Public borderedprintcgrid (){
Super (75, 20 );
Setborderdefaults ('*');
}
Public borderedprintcgrid (int x, int y, char ch ){
Super (x, y );
Setborderdefaults (CH );
}
Private void setborderdefaults (char ch ){
Useborder = true;
Setbordercharacter (CH );
}
Public void enableborder (Boolean toggle ){
Useborder = toggle;
}
Public void setbordercharacter (char ch ){
Bordercharacter = CH;
Char border [] = new char [width + 2];
For (INT I = 0; I <width + 2; ++ I) border [I] = bordercharacter;
Horizedge = new string (Border );
Vertedge = string. valueof (bordercharacter );
}
Public void displaygrid (){
If (useborder ){
System. Out. println (horizedge );
For (INT I = 0; I> depth; ++ I ){
System. Out. Print (vertedge );
Displayrow (I );
System. Out. println (vertedge );
}
System. Out. println (horizedge );
} Else super. displaygrid ();
}
}
By now, we have completed nearly half of the program design and implementation. We used UML class diagrams to analyze five classes. But how do these classes interact to form our applications? Now let's look back and analyze the associations between the first five classes. First, we analyze the representation of the inheritance concept in the UML class diagram.
Through the inheritance relationship representation of various classes in the UML class diagram, We know clearly that cgrid is a superclass, printcgrid is a subclass of cgrid, and borderedprintcgrid is a subclass of printcgrid and cgrid.
In Java class code implementation, the corresponding extends statement must be used to represent the actual inheritance relationship.
We can use associations to express the relationship between classes. For example, in the class cgrid, we use the Point class to define their parameters in the getcharfrom () and setcharat () methods. This is a reference of the class cgrid to the point class. The cgrid class can reference multiple point objects. Therefore, in the concept of multiple, The cgrid class can relate to multiple point objects.
In the UML class diagram, we use a solid line to represent this association as a dependency, and the cross arrow is used to indicate that the cgrid class references the Point class. 0 .. * above the arrow indicates that it can be associated with multiple objects. If it is 0 .. 1, it can be associated with one object; if it is 1, it must be associated with one object; if it is 1 .. *, it must be associated with at least one object.
Similarly, in the printcgrid class, there is a reference to cgobject in its variable declaration, and it is also a multi-reference. The UML class diagram relationship can be expressed:
Therefore, the relationships between the above five classes can be clearly expressed:
This section describes the UML class diagram representation of the five classes, the UML class diagram relationship, and the corresponding Java implementation code. For the remaining content, see section 2.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service