没有单元测试,何谈重构(2)

发表于:2017-03-31来源:jianshu作者:溪石iOS点击数: 标签:重构
//// Demand.h// RefactorDemo//// Created by xishi on 16/10/29.// Copyright ? 2016年 xs. All rights reserved.// #import Foundation/Foundation.h typedef NS_ENUM(NSUInteger, TimePeriodEnum) { TimePeriod

 

//
//  Demand.h
//  RefactorDemo
//
//  Created by xishi on 16/10/29.
//  Copyright ? 2016年 xs. All rights reserved.
//

#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, TimePeriodEnum) {
TimePeriodEnumWorkDaytime = 1,
TimePeriodEnumWorkNight = 2,
TimePeriodEnumWeekend = 3
};

@class Movie;
@interface Demand : NSObject
@property(nonatomic) Movie *movie;
@property(nonatomic, assign) int timePeriod;

- (id)initWithMovie:(Movie *)movie
timePeriod:(TimePeriodEnum)timePeriod;
@end

 

 

//
//  Demand.m
//  RefactorDemo
//
//  Created by xishi on 16/10/29.
//  Copyright ? 2016年 xs. All rights reserved.
//

#import "Demand.h"
#import "Movie.h"

@implementation Demand
- (id)initWithMovie:(Movie *)movie
timePeriod:(TimePeriodEnum)timePeriod {
self = [super init];
if (self) {
_movie = movie;
_timePeriod = timePeriod;
}
return self;
}
@end

 

顾客类:

 

//
//  Customer.h
//  RefactorDemo
//
//  Created by xishi on 16/10/29.
//  Copyright ? 2016年 xs. All rights reserved.
//

#import <Foundation/Foundation.h>

@class Demand;
@interface Customer : NSObject
- (id)initCustomerWithName:(NSString *)name;
- (void)addDemand:(Demand *)demand;
- (NSString *)statement;
@end

 

 

//
//  Customer.m
//  RefactorDemo
//
//  Created by xishi on 16/10/29.
//  Copyright ? 2016年 xs. All rights reserved.
//

#import "Customer.h"
#import "Demand.h"
#import "Movie.h"
@interface Customer () {
NSString *_name;
NSMutableArray *_demands;
}
@end
@implementation Customer
- (id)initCustomerWithName:(NSString *)name {
self = [super init];
if (self) {
_name = name;
}
return self;
}

- (void)addDemand:(Demand *)demand {
if (!_demands) {
_demands = [[NSMutableArray alloc] init];
}
[_demands addObject:demand];
}

- (NSString *)statement {
double totalAmount = 0;
int frequentDemandPotnts = 0;
NSMutableString *result = [NSMutableString stringWithFormat:@"%@的点播清单\\\\n", _name];
for (Demand *aDemand in _demands) {
double thisAmount = 0;

// 根据不同电影定价:
switch (aDemand.movie.priceCode) {
case MovieEnumRegular:
thisAmount += 2; // 普通电影2元一次
break;

case MovieEnumNewRelease:
thisAmount += 3; // 新电影3元一次
break;

case MovieEnumChildrens:
thisAmount += 1.5; // 儿童电影1.5元一次
}

// 根据不同时段定价:
if (aDemand.timePeriod == TimePeriodEnumWorkDaytime)
thisAmount *= 1.0; // 工作日全价
else
if (aDemand.timePeriod == TimePeriodEnumWeekend) {
thisAmount *= 0.5; // 周末半价
}
else
if (aDemand.timePeriod == TimePeriodEnumWorkNight){
thisAmount *= 1.5; // 下班1.5倍
}

frequentDemandPotnts++;
// 周末点播新片积分翻倍:
if ((aDemand.movie.priceCode == MovieEnumNewRelease) &&
aDemand.timePeriod == TimePeriodEnumWeekend) {
frequentDemandPotnts++;
}

[result appendFormat:@"\\\\t%@\\\\t%@ 元\\\\n", aDemand.movie.title, @(thisAmount)];
totalAmount += thisAmount;
}

[result appendFormat:@"费用总计 %@ 元\\\\n", @(totalAmount).stringValue];
[result appendFormat:@"获得积分 %@", @(frequentDemandPotnts).stringValue];

return result;
}
@end

原文转自:http://www.jianshu.com/p/0b4fd636ad2c