Java 这个班级设计错了吗?
我开始是一名java开发人员。我正在用java和mysql开发一个股票应用程序 我有个大问题。我想不出应该在哪里放置方法来保存数据。我应该为DB operatios创建一个新类,还是应该将它们放在inventory类中 谢谢 我的课程是:Java 这个班级设计错了吗?,java,mysql,class,Java,Mysql,Class,我开始是一名java开发人员。我正在用java和mysql开发一个股票应用程序 我有个大问题。我想不出应该在哪里放置方法来保存数据。我应该为DB operatios创建一个新类,还是应该将它们放在inventory类中 谢谢 我的课程是: package inventory; public class Item { private String bc; // ITEM BARCODE private String description; // ITEM DESCRIPTION private
package inventory;
public class Item
{
private String bc; // ITEM BARCODE
private String description; // ITEM DESCRIPTION
private int nItm; // NUMBER OF ITEMS IN A PACK OR ITEMS
/**
* Constructor for objects of class item
* - Initialise instance variables bc, description to null and nItm to 0
*/
public Item()
{
// initialise instance variables
bc = null;
description = null;
nItm = 0;
}
/**
* Constructor for objects of class item
* - Initialise instance variables to a given parameters.
* @param bc String item codebar.
* @param description String description of the item.
* @param nItm int items or number of items in a pack
*/
public Item(String bc,String description,int nItm)
{
this.bc = bc;
this.description = description;
this.nItm = nItm;
}
/**
* Gets a codebar from the item
*
* @return A String with the codebar item.
*/
public String getBarcode()
{
// put your code here
return bc;
}
/**
* Gets description from the item
*
* @return A String with the description item.
*/
public String getDescription()
{
return description;
}
/**
* Gets the number of items in stock
*
* @return Number of items in stock.
*/
public int getNumberOfItems()
{
return nItm;
}
/**
* Set the value for codebar
*
* @param bc Barcode item.
*/
public void setBarcode(String bc)
{
this.bc = bc;
}
/**
* Set the value for description
*
* @param description Description item.
*/
public void setDescription(String description)
{
this.description = description;
}
/**
* Set the value for nItm
*
* @param nItm Number of items or items in a pack
*/
public void setNumberOfItems(int nItm)
{
this.nItm = nItm;
}
/**
* Shows an item with this template "| %-16s | %-44s | %5d |"
*/
public void showItem()
{
System.out.print("\f");
System.out.printf("+------------------+----------------------------------------------+-------+\n");
System.out.printf("| BARCODE | DESCRIPTION | STOCK |\n");
System.out.printf("+------------------+----------------------------------------------+-------+\n");
System.out.printf("| %-16s | %-44s | %5d |",this.bc,this.description,this.nItm);
System.out.printf("+-------------------------------------------------------------------------+\n");
}
}
}快速回答是的,这是一个糟糕的设计对不起,下面是我要做的改进:
- 尝试实现类之间的松散耦合,每个类都应该是一个接口的实现
public interface ConnectionManager { void On() throws SQLException; void Off() throws SQLException; ResultSet ExeGet(String Query) throws SQLException; int Exe(String Query) throws SQLException; } public class MysqlConnectionManager implements ConnectionManager { @Override public void On() throws SQLException { } @Override public void Off() throws SQLException { } @Override public ResultSet ExeGet(String Query) throws SQLException { } @Override public int Exe(String Query) throws SQLException { } }
- 尝试遵循驼峰案例实践()
- 基于属性名称,属性和关联方法应该共享相同的名称
private String barCode; public String getBarCode(){ return this.barCode; } public void setBarCode(final String barCode){ this.barCode = barCode; }
- 遵循DAO模式(),基本上您需要创建一个类名ItemDao,它将负责“Item”的DB的所有操作
- 清单应该重命名为ItemService,并且它不应该只有DAO才能访问DB,因此如果您需要实现另一个DAO,则可以很容易地替换它
- 不要过多地使用System.out.print,而是将类配置为使用输出流,这样就可以轻松地使用文件或字符串输出流进行配置
- 要将数据注入数据库,请使用prepared语句,而不是在查询()中连接数据
- 尝试实现类之间的松散耦合,每个类都应该是一个接口的实现
public interface ConnectionManager { void On() throws SQLException; void Off() throws SQLException; ResultSet ExeGet(String Query) throws SQLException; int Exe(String Query) throws SQLException; } public class MysqlConnectionManager implements ConnectionManager { @Override public void On() throws SQLException { } @Override public void Off() throws SQLException { } @Override public ResultSet ExeGet(String Query) throws SQLException { } @Override public int Exe(String Query) throws SQLException { } }
- 尝试遵循驼峰案例实践()
- 基于属性名称,属性和关联方法应该共享相同的名称
private String barCode; public String getBarCode(){ return this.barCode; } public void setBarCode(final String barCode){ this.barCode = barCode; }
- 遵循DAO模式(),基本上您需要创建一个类名ItemDao,它将负责“Item”的DB的所有操作
- 清单应该重命名为ItemService,并且它不应该只有DAO才能访问DB,因此如果您需要实现另一个DAO,则可以很容易地替换它
- 不要过多地使用System.out.print,而是将类配置为使用输出流,这样就可以轻松地使用文件或字符串输出流进行配置
- 要将数据注入数据库,请使用prepared语句,而不是在查询()中连接数据
private String barCode;
public String getBarCode(){
return this.barCode;
}
public void setBarCode(final String barCode){
this.barCode = barCode;
}