CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

接口和抽象类,我晕,该如何应用,高手帮忙。

楼主zhongzuo1981(Sun ONE)2006-03-14 15:59:13 在 Java / J2SE / 基础类 提问

现在有如下需求:  
  数据对象在进入数据库之前要进行一系列的业务逻辑验证,通过验证进入数据库,否则返回给请求。具体的业务逻辑验证多种多样,还会不断变化。  
  想用接口和抽象类来设计一个验证类,可不知如果下手。  
   
  希望高手给个思路……  
  谢谢!!! 问题点数:50、回复次数:18Top

1 楼interpb(曾曾胡,深怕情多累美人!)回复于 2006-03-14 16:05:43 得分 5

把   已知的验证写成接口中的方法  
   
  预留一些验证接口  
   
  Top

2 楼zhongzuo1981(Sun ONE)回复于 2006-03-14 16:22:35 得分 0

public   interface   Validate{  
      public   Boolean   validateDataA(DataObject   dataObject)   throws   Exception;  
      public   Boolean   validateDataB(DataObject   dataObject)   throws   Exception;  
      public   Boolean   validateDataC(DataObject   dataObject)   throws   Exception;  
      ……  
  }  
  然后有个实现这个接口的类。  
  楼上是这意思么?  
   
  Top

3 楼idilent(怎么理解怎么说)回复于 2006-03-14 16:31:01 得分 40

interface   Validatable{  
    public   ValidateException   validate();  
  }  
   
  abstract   SavableObject   implement   Validatable{  
    public   void   save(){  
      validate();  
    }  
  }  
   
  后面各个对象extends   SavableObject,overide   methods   save   dan   validateTop

4 楼zhongzuo1981(Sun ONE)回复于 2006-03-14 16:39:44 得分 0

楼上,overide   methods   save   dan   validate   这是啥意思啊?Top

5 楼idilent(怎么理解怎么说)回复于 2006-03-14 18:35:57 得分 0

and,写错了一个字,就是重载这两个方法。Top

6 楼zhongzuo1981(Sun ONE)回复于 2006-03-16 13:09:56 得分 0

To   :idilent(怎么理解怎么说)  
  你这样做的思想是什么?为什么这样做?能给详细说明一下么?Top

7 楼idilent(怎么理解怎么说)回复于 2006-03-16 14:05:55 得分 0

每个数据对象继承SavableObject呀,然后每个对象存入数据库的方法都重载save,然后在各个对象中自己验证自己的业务逻辑。  
   
  这种设计方法好像那个interface有些多余。如果一定要用inferface的话,也可以这样,Saveable中有一个proteced的属性,就是interface了,然后把各个验证逻辑实现这个接口,这样一来,如果有saveable的对象共享一个逻辑的话就可以不必重写了。  
   
  interface   IValidator{  
    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.  
    }  
  }Top

8 楼idilent(怎么理解怎么说)回复于 2006-03-16 14:08:21 得分 0

http://blog.csdn.net/idilent/archive/2003/07/18/18768.aspx  
   
  这是我很早以前写的使用oo解决关闭窗口的文章,和这个类似,不过是英文的。Top

9 楼zhongzuo1981(Sun ONE)回复于 2006-03-16 17:35:28 得分 0

谢谢,研究一下Top

10 楼zhongzuo1981(Sun ONE)回复于 2006-03-17 17:05:52 得分 0

To   :idilent(怎么理解怎么说)  
  我的理解是这样的,感觉有点怪,哪有问题啊?请指教……  
  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;  
  }  
   
  }Top

11 楼sportboys(多动男孩)回复于 2006-03-17 18:01:36 得分 0

Interfaces   &   Abstract   classes  
   
  One   of   the   most   important   decisions   a   technical   architect   needs   to   make   is   whether   to   specify   an   interface   or   an   abstract   class   in   order   to   implement   some   desired   polymorphic   behavior   in   an   object   model.  
   
  Both   of   these   concepts   support   polymorphism   by   defining   methods   without   bodies.   Classes   extending   an   abstract   class   or   implementing   an   interface   are   required   to   implement   the   bodies   of   abstract   methods   (or   all   methods,   in   the   case   of   interfaces).   Any   other   class   not   familiar   with   the   subclass   can   still   utilize   an   object   by   calling   the   methods   defined   as   abstract   and   getting   results.   This   is   where   the   similarities   end,   however.   The   differences   between   them   intentionally   make   each   valuable   under   different   conditions.  
   
  Interfaces:  
   
  An   interface   is   best   used   when   a   common   inheritance   tree   is   not   possible   among   the   objects   sharing   behavior.  
   
  Have   no   method   bodies   or   functionality   whatsoever;   all   methods   are   implicitly   abstract.    
  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.  
  Top

12 楼idilent(怎么理解怎么说)回复于 2006-03-20 10:20:14 得分 0

你这个设计肯定是有些问题,这样说你不要生气。呵呵。  
   
  首先我们要知道接口是一个很抽象的东西,因此他要做的不仅仅是为了一个class提供接口,而是很多类都要用到这个。所以你的  
  public   interface   Validate   {  
   
  public   boolean   validateObject(UserInfo   userInfo)   throws   Exception;  
  }  
  这样写就只能位UserInfo这一个class服务了。所以你要这样写得话就应该写成  
  public   boolean   validateObject(Object   object)   throws   Exception;这样才能使这个接口可以验证任何一个要保存的对象。  
  Top

13 楼zhongzuo1981(Sun ONE)回复于 2006-03-20 16:37:21 得分 0

TO:idilent(怎么理解怎么说)  
  我也觉得是,那样岂不是每个数据对象都要一个接口。  
  有几个问题:  
  具体的数据类如何转成object,该怎么设计?  
  validateObject方法的实现是应该写在AbstractValidate类的子类里,还是应该为接口单写实现接口的类?  
  面向对象的思想我一直不太理解,还请您多指教。谢谢!Top

14 楼idilent(怎么理解怎么说)回复于 2006-03-21 12:05:35 得分 0

你所有的class都是object的子类,所以没有必要装成,直接进去就可了。  
  validateObject方法的实现肯定要在子类里面,这个就是你的验证逻辑,不同的data   object,验证逻辑肯定是不同的。  
   
  建议你先了解一下面向对象的基础,了解一下为什么继承,有什么好处,为什么抽象,否则这样生搬硬套只会使code更乱。Top

15 楼bgceft()回复于 2006-03-21 16:02:50 得分 5

最好的办法是看看书   然后再HEIP中去多次的运用不同的接口   慢慢就不用怕了  
  我也是这样过来的Top

16 楼zhongzuo1981(Sun ONE)回复于 2006-03-22 08:25:18 得分 0

谢谢各位!!!Top

17 楼zhongzuo1981(Sun ONE)回复于 2006-03-22 08:26:23 得分 0

To   bgceft():  
  给推荐本面向对象基础的书吧,谢谢!Top

18 楼bgceft()回复于 2006-03-29 16:40:13 得分 0

大学教程  
  有点基础就是去JAVA编程思想Top

相关问题

  • 如何写抽象类和接口?
  • 抽象类和接口的讨论
  • 接口能不能继承抽象类?
  • 接口与抽象类的区别???
  • 抽象类和接口区别总结
  • 如果一个类C继承抽象类A同时实现接口B,而抽象类A有非抽象方法out,接口B有抽象方法out...
  • 请问,接口与抽象类之间有何区别
  • 接口和抽象类的问题,请教大家
  • 何时用抽象类,何时用接口?
  • 请问抽象类和接口的区别

关键词

  • 接口
  • 验证
  • 逻辑
  • 面向对象
  • 数据库
  • 数据
  • 业务
  • userinfo
  • validateobject
  • savableobject

得分解答快速导航

  • 帖主:zhongzuo1981
  • interpb
  • idilent
  • bgceft

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo