找回密码
 申请新用户
搜索
热搜: 活动 交友 discuz
查看: 14448|回复: 11

面向对象,

[复制链接]
发表于 2004-12-21 22:07:49 | 显示全部楼层 |阅读模式
公司新来一同事,搞java的,看了看他写代码。靠,我在写什么,呀呀的,。

看看别人写的代码,灵巧而秀气,他说面向对象的代码有两点必须被记住,一是抽象,二是封装变化。

接口与继承,多态的应用,可扩展性,看了他写的实际的例子,真的是,,,

我觉的虽然是在开发阶段你多发了一点时间,可系统的质量大大提高了。

这是我第二遍看《设计模式》,再结合他写的实际的代码,想想自己真是太菜了。

醍醐灌顶啊,以后再也不能这样写代码!

抽象,封装变化,这几天,一直在想这些问题。

 楼主| 发表于 2004-12-21 22:09:31 | 显示全部楼层
多态真的是一种十分灵巧的语言特性,

年里要细读一些关于模式与面向对象的书,要做一个专题研究。


[此贴子已经被作者于2004-12-21 22:11:04编辑过]

回复

使用道具 举报

发表于 2004-12-23 17:43:59 | 显示全部楼层
嗯,说的很有道理!我觉得编程基础很重要,规范也很重要,而且心要沉的下来研究!
回复

使用道具 举报

 楼主| 发表于 2004-12-28 13:55:06 | 显示全部楼层
我觉的是自己意识不到位。跟xjb与watcher说说,听听大家的意见。

我手头的这个项目有一个上传文件操作,基本上是对文件校验(文件类型,文件大小),一个重要的操作是将文件放到服务器的目录中,或是将文件数据放到数据库的某张表的某个字段中,当然还有一些销毁自身,删除服务器文件,或是将数据库的数据生成为文件等相关操作。

我觉的这就可以设计成为一个上传文件类,编程框架中有一个得到上传文件的相关信息的类,但它不支持上面我所说的这些操作。

另外,第一贴我提到的同事解决了一个取得web页面下拉列表的问题,以前我们是一个列表写一个方法,这当然简单,但重复代码一大堆。无非是到一个固定的下拉表中取值对,或是写一个sql语句到表中取值对,如id与名称之类。

他的这个取值对框架是这样的,取值对有两个主要参数,一是数据库库连接,而是取下拉的类型,基本操作就是到下拉表里取值对。但是它的数据库连接是一个自定义的接口,具体的实现类实现这个接口,不同的连接拥有统一的参数接口,有默认实现,你也可以方便的定义自己的连接实现。而类型字符串也是智能的,它可以识别是到下拉表中取值还是一个sql语句取值对!

重要的是sql语句取的值对,因为通过它取出来的值对是对象,我不知道它是什么,也许是整数,也许是小数,也许是一个其它的什么对象,但是我的代码中是一个object(所有java类的基类),使用object.toString()方法写一个通用的代码,所有的对象都实现了自己的toString()方法,具体运行的时候,这里调用的是动态的toString()方法,是的,不会错的,它运行良好。


重要的是要有这种意识,写了两处重复上传文件操作时,就应该重构它,封装这些操作。发现在写同一种下拉写法的时候,重构它,不能不思考。

回复

使用道具 举报

发表于 2004-12-28 16:24:39 | 显示全部楼层
对,意识很重要,要把意识当成习惯。不养成习惯,手“写”乱了以后难改了。

写任何一段代码前,不能想着,我的代码只是权宜之际的,不考虑以后,不考虑以后的工作!

对于“面对对象”,我觉得首先要做到“抽象”,如果和抽象出一个“通用”类非常重要,既然是类,就不能大而全,要精当,要精华。没这个基础,就不能称为:面对对象!
回复

使用道具 举报

 楼主| 发表于 2004-12-30 13:45:58 | 显示全部楼层
这几天有点着迷,这事得以后再谈。呵呵,

不过,

“写任何一段代码前,不能想着,我的代码只是权宜之际的,不考虑以后,不考虑以后的工作!”

这句话,我完全赞成。可谓一针见血,因为公司它总是要求速度。

老实说,我都没有怎么好好画过类图!这些对方不改进,可能代码质量将无法进一步提高。
回复

使用道具 举报

 楼主| 发表于 2005-1-11 10:06:04 | 显示全部楼层
/**
* Created on 2005-1-5
*
* ProjectName:IRMSHB 1.0
*
*/
package com.ifreeway.irmshb.waf;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts.upload.FormFile;

import com.ifreeway.common.util.StringUtil;

/**
*
* <p>Title: 文件上传类</p>
* <p>Description:处理文件上传</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: ifreeway.com 广州鑫路交通科技公司</p>
* @author wangsy
* @version 1.0
*/
public  class FileUpload {
   
    /**
     * 文件信息
     */
    private FormFile file_info;
   

    /**
     * 构造器,得到文件信息
     * @param formfile 表单上传文件
     */
    public FileUpload(FormFile formfile){
        file_info = formfile;   
    }
    /**
     * 校验文件后缀名
     * @param file_ext 文件扩展名数组,如.jpg, .rar, .zip。
     * @return true:是该扩展名数组中存在的后缀名 false:不是该扩展名数组中存在的后缀名
     */
    public boolean validateFileName(String[] file_ext){
        if (file_info == null
                ||StringUtil.isEmpty(file_info.getFileName())
                ||file_info.getFileSize()==0) {
            return false;
        }
        if (file_ext == null||file_ext.length==0){
            return false;
        }
        
        String file_name = file_info.getFileName();
        
        int s_id = file_name.lastIndexOf(".");
        
        if (s_id == -1) {
            return false;
        }
        
        String ext = file_name.substring(s_id);
        System.out.println("File Ext:"+ext);
        
        int size = file_ext.length;
        
        for(int i=0;i<size;i++){
           if (ext.toLowerCase().equals(file_ext.toLowerCase())){
                return true;
            }
        }
        
        return false;
    }
   
    /**
     * 校验aformfile对象的后缀名,
     * @param aformfile 实现FormFile接口的对象 
     * @param file_ext 文件扩展名数组,如.jpg, .rar, .zip。
     * @return true:是该扩展名数组中存在的后缀名 false:不是该扩展名数组中存在的后缀名
     */
    public static boolean validateFileName(FormFile aformfile,String[] file_ext){
        if (aformfile == null
                ||StringUtil.isEmpty(aformfile.getFileName())
                ||aformfile.getFileSize()==0) {
            return false;
        }
        if (file_ext == null||file_ext.length==0){
            return false;
        }
        
        String file_name = aformfile.getFileName();
        
        int s_id = file_name.lastIndexOf(".");
        
        if (s_id == -1) {
            return false;
        }
        
        String ext = file_name.substring(s_id);
        System.out.println("File Ext:"+ext);
        
        int size = file_ext.length;
        
        for(int i=0;i<size;i++){
           if (ext.toLowerCase().equals(file_ext.toLowerCase())){
                return true;
            }
        }
        
        return false;
    }
   
    /**
     * 校验文件大小
     * @param size 要求的文件大小
     * @return false 超过文件size true 小于或等于文件size 
     */
    public boolean validateFileSize(long size){
        if (file_info == null
                ||StringUtil.isEmpty(file_info.getFileName())
                ||file_info.getFileSize()==0) {
            return false;
        }
        if (size<=0){
            return false;
        }
        
        
        return ((long)(file_info.getFileSize()))<=size ;
        
    }
   
    /**
     * 校验文件对象aformfile大小
     * @param aformfile 文件信息对象
     * @param size 要求的文件大小
     * @return false 超过文件size true 小于或等于文件size
     */
    public static boolean validateFileSize(FormFile aformfile,long size){
        if (aformfile == null
                ||StringUtil.isEmpty(aformfile.getFileName())
                ||aformfile.getFileSize()==0) {
            return false;
        }
        if (size<=0){
            return false;
        }
        
        
        return ((long)(aformfile.getFileSize()))<=size ;
        
    }
   
    /**
     *  将文件存入服务器。注意,需要将数据拷贝到服务器文件中
     * @param path 文件目录
     * @param filename 文件名称
     * @return 如果发生异常,将传回null,否则传回服务器文件名称。
     * @throws FileNotFoundException 未找到文件或服务器目录时
     * @throws IOException 读写数据异常时
     */
    public String saveFileToServer(String path,String filename)
    throws FileNotFoundException,IOException{
        
        return saveFileToServer(path+filename);
    }
   
    /**
     *  将文件存入服务器。注意,需要将数据拷贝到服务器文件中
     * @param path 文件目录
     * @param filename 文件名称
     * @return 如果发生异常,将传回null,否则传回服务器文件名称。
     * @throws FileNotFoundException 未找到文件或服务器目录时
     * @throws IOException 读写数据异常时
     */
    public String saveFileToServer(File path,String filename)
    throws FileNotFoundException,IOException{
        return saveFileToServer(path.getAbsolutePath()+filename);
    }
   
    /**
     * 将文件存入服务器。注意,需要将数据拷贝到服务器文件中
     * @param full_filename 需要存入的文件
     * @return 如果发生异常,将传回null,否则传回服务器文件名称。
     * @throws FileNotFoundException 未找到文件或服务器目录时
     * @throws IOException 读写数据异常时
     */
    public String saveFileToServer(String full_filename)
    throws FileNotFoundException,IOException{
        
        if (StringUtil.isEmpty(full_filename)){
            return null;
        }
        
        File file = null;
        //检查是否是文件
        try{
            
            if (!(new File(full_filename).isFile())){
                return null;
            }else{
                file = new File(full_filename);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
        
        return file==null?null:saveFileToServer(file);
        
    }
   
   
    /**
     * 将文件存入服务器。注意,需要将数据拷贝到服务器文件中
     * @param full_filename 需要存入的文件
     * @return 如果发生异常,将传回null,否则传回服务器文件名称。
     * @throws FileNotFoundException 未找到文件或服务器目录时
     * @throws IOException 读写数据异常时
     */
    public String saveFileToServer(File full_filename)
    throws FileNotFoundException,IOException{
        
        if (file_info == null
                ||StringUtil.isEmpty(file_info.getFileName())
                ||file_info.getFileSize()==0) {
            return null;
        }
        
        //判断是否是jsp文件及服务器是否有重名文件
        
        if (full_filename.getName().lastIndexOf(".jsp")!=-1
                ||full_filename.getName().lastIndexOf(".js")!=-1){
            return null;
        }
        
        if (full_filename.exists()){//如有重名文件,删除旧文件
            full_filename.delete();
        }
        
        try{
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InputStream stream = file_info.getInputStream();
            //write the file to the file specified
            OutputStream bos = new FileOutputStream(full_filename);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            //close the stream
            stream.close();
        } catch (FileNotFoundException fex) {
            fex.printStackTrace();
            return null;
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
            return null;
        }
        
        return full_filename.getName();
    }
   
    /**
     *  将aformfile存入指定的文件
     * @param aformfile 实现FormFile接口的对象
     * @param full_filename 服务器文件
     * @return 文件名
     * @throws FileNotFoundException 如果未找到文件
     * @throws IOException 如果读写异常
     */
    public static String saveFileToServer(FormFile aformfile,String full_filename)
    throws FileNotFoundException,IOException{
        
        if (aformfile == null
                ||StringUtil.isEmpty(aformfile.getFileName())
                ||aformfile.getFileSize()==0) {
            return null;
        }
        
        if (StringUtil.isEmpty(full_filename)){
            return null;
        }
        
        File file = null;
        //检查是否是文件
        try{
            
            if (!(new File(full_filename).isFile())){
                return null;
            }else{
                file = new File(full_filename);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            return null;
        }
        
        return    file==null?null:saveFileToServer(aformfile,file);
    }
   
    /**
     *  将aformfile存入指定的文件
     * @param aformfile 实现FormFile接口的对象
     * @param full_filename 服务器文件
     * @return 文件名
     * @throws FileNotFoundException 如果未找到文件
     * @throws IOException 如果读写异常
     */
    public static String saveFileToServer(FormFile aformfile,File full_filename)
    throws FileNotFoundException,IOException{
        if (aformfile == null
                ||StringUtil.isEmpty(aformfile.getFileName())
                ||aformfile.getFileSize()==0) {
            return null;
        }
        
        if (full_filename == null){
            return null;
        }
        
        //判断是否是jsp文件及服务器是否有重名文件
        
        
        if (full_filename.getName().lastIndexOf(".jsp")!=-1
                ||full_filename.getName().lastIndexOf(".js")!=-1){
            return null;
        }
        
        if (full_filename.exists()){//如有重名文件,删除旧文件
            full_filename.delete();
        }
        
        try{
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InputStream stream = aformfile.getInputStream();
            //write the file to the file specified
            OutputStream bos = new FileOutputStream(full_filename);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            //close the stream
            stream.close();
        } catch (FileNotFoundException fex) {
            fex.printStackTrace();
            return null;
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
            return null;
        }
        
        return full_filename.getName();
    }
    /**
     * 销毁文件信息对象(实现该接口的对象)
     *
     */
    public void destroy(){
        if (file_info!=null){
            file_info.destroy();
            file_info = null;
        }
        
    }
    /**
     * 销毁文件信息对象(实现该接口的对象)
     * @param aformfile 文件信息对象
     */
    public static void destroy(FormFile aformfile){
        if (aformfile!=null){
            aformfile.destroy();
            aformfile = null;
        }
    }
   
    public static boolean deleteFile(File full_filename){
        if (full_filename!=null){
            return full_filename.delete();
        }
        return false;
    }
   
    public static boolean deleteFile(String full_filename){
        if (full_filename == null) return false;
        return deleteFile(new File(full_filename));   
    }
   
   
   
   
    /**
     * @return Returns the file_info.
     */
    public FormFile getFile_info() {
        return file_info;
    }
    /**
     * @param file_info The file_info to set.
     */
    public void setFile_info(FormFile file_info) {
        this.file_info = file_info;
    }
}



[此贴子已经被作者于2005-1-11 10:14:49编辑过]

回复

使用道具 举报

 楼主| 发表于 2005-1-11 10:18:21 | 显示全部楼层
这个类还未经过测试,因为手头的项目己经到后期了。还是不要用它,

不过,重新审视自己写过的代码。不得不承认,

自己还未能领会面向对象所带来的好处,


交流,真的很重要。
现在很少到一些技术性的论坛呢,忙象是一个借口,
回复

使用道具 举报

 楼主| 发表于 2005-1-11 10:32:08 | 显示全部楼层
现在才明白,为什么会有private,public,有static、final之类的关键字,

这些关键字是在一组类协同工作的时候,尤为紧要,
回复

使用道具 举报

发表于 2005-1-11 12:38:04 | 显示全部楼层
可惜你的代码是jsp的,这个论坛是asp的,用不了 !
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 申请新用户

本版积分规则

守望轩 ( 湘ICP备17013730号-2 )|网站地图

GMT+8, 2024-4-20 18:57 , Processed in 0.027133 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表