• 从头认识java-1.4 复用代码


    这一章节我们来讨论一下复用代码。

    1.为什么?

    这一点我也不详细展开了,随便说说都可以说出几个理由。

    笔者总结了一下:

    对于老板:降低成本,缩短开发时间,追求利润最大化

    对于teamleader:加快进度,压缩工期,最短时间交付最多项目

    对于程序猿:不用加班

    2.怎么做?

    笔者分成四个方面来描述:

    1)在方法层面:

    例如:我们平常在很多地方都需要时间这个东西,但是我们就可能复制粘贴下面的代码

    Calendar calendar = Calendar.getInstance();
    int thisMonth = calendar.get(Calendar.MONTH)+1;

    搞到代码里面各处都是获取时间,这个时候其实我们可以抽取些代码,形成时间类

    /**
     * 处理日期时间工具类
     * @author ray
     */
    public class DateUtil {
    	/**
    	 * @return 获得当前Calendar
    	 */
    	public static Calendar getCalendar(){
    		Calendar calendar = Calendar.getInstance();
    		return calendar;
    	}
    	/**
    	 * @return 获得今年
    	 */
    	public static int getThisYear(){
    		return getCalendar().get(Calendar.YEAR);
    	}
    	/**
    	 * @return 获得本月
    	 */
    	public static int getThisMonth(){
    		return getCalendar().get(Calendar. MONTH)+1;
    	}
    	/**
    	 * @return 获得当前时间
    	 */
    	public static Date getNow(){
    		return getCalendar().getDate();
    	}
    }

    又例如:在系统里面我们需要处理servlet传回来的数据

    double amount = Double.valueOf(req.getParameter(“amount”)).doubleValue();
    int count = Integer.valueOf(req.getParameter(“count”)).intValue();
    String name = new String(req.getParameter(“name”).getBytes(),"GBK");
    Date now = DateUtil.getDate(req.getParameter(“now”),"yyyy-MM-dd");

    我们可以抽象成一个转换器的类

    /**
     * 默认的读取器,读取POST提交的数据
     * @author raylee
     */
    public class Converter {
    	private HttpServletRequest req = null;
    	/* 
    	 * 转换器
    	 * @param HttpServletRequest
    	 */
    	public Converter(HttpServletRequest req) throws IOException {
    		this.req = req;
    	}
    	/* 
    	 * @param 名称
    	 * @return the data of Object
    	 */
    	public Object getData(String name) throws IOException {
    		Object data = req.getParameter(name);
    		if (data==null) {
    			throw new IOException("转换器失败");
    		}
    		return data;
    	}
    	/* 
    	 * @param 名称
    	 * @return 返回double
    	 */
    	public double getDouble(String name) throws IOException {
    		return Double.valueOf(getString(name)).doubleValue();
    	}
    	/* 
    	 * @param 名称
    	 * @return 返回整数
    	 */
    	public int getInteger(String name) throws IOException {
    		return Integer.valueOf(getString(name)).intValue();
    	}
    	/* 
    	 * @param 名称
    	 * @return 返回字符串
    	 */
    	public String getString(String name) throws IOException {
    		return getData(name).toString();
    	}
    	/* 
    	 * @param 名称
    	 * @return 返回GBK编码
    	 */
    	public String getChinese(String name) throws IOException {
    		return new String(getString(name).getBytes(),"GBK");
    	}
    
    }


    2)在类层面:

    (1)聚合

    我们看下面的例子:

    package com.ray.ch01;
    
    public class Bird {
    
    	private String name;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		if (name != null) {
    			this.name = name;
    		} else {
    			System.out.println("名称不能为空");
    		}
    	}
    }
    

    先建立一个Bird这一个类。

    然后jack和rose同时养了一只小鸟

    package com.ray.ch01;
    
    public class Jack {
    	private Bird bird;
    
    	public Bird getBird() {
    		return bird;
    	}
    
    	public void setBird(Bird bird) {
    		this.bird = bird;
    	}
    }
    

    package com.ray.ch01;
    
    public class Rose {
    	private Bird bird;
    
    	public Bird getBird() {
    		return bird;
    	}
    
    	public void setBird(Bird bird) {
    		this.bird = bird;
    	}
    }
    

    聚合,其实就是has-a关系,上面也是代码复用之一,两者复用了Bird这个类。


    (2)继承

    我们看下面的两个类

    歌唱家:

    package com.ray.ch01;
    
    public class Singer {
    	private long id;
    	private String name;
    	private String sex;
    
    	public long getId() {
    		return id;
    	}
    
    	public void setId(long id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getSex() {
    		return sex;
    	}
    
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    
    	@Override
    	public String toString() {
    		return "id:" + getId() + ";name:" + getName() + ";sex:" + getSex();
    	}
    
    	public void sing() {
    
    	}
    
    }
    

    拳击手:

    package com.ray.ch01;
    
    public class Boxer {
    	private long id;
    	private String name;
    	private String sex;
    
    	public long getId() {
    		return id;
    	}
    
    	public void setId(long id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getSex() {
    		return sex;
    	}
    
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    
    	@Override
    	public String toString() {
    		return "id:" + getId() + ";name:" + getName() + ";sex:" + getSex();
    	}
    
    	public void fight() {
    
    	}
    
    }
    

    从上面的两个类可以看出,大家的代码几乎一样,这个时候我们可以抽象出一个人Person的类来,然后让singer和boxer来继承他

    这里涉及的继承概念我们下一章节将详细展开。

    package com.ray.ch01;
    
    public class Person {
    	private long id;
    	private String name;
    	private String sex;
    
    	public long getId() {
    		return id;
    	}
    
    	public void setId(long id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getSex() {
    		return sex;
    	}
    
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    
    	@Override
    	public String toString() {
    		return "id:" + getId() + ";name:" + getName() + ";sex:" + getSex();
    	}
    }
    

    我们将共性的代码放到里面,然后其他两个实现类将实现各自独有的代码即可。

    package com.ray.ch01;
    
    public class Boxer extends Person {
    
    	public void fight() {
    
    	}
    
    }
    

    package com.ray.ch01;
    
    public class Singer extends Person {
    
    	public void sing() {
    
    	}
    
    }
    

    继承:is-a关系,代码复用的重要手段。


    3)在配置层面:

    例如我们平常使用框架,一般都有配置文件,我们可以把这些配置文件集中管理,然后重复使用


    4)在业务层面:

    例如,现在企业内部存在多系统情况,但是一般来说每个系统都需要有登陆模块,我们可以把这个登陆模块单独抽取出来,变成构件,这样就可以重复使用。


    总结:这一章节主要讨论了如何复用代码。


    这一章节就到这里,谢谢。

    -----------------------------------

    目录


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Python学习笔记模式匹配与正则表达式之使用和不使用正则表达式
    关于Jqury的一些杂碎
    客户端验证模型
    导航(摘)
    解决了DIV+CSS一个问题
    购买了新书《Bootstrap用户手册—设计响应式网站》及简介Bootstrap是什么
    博客园的博客页面开通了,今天!
    利用Cmake 将最新版本OBS编译成windows版本。
    C语言无法使用引用,一定要使用怎么办? ------指针的指针做参数
    linux core文件的打开和分析
  • 原文地址:https://www.cnblogs.com/raylee2007/p/4896703.html
Copyright © 2020-2023  润新知