设计思路
- 定义状态基类:创建一个基类表示订单状态,定义通用的方法,如获取当前状态描述、处理特定操作等。
- 创建具体状态类:继承自状态基类,针对每个订单状态(待支付、已支付、已发货、已完成、已取消)创建具体的状态类,重写基类方法来实现每个状态下的特定行为。
- 订单类:包含一个状态对象的引用,根据不同的操作切换状态对象,从而实现状态的转换。
核心代码片段
- 状态基类
#import <Foundation/Foundation.h>
// 订单状态基类
@interface OrderState : NSObject
- (NSString *)stateDescription;
- (void)handlePayment:(NSObject *)order;
- (void)handleViewLogistics:(NSObject *)order;
@end
@implementation OrderState
- (NSString *)stateDescription {
return @"未知状态";
}
- (void)handlePayment:(NSObject *)order {
NSLog(@"当前状态不支持支付操作");
}
- (void)handleViewLogistics:(NSObject *)order {
NSLog(@"当前状态不支持查看物流操作");
}
@end
- 具体状态类 - 待支付
#import "OrderState.h"
@interface PendingPaymentState : OrderState
- (NSString *)stateDescription;
- (void)handlePayment:(NSObject *)order;
@end
@implementation PendingPaymentState
- (NSString *)stateDescription {
return @"待支付";
}
- (void)handlePayment:(NSObject *)order {
// 处理支付逻辑,这里简单示例状态转换
NSLog(@"支付成功,订单状态转换为已支付");
// 假设order有一个方法来切换状态
[(id)order changeState:[[PaidState alloc] init]];
}
@end
- 具体状态类 - 已支付
#import "OrderState.h"
@interface PaidState : OrderState
- (NSString *)stateDescription;
- (void)handleViewLogistics:(NSObject *)order;
@end
@implementation PaidState
- (NSString *)stateDescription {
return @"已支付";
}
- (void)handleViewLogistics:(NSObject *)order {
// 这里简单示例,假设已发货才支持查看物流,先判断是否可以转换到已发货状态
NSLog(@"订单尚未发货,暂不支持查看物流");
// 这里可以添加逻辑判断是否可以转换到已发货状态,比如商家操作等
}
@end
- 具体状态类 - 已发货
#import "OrderState.h"
@interface ShippedState : OrderState
- (NSString *)stateDescription;
- (void)handleViewLogistics:(NSObject *)order;
@end
@implementation ShippedState
- (NSString *)stateDescription {
return @"已发货";
}
- (void)handleViewLogistics:(NSObject *)order {
NSLog(@"查看物流信息...");
}
@end
- 具体状态类 - 已完成
#import "OrderState.h"
@interface CompletedState : OrderState
- (NSString *)stateDescription;
@end
@implementation CompletedState
- (NSString *)stateDescription {
return @"已完成";
}
@end
- 具体状态类 - 已取消
#import "OrderState.h"
@interface CancelledState : OrderState
- (NSString *)stateDescription;
@end
@implementation CancelledState
- (NSString *)stateDescription {
return @"已取消";
}
@end
- 订单类
#import <Foundation/Foundation.h>
#import "OrderState.h"
@interface Order : NSObject
@property (nonatomic, strong) OrderState *currentState;
- (instancetype)initWithState:(OrderState *)state;
- (void)changeState:(OrderState *)state;
- (void)handlePayment;
- (void)handleViewLogistics;
@end
@implementation Order
- (instancetype)initWithState:(OrderState *)state {
self = [super init];
if (self) {
_currentState = state;
}
return self;
}
- (void)changeState:(OrderState *)state {
_currentState = state;
NSLog(@"订单状态已转换为: %@", [_currentState stateDescription]);
}
- (void)handlePayment {
[_currentState handlePayment:self];
}
- (void)handleViewLogistics {
[_currentState handleViewLogistics:self];
}
@end
使用示例
#import <UIKit/UIKit.h>
#import "Order.h"
#import "PendingPaymentState.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
Order *order = [[Order alloc] initWithState:[[PendingPaymentState alloc] init]];
NSLog(@"当前订单状态: %@", [order.currentState stateDescription]);
[order handlePayment];
NSLog(@"当前订单状态: %@", [order.currentState stateDescription]);
[order handleViewLogistics];
}
return 0;
}