当前位置: 技术问答>java相关
关于面向对象的思考,请大家参与!
来源: 互联网 发布时间:2015-03-22
本文导语: 下面是同样功能的两种写法,不考虑执行效率,仅从面向对象角度考虑,哪种更符合面向对象规范? package instance; class InstanceEntity { private static String tablename="Workflow_Instance_Info"; private String InstanceName; private String In...
下面是同样功能的两种写法,不考虑执行效率,仅从面向对象角度考虑,哪种更符合面向对象规范?
package instance;
class InstanceEntity
{
private static String tablename="Workflow_Instance_Info";
private String InstanceName;
private String InstanceType;
public InstanceEntity(){
}
public InstanceEntity(String name,String type){
this.InstanceName=name;
this.InstanceType=type;
}
public static boolean insert(InstanceEntity){
String name=InstanceEntity.InstanceName;
String type=InstanceEntity.InstanceType;
String sql="insert into " + tablename + "(name,type) values('" + name + "','" + type + "')";
bean = new ExecuteSQL();
return bean.execUpdateSQL(sql);
}
}
//************************************************************
package instance;
import instance.InstanceEntity;
class Instance
{
public boolean addEntity(name,type,templateid){
InstanceEntity entity = new InstanceEntity(nama,type);
InstanceEntity.insert(entity);
}
}
//
//***********************************************************
//***********************************************************
//
package instance;
class InstanceEntity
{
private static String tablename="Workflow_Instance_Info";
private String InstanceName;
private String InstanceType;
public InstanceEntity(){
}
public InstanceEntity(String name,String type){
this.InstanceName=name;
this.InstanceType=type;
}
public static boolean insert(String name,String type){
String sql="insert into " + tablename + "(name,type) values('" + name + "','" + type + "')";
bean = new ExecuteSQL();
return bean.execUpdateSQL(sql);
}
}
//****************************************************
package instance;
import instance.InstanceEntity;
class Instance
{
public boolean addEntity(name,type,templateid){
InstanceEntity.insert(nama,type);
}
}
package instance;
class InstanceEntity
{
private static String tablename="Workflow_Instance_Info";
private String InstanceName;
private String InstanceType;
public InstanceEntity(){
}
public InstanceEntity(String name,String type){
this.InstanceName=name;
this.InstanceType=type;
}
public static boolean insert(InstanceEntity){
String name=InstanceEntity.InstanceName;
String type=InstanceEntity.InstanceType;
String sql="insert into " + tablename + "(name,type) values('" + name + "','" + type + "')";
bean = new ExecuteSQL();
return bean.execUpdateSQL(sql);
}
}
//************************************************************
package instance;
import instance.InstanceEntity;
class Instance
{
public boolean addEntity(name,type,templateid){
InstanceEntity entity = new InstanceEntity(nama,type);
InstanceEntity.insert(entity);
}
}
//
//***********************************************************
//***********************************************************
//
package instance;
class InstanceEntity
{
private static String tablename="Workflow_Instance_Info";
private String InstanceName;
private String InstanceType;
public InstanceEntity(){
}
public InstanceEntity(String name,String type){
this.InstanceName=name;
this.InstanceType=type;
}
public static boolean insert(String name,String type){
String sql="insert into " + tablename + "(name,type) values('" + name + "','" + type + "')";
bean = new ExecuteSQL();
return bean.execUpdateSQL(sql);
}
}
//****************************************************
package instance;
import instance.InstanceEntity;
class Instance
{
public boolean addEntity(name,type,templateid){
InstanceEntity.insert(nama,type);
}
}
|
既然insert方法用到的参数是InstanceEntity的属性,所以你的insert(InstanceEntity entity)不如改为不带参数的更符合面向对象的原理
|
oo的一个最大原则是将功能赋予最需要实现的那个类
我觉得第一种比较好,他通过产生内的实例来调用方法
第二种直接使用了static方法调用,实际上不要轻易使用static方法,因为他是随程序一起编译的,无法使用动态绑定的好处.
我觉得static方法只适用于定义一些常用的转换方法,不要用于这种类的常规方法中