现在有如下需求:
数据对象在进入数据库之前要进行一系列的业务逻辑验证,通过验证进入数据库,否则返回给请求。具体的业务逻辑验证多种多样,还会不断变化。
想用接口和抽象类来设计一个验证类,可不知如果下手。希望高手给个思路……
谢谢!!!
数据对象在进入数据库之前要进行一系列的业务逻辑验证,通过验证进入数据库,否则返回给请求。具体的业务逻辑验证多种多样,还会不断变化。
想用接口和抽象类来设计一个验证类,可不知如果下手。希望高手给个思路……
谢谢!!!
解决方案 »
- exe文件在执行时不能弹出JOptionPane.showMessageDialog警告对话框
- android(java) 中文乱码的问题
- 请问各位如何建立一个能够对比识别的库
- 关于Annotation的问题
- Java编译过程的问题,有关jbuilder9的问题
- 如何让JDialog有最大化和最小化按钮?
- 关于时间函数
- 30分钟写一道程序(C、C++、Java、伪码),昨天的面试题目!!!
- 谁有JAVAMAIL的例子?给我一个
- 海康SDK开发求解:海康预览时候可以设置OSD字符串,那么请问下预览时可以设置自定义的图标嘛?
- 只是把代码换了一行,服务器端为什么就不显示要打印的内容,详细代码见跟贴
- 关于静态变量的处理
public Boolean validateDataA(DataObject dataObject) throws Exception;
public Boolean validateDataB(DataObject dataObject) throws Exception;
public Boolean validateDataC(DataObject dataObject) throws Exception;
……
}
然后有个实现这个接口的类。
楼上是这意思么?
public ValidateException validate();
}abstract SavableObject implement Validatable{
public void save(){
validate();
}
}后面各个对象extends SavableObject,overide methods save dan validate
你这样做的思想是什么?为什么这样做?能给详细说明一下么?
public ValidateException validate();
}
calss ValidatorFactory{}
abstract SavableObject implement Validatable{
protected Validator validator;
public void save(){
validatory = ValidatorFactory.getValidator(this.getClass());
validatory.validate(this);
//save object to database.
}
}
我的理解是这样的,感觉有点怪,哪有问题啊?请指教……
1、
public class UserInfo {
private String userName;
private String passWord;
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
2、
public interface Validate { public boolean validateObject(UserInfo userInfo) throws Exception;
}
3、
public abstract class AbstractValidate implements Validate{
protected Validate userValidate;
public abstract int insertObject(UserInfo userInfo)throws Exception;
public abstract int updateObject(UserInfo userInfo)throws Exception;
}
4、
public class UserValidateExt extends AbstractValidate{
public boolean validateObject(UserInfo userInfo) throws Exception{
boolean isValidate = false;
//业务逻辑验证
return isValidate;
}
public int insertObject(UserInfo userInfo) throws Exception{
int i = 0;
if(userValidate.validateObject(userInfo)){
//插入数据库
i=1;
}else{
i=0;
}
return i;
}
public int updateObject(UserInfo userInfo) throws Exception{
int i = 0;
if(userValidate.validateObject(userInfo)){
//更新数据库
i=1;
}else{
i=0;
}
return i;
}}
Can be implemented on a class without affecting its ancestry.
Can be used as a flag (as is the case with Cloneable and Serializable) to indicate that something is allowed to happen to a class.
Cannot declare object attributes, but can implement static final attributes
Can emulate attributes only by declaring getter and setter methods, which will require an implementing class to fill them with code.
Abstract Classes:The most obvious situation where an abstract class is called for is when a significant amount of functionality needs to be shared across several classes that share some particular concept.Can have both implemented bodies with functionality and abstract methods. The implemented methods become shared code for the classes that extend the abstract class.
Can have private methods and attributes, just like an ordinary class.
Cannot be instantiated by definition; it must be subclassed in order to utilize its functionality.
Both Interfaces and Abstract Classes:Require that a subclass or implementing class write method bodies for each of the methods in the interface or just the methods declared abstract in the abstract ancestor.
Can be used to cast objects that implement or subclass from them for the purpose of polymorphism.
Most texts describe interfaces as contracts. This is often the best way to think about an interface, because it’s a guarantee that a class will support some functionality without describing how it will be carried out. If you are a painter and business is good then you may have lots of contracts to paint houses, but the contracts will not be specific about how it will be performed. You might use a paintbrush or a spraygun or even subcontract it to someone else at a lower price, but the contract guarantees that the work will take place.An abstract class fits into the inheritance tree. By declaring a class abstract the developer is requiring that the functionality can only be instantiated as some object that subclasses it. This approach lends more towards inheritance (the sharing of functionality between classes) than polymorphism (the differentiation of classes by implementation) in the system’s object model.The decision that is made between designing an interface and coding an abstract class can have profound benefits or serious side effects on a system. In general, the abstract class has more functionality but the interface is more flexible. Specifically, by selecting an interface as the form of polymorphism, you lock your subclasses out of being able to share common code and attributes. By selecting an abstract class, you get the code but sacrifice the ability to subclass from anything else. While the choice is up to the architect, there are some clear cases where one is favorable over the other. The next two sections serve to demonstrate this with a pair of real-world cases that should only have been done one way or the other.
public interface Validate {public boolean validateObject(UserInfo userInfo) throws Exception;
}
这样写就只能位UserInfo这一个class服务了。所以你要这样写得话就应该写成
public boolean validateObject(Object object) throws Exception;这样才能使这个接口可以验证任何一个要保存的对象。
我也觉得是,那样岂不是每个数据对象都要一个接口。
有几个问题:
具体的数据类如何转成object,该怎么设计?
validateObject方法的实现是应该写在AbstractValidate类的子类里,还是应该为接口单写实现接口的类?
面向对象的思想我一直不太理解,还请您多指教。谢谢!
validateObject方法的实现肯定要在子类里面,这个就是你的验证逻辑,不同的data object,验证逻辑肯定是不同的。建议你先了解一下面向对象的基础,了解一下为什么继承,有什么好处,为什么抽象,否则这样生搬硬套只会使code更乱。
我也是这样过来的
给推荐本面向对象基础的书吧,谢谢!
有点基础就是去JAVA编程思想