码迷,mamicode.com
首页 > 其他好文 > 详细

【重构】【重构:第一个案例】

时间:2017-03-22 22:45:11      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:多态   逻辑   children   return   regular   价格   static   logs   void   

【起点】

影片类

// 简单的纯数据类
public class Movie {
    public static final int CHILDREN = 2;
    public static final int REGULAR = 0;
    public static final int NEW_RELEASE = 1;

    private String _title;
    private int _priceCode;

    public Movie(String title, int priceCode) {
        _title = title;
        _priceCode = priceCode;
    }

    public String getTitle() {
        return _title;
    }

    public int getPriceCode() {
        return _priceCode;
    }

    public void set_title(String _title) {
        this._title = _title;
    }
}

租赁类

// 表明某个顾客租了一部影片
public class Rental {
    private Movie _movie;
    private int _daysRent;

    public Rental(Movie movie, int daysRent) {
        _movie = movie;
        _daysRent =daysRent;
    }

    public Movie getMovie() {
        return _movie;
    }

    public int getDaysRent() {
        return _daysRent;
    }
}

顾客类

import java.util.Vector;

public class Customer {
    private String _name;
    private Vector _rentals = new Vector();

    public Customer(String name) {
        _name = name;
    }

    public void addRental(Rental arg) {
        _rentals.addElement(arg);
    }

    public String getName() {
        return _name;
    }
}

 

【重构的第一步】

【分解并重组Statement】

【运用多态(polymorphism)取代与价格相关的条件逻辑】

【结语】

 

【重构】【重构:第一个案例】

标签:多态   逻辑   children   return   regular   价格   static   logs   void   

原文地址:http://www.cnblogs.com/xkxf/p/6602381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!