语言只是载体,思想才是程序设计中的灵魂。
能把“白菜做出茄子味”来的程序员才是优秀的程序员。语言是相同的不管是像java、C#这种纯面向对象的语言还是C++过渡语言或者C面向过程的语言……总是有很多相似的思想在里面。就那java中的DAO设计模式与C#中的三层架构来说:
java中的DAO设计模式,这种模式是比较常用的一种模式。主要应用于对数据库的操作访问。实现了数据层、实体层、业务逻辑层、表示层得分离。便于程序的维护,降低了程序的耦合性。
================
如使用java中的DAO设计模式创建实体类继承 接口
==========
package cn.edu.bzu; public interface PersonDao{ //插入数据 public void insert(Person person)throws Exception ; //更新数据 public void update(Person person)throws Exception ; //删除数据 public void delete(String no) throws Exception; //查询数据 public Person select (String no) throws Exception ; } ======== 实体类实现接口 ========= import java.sql.*; public class PersonDaoImpl implements PersonDao { public void delete(String no) throws Exception { //删除数据 String order="delete from Student where no=?"; PreparedStatement pstate=null; DataBaseConnection dbc=null; try{ dbc=new DataBaseConnection(); pstate=dbc.getConnection().prepareStatement(order); pstate.setString(1,no); pstate.executeUpdate(); }catch(Exception e){ System.out.println("删除数据时出现异常"); }finally{ pstate.close(); dbc.closeConnection(); } } public void insert(Person person) throws Exception { //插入数据 String order="insert into Student(no,name,sex,age) values(?,?,?,?)"; PreparedStatement pstate=null; DataBaseConnection dbc=null; try{ dbc=new DataBaseConnection(); //被实例化时,就连接到了数据库 pstate=dbc.getConnection().prepareStatement(order); pstate.setString(1,person.getNo()); pstate.setString(2,person.getName()); pstate.setString(3,person.getSex()); pstate.setInt(4,person.getAge()); pstate.executeUpdate(); pstate.close(); }catch(Exception e){ System.out.println("插入数据时出现异常。"); }finally{ dbc.closeConnection(); } } public Person select(String no) throws Exception { //查询数据 String order="select * from Student where no=?"; PreparedStatement pstate=null; DataBaseConnection dbc=null; ResultSet set=null; Person person=new Person(); try{ dbc=new DataBaseConnection(); pstate=dbc.getConnection().prepareStatement(order); pstate.setString(1,no); set=pstate.executeQuery(); if(set.next()){ person.setAge(set.getInt(1)); person.setName(set.getString(2)); person.setNo(set.getString(3)); person.setSex(set.getString(4)); } System.out.println(person.getName()); //输出一个查询结果在后台 pstate.close(); set.close(); }catch(Exception e){ System.out.println("查询数据出现异常"); }finally{ dbc.closeConnection(); } return person; } public void update(Person person) throws Exception { //更新数据 String order="update student set name=?,sex=?,age=? where no=?"; DataBaseConnection dbc=null; PreparedStatement pstate=null; dbc=new DataBaseConnection(); try{ pstate=dbc.getConnection().prepareStatement(order); pstate.setString(1,person.getName()); pstate.setString(2,person.getSex()); pstate.setInt(3,person.getAge()); pstate.setString(4,person.getNo()); pstate.executeUpdate(); pstate.close(); } catch(Exception e){ System.out.println("更新数据出现异常"); }finally{ dbc.closeConnection(); } } }
==========
实体类,也就是DAO中的VO类。
=======
package cn.edu.bzu;
public class Person { //Person中的属性要与表中的字段一一对应。
private String no;
private String name;
private String sex;
private int age;
public void setNo(String no){
this.no=no;
}
public String getNo(){
return no;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setSex(String sex){
this.sex=sex;
}
public String getSex(){
return sex;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
}
==========
尤其是在上面的实体类与VO类中最能体现出来。它们都是实现了对实体对象的封装。不同的类或者不同的架构之间使用实体对象来进行交互。同时这样反应了面向对象的特点。
java DAO设计模式中VO实现类+接口也就相当于C#中的实体类(进行数据层与业务逻辑层或业务逻辑层与表示层之间的交互)DAO中的数据库连接类也就自然而然的成了三层架构中的数据层。DAO实现类也就是三层架构中的业务逻辑层了。此外java DAO设计模式中还有一个工厂类。
使用DAO工厂类可以很好的解决后期修改的问题,可以通过该DAO工厂类的一个静态方法来获得DAO实现类实例。这时如果需要替换DAO实现类,只需修改该DAO工厂类中的方法代码,而不必修改所有的操作数据库代码。
=====比方上面程序的工厂类
+++++++++++
package cn.edu.bzu;
public class PersonDaoFactory { //这是工厂类
public static PersonDao getPersonDaoInstance(){
return new PersonDaoImpl();
}
}
======================
这种模式是比较常用的一种操作数据库的思想模式。在其他语言中也是有同样的体现。就像上面说的C#中的三层架构。就是同一种思想在两种语言上的不同表现形式而已。
=======================
思想确实是编程灵魂。就像讲师说的那样:代码谁也会敲,关键是思想。
任何一个程序,即使是很简单的程序,要想做好,也有很多文章可做。平凡处见不平凡,简单里有不简单,才是高手。能把白菜这种简单的菜做成美味的厨子才是好厨子。
+++++++++++++++++++++++++++

+++++++++++++++++++++++++++