《重构-改善代码既有的设计》重构,第一个案例

这篇博客详细记录了一次重构过程,首先介绍了编写Movie、Rental和Customer三个类的代码,然后通过一系列重构步骤,包括提炼方法、移动方法、以查询取代临时变量等,逐步改善代码设计。重构过程中使用了Myeclipse 2015编译器,并最终实现了用Price类来处理不同类型的影片价格,应用了多态原则。
摘要由CSDN通过智能技术生成

起点:编写3个类的代码
1、第一个类-影片(Movie):

package com.lee.test.aFirstExample;

public class Movie {
   

    /**
     * @param title
     * @param priceCode
     */
    public Movie(String title, int priceCode) {
        super();
        this.title = title;
        this.priceCode = priceCode;
    }

    public static final int childrens = 2;
    public static final int regular = 0;
    public static final int new_release = 1;

    private String title;
    private int priceCode;



    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getPriceCode() {
        return priceCode;
    }
    public void setPriceCode(int priceCode) {
        this.priceCode = priceCode;
    }


}

2、第二个类-租赁(Rental):

package com.lee.test.aFirstExample;

public class Rental {
   
    /**
     * @param movie
     * @param dayRented
     */
    public Rental(Movie movie, int dayRented) {
        super();
        this.movie = movie;
        this.dayRented = dayRented;
    }

    private Movie movie;
    public Movie getMovie() {
        return movie;
    }

    private int dayRented;
    public int getDayRented() {
        return dayRented;
    }


}

3、第三个类-消费者(Customer):

package com.lee.test.aFirstExample;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {
   
    /**
     * @param name
     */
    public Customer(String name) {
        super();
        this.name = name;
    }
    private String name;
    public String getName() {
        return name;
    }
    private Vector rentals = new Vector();

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

    public String statement(){
        double totalAmount = 0;
        int frequentRenterPoints = 0;
        Enumeration rentalss = rentals.elements();
        String result = "Rental Record for" +" "+ getName()+"\n";
        while(rentalss.hasMoreElements())
        {
            double thisAmount = 0;
            Rental each = (Rental) rentalss.nextElement();

            switch(each.getMovie().getPriceCode())
            {
                case Movie.regular:
                thisAmount += 2;
                if(each.getDayRented()>2)
                    thisAmount += (each.getDayRented()-2)*1.5; 
                break;

                case Movie.new_release:
                thisAmount += each.getDayRented()*3;
                break;

                case Movie.childrens:
                    thisAmount += 1.5;
                    if(each.getDayRented()>3)
                        thisAmount += (each.getDayRented()-3)*1.5; 
                    break;
            }

            //积分  每借一张加1个积分
            frequentRenterPoints++;
            //积分累加条件  新版本的片子,借的时间大于1天
            if((each.getMovie().getPriceCode()==Movie.new_release)&&each.getDayRented()>1)
            {
                frequentRenterPoints++;
            }

            result +="\t" +each.getMovie().getTitle()+"\t" 
                    +String.valueOf(thisAmount)+"\n";

            totalAmount += thisAmount;
        }

        result += "Amount owed is "+ String.valueOf(totalAmount)+"\n";
        result +="You earned "+String.valueOf(frequentRenterPoints)+" "
                +"frequent renter points";
        return result;
    }


}

4、自己定义客户端(Client)。

package com.lee.test.aFirstExample;

public class Client {

    public static void main(String[] args) {

        Movie mov = new Movie("metal",2);
        Rental ren = new Rental(mov,8);
        Customer cus = new Customer("Lee");
        cus.addRental(ren);

        System.out.println(cus.statement());


    }


}

5、输出结果

Rental Record for Lee
    metal   9.0
Amount owed is 9.0
You earned 1 frequent renter points

6、重构第一步
提炼方法(Extract Method)
提炼Switch/Case分支到一个方法,修改后Customer代码:

package com.lee.test.aFirstExample;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {
   
    /**
     * @param name
     */
    public Customer(String name) {
        super();
        this.name = name;
    }
    private String name;
    public String getName() {
        return name;
    }
    private Vector rentals = new Vector();

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

    public String statement(){
        double totalAmount = 0;
        int frequentRenterPoints = 0;
        Enumeration rentalss = rentals.elements();
        String result = "Rental Record for" +" "+ getName()+"\n";
        while(rentalss.hasMoreElements())
        {
            double thisAmount = 0;
            Rental each = (Rental) rentalss.nextElement();

            thisAmount = amountFor(each);  //提炼方法  
            /*switch(each.getMovie().getPriceCode())
            {
                case Movie.regular:
                thisAmount += 2;
                if(each.getDayRented()>2)
                    thisAmount += (each.getDayRented()-2)*1.5; 
                break;

                case Movie.new_release:
                thisAmount += each.getDayRented()*3;
 
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
一直很喜欢重构这本书,但是由于自己记性不太好,书看过之后其中的方法总是记不住,于是想如果有电子版的重构书就好了,工作中遇到重构的问题可以随时打开查阅。在网上搜索了许久,发现重构这本书有英文chm版本的,而中文版的电子书只有扫描的PDF版本,用起来非常不方便。于是萌生想做一本重构工具书的想法,本来打算自己重新将重构书的内容再整理归类一下,后来发现原书的目录编排就很适合做工具书,包括坏味道分类,重构手法归类等,都有了一个比较系统的整理。因此,我利用空余时间制作了这样的一本中文的chm版重构,希望对大家有所帮助,也算对中国软件业做出一点小小的贡献。 本书基本上是取自”重构”中文版一书的内容,但格式上参照的是chm英文版的格式,还有一些格式小修改,比如第一章重构前后代码对比。因为时间匆促,个人能力有限,本书难免存在一些缺漏,如果大家发现有问题,随时可以给我发邮件,我会尽快更新错误的内容。 最后再次感谢几位大师 Martin Fowler、Kent Beck等,还有翻译的侯捷和熊节先生,为我们带来这么精彩的一本书。谢谢。 免责声明:本书仅供个人学习研究之用,不得用于任何商业目的,不得以任何方式修改本作品,基于此产生的法律责任本人不承担任何连带责任。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值