OOP has been used for a long time and is believed by the developers. It is much more beneficial. I am not to sing it here. I encourage you to use TOP, but there is still two well-known issues when using OOP:
One is that the persistence layer is generally the relational database, which is not a natural thing to be mapped to the relational database, so it is necessary to add an ORM layer to implement the object to the relational database mapping.
Second, in the view layer in the OOP architecture, most of the case is a single table, even if an object is combined by multiple objects, the display layer exhibits other information by such as "View Details", so it is used in the view layer. Organizational business logic's objects are displayed, sometimes it is necessary, and. To show, it is possible to add some display to an object.
When business logic is more complicated, use OOP, although it has brought these two questions, it is still very worthwhile, however, if business logic is relatively simple, I recommend you use the way to use, Table-Oriented Programming, use It, database access and data show will be very natural, and you may have many tools to help your development.
First introduce the concept of TOP, then talk about the problem that it will encounter in the show layer, and finally propose SimpleValue to solve these problems.
TOP is the meaning of database table programming, solving business logic, using Table Module (Martin Fowler, PAEE); database access is the Row Data Gateway (Martin Fowler, PAEE), which is ideal for simple logic It. For a use case: According to the employee name, the employee details
Database table: employee form and department table, employee belongs to a department
CREATE TABLE EMPLOYE
(
ID Number (9) PRIMARY Key,
Name varchar (20),
Sex Number (1),
Department_id Number (5)
)
Create Tale Department
(
ID Number (9) PRIMARY Key,
Name varchar (20),
P_id number (9)
)
Public Class Employe
{
Public int ID;
Public String Name;
Public int SEX;
Public int dptid;
STATIC STRING SQL = "SELECT * FROM EMPLOYE WHERE ID =?"
Public Static Employe GetEmploye (INT ID) THROWS ApplicationException;
{
Employe EMP = New EMPLOYE ();
Connection conn = NULL;
Try
{
CONN = dbhelper.getConnection ();
PreparedStatement PS = Conn.con.PrepareStatement (SQL);
...............
}
Catch (Sqlexception SQLE)
{
Throw new ApplicationException (Sqle.getMessage ());
}
Finally
{
DBHELPER.CLANUP (CONN);
}
Return EMP;
}
}
Public class departments
{
Public int ID;
Public String Name;
Public int PI;
STATIC STRING SQL = "SELECT * from Department Where Id =?";
Static string allsql = "select * from department";
Public Static Department getDept (int id) throws APLICATIONEXCEPTION
{
Department dept = new department (); // ...
Return dept;
}
Public static department [] getAllDept () THROWS APLICATIONEXCEPTION
{
List depts = new arraylist ();
// ......
Return (Department []) DePts.Toarray (New Department [dePts.Size ()]);
}
}
The above example is a common style of TOP programming. Not much to say, you can refer to PEEA to learn more. Now use TOP programming is to display a value object (object different from OO), for example, display a user When detailed, the department name is displayed instead of the display department ID. For object-oriented programming, you need to modify the Emploee object, add a property declared public department dept, and make a lot of code in your O / R mapping layer Modify work or configuration work (if you use Hibernate tool to complete mapping). For TOP programming, the usage method is to apply SimpleValue to help you complete the work