不良代码展示-父类中可以抽象出子类的行为

本文探讨了一种将业务逻辑从代码中剥离的方法,通过定义抽象类和具体实现来改善代码结构。原始代码中,基类的判断逻辑被用于不同子类的行为,导致扩展性不佳且不易维护。通过将行为定义为抽象函数并由子类实现,可以显著提高代码的可读性和可维护性。示例代码展示了如何将具体的业务逻辑分离到各个子类中,以实现更清晰、灵活和易于扩展的设计。

原创文章,如有转载,请注明出处:http://blog.youkuaiyun.com/yihui823/article/details/6932952


把一段代码,业务逻辑剥离后,给大家看看。

基类:

package testjava;

/**
 * 书的基类
 */
public class BaseBook {
    private int type = -1;
    
    public BaseBook(int type) {
        this.type = type;
    }
    
    /**
     * 打印页面
     */
    public void printPage() {
        if (type == 1) {
            System.out.println("Print with the green color.");
        } else if (type == 2) {
            System.out.println("Print with the blue color.");
        } else {
            System.out.println("Print with the white color.");
        }
    }
}

子类1:

package testjava;

/**
 * 绿色的书
 */
public class GreenBook extends BaseBook {
    public GreenBook(int type) {
        super(type);
    }
    
    /**
     * 得到颜色
     * @return 颜色 
     */
    public String getColor() {
        return "Green";
    }
}

子类2:

package testjava;

/**
 * 蓝色的书
 */
public class BlueBook extends BaseBook {
    
    public BlueBook(int type) {
        super(type);
    }
    
    /**
     * 得到颜色
     * @return 颜色 
     */
    public String getColor() {
        return "Blue";
    }
}

调用的类:

package testjava;

/**
 * 执行类
 */
public class Testjava {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BlueBook bb = new BlueBook(2);
        bb.printPage();
        
        GreenBook gb = new GreenBook(1);
        gb.printPage();
    }
}

运行结果当然是对的。可是,子类的行为,却定义在了父类之中了。也就是说,基类的“书”,却根据每本书的颜色去做行为判断的。

        if (type == 1) {
            System.out.println("Print with the green color.");
        } else if (type == 2) {
            System.out.println("Print with the blue color.");
        } else {
            System.out.println("Print with the white color.");
        }

这个结构,扩展性不好,也不好维护,也不好阅读。

问当事人,当事人说,我在父类没法知道是哪个子类啊。但是这个业务是可以抽取的,所以就写成这个样子了。

其实,可以这么写的。

基类:

package testjava.right;

/**
 * 书的基类
 */
public abstract class BaseBook {
    
    public abstract String getColor();
    
    /**
     * 打印页面
     */
    public void printPage() {
        System.out.println("Print with the " + getColor() + " color.");
    }
}

子类1:

package testjava.right;

/**
 * 蓝色的书
 */
public class BlueBook extends BaseBook {
    
    /**
     * 得到颜色
     * @return 颜色 
     */
    public String getColor() {
        return "Blue";
    }
}

子类2:

package testjava.right;

/**
 * 绿色的书
 */
public class GreenBook extends BaseBook {

    /**
     * 得到颜色
     * @return 颜色 
     */
    public String getColor() {
        return "Green";
    }
}

执行类:

package testjava.right;

/**
 * 执行类
 */
public class Testjava {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BlueBook bb = new BlueBook();
        bb.printPage();
        
        GreenBook gb = new GreenBook();
        gb.printPage();
    }
}

把属于子类的行为,定义成抽象函数,由子类去实现。OK











评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值