8.2 Object-oriented analysis and data sheet creation (version V0010) (transfer)

xiaoxiao2021-03-06  39

Today, I will post in the "Plug-in Project Battle" chapter on modeling. Although it is simple, it is still very important, because in viewing a lot of posts, I have a lot of arguments, I estimate at least 70% of Java programmers, can't do very well. Object-oriented design and analysis, how much this section also reflects some of my experiences and points, I hope to help everyone. *********************************************************** ***** author: Chen Gang, general programmer, had the privilege to Eclipse plug-ins developed over a medium-sized software. It is now practiced that paper, temporarily tentatively take the book

8.2 Object-Oriented Analysis and Data Sheet Creation (Version V0010)

8.2.1 Interface effect and implementation

This chapter is preparing a student score management software. Since the main purpose is to give an example of project development, the function of this software is fairly simple, and there is no need to analyze phase. Regarding some of the functions and concepts of student performance management software, no more, after all, everyone is coming out of school and exams, it is very familiar with these.

The main interface framework of this chapter is shown in Figure 8.19:

Figure 8.19 Main interface frame

Function Description:

l The upper left is the primary function navigator view (referred to as the main function navigator or the main function view), which provides a functional node tree, which will implement the "File Management" and "Performance Management" function.

l The right is an editor that will generate an editor when you click the File Management node.

l Left lower part is a search view of the performance management, and you can query the corresponding test scores based on the search criteria set by this view.

l There is also an editor called "2003-12-11", which will generate this editor when clicking the "Search" button left down, as shown in Figure 8.20:

Figure 8.20 Score Editor

8.2.2 Object-Oriented Analysis and Design

Object-oriented analysis and design, also known as OOAD (Object Oriented Analyze Design). Because it can describe the real thing more accurately and naturally to describe the real thing and make the software built on it, it has better reuse rate, scalability, and maintenanceability, and OOAD is the current most important software methodology. One.

OOAD and ROSE, TOGETHER and other UML software do not necessarily the relationship, OOAD is a method, UML is a graphical language describing this method, and Rose, etc. is a specific tool using UML. The key to OOAD is the transformation of thinking, not the use of tools, even if only pencils and white paper can be an excellent OOAD expert.

Nowadays, the college's courses are mostly C, Basic, VB, FoxPro, even with C , Java, it is also possible to write programs in a process-oriented way, so it does not mean that you are in an object-oriented way. And programming. The shape of the object, and there is no object, is the biggest defect of general programmers.

Taking this project as an example, most developers who are accustomed to the process-oriented programming way of programming, which generally begins to design the table structure of the database, while the coding stage begins to consider the object according to the table structure. Design and creation, this development method is to have a deep-oriented process in the past, and is branded for database table programming. The so-called "all things", OOAD should be the core of thinking as a thinking, not only "object" as a programming means, should complete the object design, then create a table according to objects, this is the most basic order.

Of course, this approach will encounter some difficulties and resistance when converting into a database. After all, the database is not object-oriented, and the SQL language is not object-oriented. However, database persistence technology such as Hibernate, JDO, EJB has allowed developers to program with complete object-oriented methods without having to endure the pain of "object" to "relationship" transformation.

In order to let readers can understand how to manually complete the transformation of "objects" to "relationship", this plugin project is still implemented using pure JDBC. In Chapter 9, Hibernate is explained, the so-called "first bitterness", through two ways comparison, readers can deeper the beauty of Hibernate and other database persistent technologies.

Students' results management software in this chapter have the following objects: students, teachers, grades, classes, courses, grades, exams, all objects of this project are created under the cn.com.chengang.sms.Model package, as shown in Figure 8.21 below. Next, these objects will be analyzed and given the source code and UML class diagrams.

Figure 8.21 Package where the data object is located

1, user object: student, teacher

This system may have a front desk website, such as: Teacher uses Eclipse to manage grades, and students query grades through a web page, and all data is concentrated on the center of the school. Therefore, there are two types of systems: students, teachers, and these two users have some information, and some are different. For example, they all have user names, name, password, etc., and students do not have a teacher's course attribute, and teachers do not have students' class properties.

From the above analysis, we will abstract the commonality of the two users into an interface: iuser, this interface has the following properties: Database ID Number (ID), User ID, Password, Name, Last Login Latestonline. In addition, Student has a class attribute (Schoolclass), teacher class (TEACHER), Course, student class and teacher class are implemented in iUser interface.

Another advantage of abstracting the user into an interface is that the user is placed under the same specification. In the future, it is necessary to add a type of user, such as parent users, just need to implement the iUser interface. "Interface" is a most important concept developed by Java, but also a concept of excellent Java designer must master and skilled.

Other instructions: The instance variable of the class has a variety of schematic: the universal name is "instance variable" or "attribute"; in the physical class, it can be called "field" because the field of the data table is corresponding to the field of the data table; some books The article is also called "domain".

First give the UML design diagram of the user class, as shown in Figure 8.22 below:

Figure 8.22 User class UML class diagram

The source code of the user class is as follows:

(1) User interface iuser

Package cn.com.chengang.sms.model; import java.util.date;

Public interface iuse {

/ **

* Get the database ID

* /

Public long getId ();

/ **

* Setting up the database ID

* /

Public void setid (long id);

/ **

* Get your username

* /

Public string getUserid ();

/ **

* Set the username

* /

Public void setUserid (String UserID);

/ **

* Get your password

* /

Public string getpassword ();

/ **

* set password

* /

Public void setPassword (String Password);

/ **

* Get the user's name

* /

Public string getname ();

/ **

* Set the user name

* /

Public void setname (String name);

/ **

* Get the last login time

* /

Public Date getlatestonline ();

/ **

* Set the last login time

* /

Public void setlatestonline (Date Date);

}

Program description:

l Interface specification can only define methods to define attribute variables, so this example only defines the SET / GET method for users attributes.

l Is the interface defined by the interface to be as public or abstract, this example adds public, you can also remove, both effects.

l You need to pay attention to the Date object is java.util.date, don't confuse the java.sql.date.

(2) Implementation of an abstract class AbstractUser for interface IUSER

Each specific user class (student, teacher) must implement the method defined in the interface iUser, and the code of these methods is the same, so we use an abstract class AbstractUser to unify the public properties in the iUser interface, we put This abstract class is called "default to achieve abstract classes". AbstractUser also provides a method of implementation, but also provides definitions of attribute variables, and all user subclasses will inherit and have these properties.

The specific code of the AbstractUser class is as follows:

Package cn.com.chengang.sms.model;

Import java.util.date;

Abstract Class AbstractUser Implements iuse {

Private long ID; // Database ID

Private string userid; // username

Private string password; // password

Private string name; // Name

Private date limited; // Last login time

/ ********* The following is the implementation method of interface IUSER *********** /

Public long getId () {

Return ID;

}

Public void setid (long id) {

THIS.ID = ID;

}

Public string getUserid () {

Return UserId;

}

Public void setUserid (String UserID) {

THIS.USERID = UserId;

}

Public string getpassword () {

Return Password;

}

Public void setpassword (string password) {

this.password = password;}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public Date getlatestonline () {

Return Latestonline;

}

Public void setlatestonline (Date Latestonline) {

THIS.LATESTONLINE = Latestonline;

}

}

(3) Student class Student

Student class Student inherits from abstract class AbstractUser, so it also has all the properties and methods in the abstract class, so it is only necessary to define the student class independent properties and methods.

Package cn.com.chengang.sms.model;

Public class student extends abstract {

// The class is derived in order to avoid confusion with the name of the class (Class), named Schoolclass

Private SchoolClass School School;

/ **

* Get the class of students

* /

Public schoolclass getSchoolclass () {

Return SchoolClass;

}

/ **

* Set the class of students

* /

Public void setschoolclass (schoolclass schoolclass) {

THIS.SCHOOLCLASS = SchoolClass;

}

}

(4) Teacher TEACHER

Package cn.com.chengang.sms.model;

Import java.util.hashset;

Import java.util.set;

Public class teacher extends abstractuser {

Private set coursees = new hashset (); // teaches course

/ **

* Get all courses

* /

Public set getcourses () {

Return Courses;

}

/ **

* Set a batch of courses

* /

Public void setcoursees (set courses) {

THIS.COURSES = COURSES;

}

/ **

* Add a course

* /

Public void addcourse (Course Course) {

Course ..add (course);

}

/ **

* Delete a course

* /

Public Void RemoveCourse (Course Course) {

Course.Remove (Course);

}

/ **

* Clear all courses

* /

Public void clearcourses () {

Course.clear ();

}

/ **

* Do you teach this lesson?

* /

Public Boolean ISCOURSE (Course Course) {

Return Courses.Contains (COURSE);

}

}

Program description:

l We also view the course as an object, named course, will give its code later. Teachers and courses are many-to-many relationships: a teacher may teach multiple courses, and a course may also have several teachers to teach. When an object corresponds to a plurality of objects, such as a teacher, a Java collection is needed to store these courses, and one element in the collection is a course.

l In two sets of List and SET, this example selects the SET type collection. The characteristics of the SET are the elements they contain will not be repeated (if you don't make an error, it is equal to not adding), but the elements in the set are disorderly, if you join "Chinese", add "Mathematics", It is not necessary to "Chinese" before "Mathematics" when removing the display later. The LIST set is different, and it is arranged in the order of addition, and the repeated element is allowed. l Set is an interface, the class that is actually used is HashSet, which should be utilized as much as possible when defining the object to have better scalability.

l The teacher's course attribute added three ways on the SET / GET method: increase the course, delete the course, and determine whether the teacher teaches a course, joining these methods is mainly for future use.

L Because in the class ISCOURSE, CLARCOURS, AddCourse, etc., when course is empty, it will be wrong, so when defining the coursees property, a Hashset value is given.

2, Course, Class (Schoolclass), Grade Object

These three objects are relatively simple. The source code is as follows:

(1) Course class Course

Package cn.com.chengang.sms.model;

Public class course {

Private long id;

Private string name; // Course Name: Mathematics, Language

Public course () {}

Public course (long ID, string name) {

THIS.ID = ID;

THIS.NAME = Name;

}

/ ********* Attribute corresponding set / get method ************* /

Public long getId () {

Return ID;

}

Public void setid (long id) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

}

Program description:

l For courses, this recorded attribute, does not seem to use the long type, but for the whole, all objects' IDs use the long type.

l Here to add a constructor course (long ID, string name) in order to create an object.

(2) Class class Schoolclass

Package cn.com.chengang.sms.model;

Public class schoolclass {

Private long id;

Private string name; // Class: 43 shift, 52 classes

Private grade grade; // The class of this class is

Public schoolclass () {}

Public SchoolClass (Long ID, String Name, grade grade) {

THIS.ID = ID;

THIS.NAME = Name;

THIS.GRADE = GRADE;

}

/ ********* Attribute corresponding set / get method ************* /

Public long getId () {

Return ID;

}

Public void setid (long id) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public grade getGrade () {

Return grade;

}

Public void setgrade (grade grade) {

THIS.GRADE = GRADE;

}

}

(3) Year grade grade

Package cn.com.chengang.sms.model;

PUBLIC CLASS GRADE {

Private long id;

Private string name; // grade name: big one, third

Public grade () {}

Public grade (long ID, string name) {

THIS.ID = ID;

THIS.NAME = Name;

}

/ ********* Attribute corresponding set / get method ************* /

Public grade (int id, string name) {

THIS.ID = New long (id);

THIS.NAME = Name;

}

Public long getId () {

Return ID;

}

Public void setid (long id) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

}

(4) Three types of UML maps, as shown in Figure 8.23 ​​below:

Figure 8.23 ​​Course, class, grade UML map

3, student score (Studentscore), exam (exam) object

The student's achievements generally include the following information: Which student's grades are, which exam, how much is the score of this student? Here we take the information of the exam to form an exam (Exam).

l The properties of the student score include: student objects, exam objects, scores.

l It has been given in front of the student object, the score is a real number.

l And the test object contains the following properties: the exam name, invigilator, exam, class, test time. If necessary, you can also add more attribute fields, such as: The number of exams, the number of people, the number of cheating, etc.

(1) Student transcripts StudentScore

Package cn.com.chengang.sms.model;

Public class studentscore {

Private long id;

Private exam exam; // test entity

Private student; // Student

Private float score; // score

/ ********* Attribute corresponding set / get method ************* /

Public long getId () {

Return ID;

}

Public void setid (long id) {

THIS.ID = ID;

}

Public float getscore () {

Return score;

}

Public void setscore (float score) {

this.score = score;

}

Public student getstudent () {return student;

}

Public void setstudent (student student) {

THIS.STUDENT = STUDENT;

}

Public exam getExam () {

Return exam;

}

Public void setExam (exam exam) {

THIS.EXAM = EXAM;

}

}

(2) Exam EXAM

Package cn.com.chengang.sms.model;

Import java.util.date;

PUBLIC CLASS EXAM {

Private long id;

Private string name; // Examination name, such as: 2004, half semester, 143 shift, no language test

Private teacher teacher; // invigilator

Private course course; // exam courses

Private SchoolClass School School; // Examination Class

Private date Date; // Test time

/ ********* Attribute corresponding set / get method ************* /

Public course getcourse () {

Return Course;

}

Public void setcourse (Course Course) {

THIS.COURSE = COURSE;

}

Public long getId () {

Return ID;

}

Public void setid (long id) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public schoolclass getSchoolclass () {

Return SchoolClass;

}

Public void setschoolclass (schoolclass schoolclass) {

THIS.SCHOOLCLASS = SchoolClass;

}

Public teacher getteacher () {

Return teacher;

}

Public void settecher (teacher teacher) {

THIS.TEacher = Teacher;

}

Public Date getdate () {

Return Date;

}

Public void setdate (Date Date) {

THIS.DATE = DATE;

}

}

(3) Two types of UML diagrams, as shown in Figure 8.24

Figure 8.24 Student grade, class diagram of the exam

4, summary

When designing objects such as grade, class, there is also a possible approach to cancel these objects and use the character types directly in the student class, class attributes. This method seems to be convenient in programming, but does not meet the design specification of the database, it mainly has the following shortcomings:

l Data redundancy - If you need to add a "class teacher" attribute, this book is only necessary to add an attribute in the class class, and the latter approach needs to join a class teacher in the student class. There are dozens of students in a class, and their teachers are the same, which produces a lot of data redundancy.

l Modification is inconvenient - If you want to change the name of the class, the book's approach only needs to modify a record in the class table, and the latter approach needs to update all the class fields in the student table.

l Consistency - the latter approach may have a consistency problem. For example, a class may have a variety of names in the student watch: 43, 43 shifts, high 43 classes. Practice recommendation:

l When designing objects, you should keep the fine particle size of the object. For example: the design of the performance, the design of the exam is to follow this principle. Some people may cancel the test object, and combine their properties into a grade object, which is not correct, and will cause data redundancy of the data table later.

l Try to an identification attribute (field) of each entity object (table), add one and business logic, such as the automatic handset attribute (field) ID in this example. After balancing between speed and scalability, it is recommended to define it into a java.lang.long type.

l Designing databases as possible in accordance with the database design paradigm, do not put the same field in multiple tables in order to write SQL statements, unless you are extremely high in query speed. And it is necessary to know that this will lead to the difficulties of future database maintenance and extension, and will need to update multiple tables when updating data, and increase complexity.

l The entity object is a pure data object, and the database table has a certain degree of correspondence, but it is not completely corresponding. Remember not to join business logic in the entity object or take data from the database, it should be completely separated from business logic to ensure that entity objects are purified as pure data objects, which makes it more reused. Sex.

Other Description: The object created in this section called an entity object, which is a concept proposed by EntityBean in EJB, which uses an entity object (entity class). It can also be called Pojo (Plain Old Java Object, simple original Java object), using POJO's called Pojo in Hibernate.

转载请注明原文地址:https://www.9cbs.com/read-60135.html

New Post(0)