开发者

Should I "Pull Up" Refactor

I have some very small classes that I feel should be "pulled up" but the methods are so small I'm not sure. For example, the only thing that's meaningfully different is the body of the buildFromJSON: selector.

I acknowledge that this is similar to: Pull-up refactoring, Objective-C but I feel my question is specific to refactoring very small classes/methods.

Also, not sure it relates to my particular code example, but I'm wondering if a child class says it conforms to a protocol, whether it's enough that it's parent actually supply the implementation of required selector(s)?

@implementation AsyncFoo

-(void)dealloc {
    [clientDelegate release];
    [super dealloc];
}

- (id)initWithDelegate: (id <ServiceClientProtocol>) delegate {
    if((self = [super init])) {
        clientDelegate = [delegate retain];
    }
    return self;
}

- (void)buildFromJSON:(NSString*)jsonResponseString {
    [clientDelegate serviceComplete:[RestAdapter buildFooArray: jsonResponseString]];
}

@end



@implementation AsyncBar

-(void)dealloc {
    [clientDelegate release];
    [super dealloc];
}

- (id)initWithDelegate: (id <ServiceClientProtocol>) delegate {
    if((self = [super init])) {
        clientDelegate = [delegate retain];
    }
    return self;
}

- (void)buildFromJSON:(NSString*)jsonResponseString {
    [clientDelegate serviceComplete:[RestAdapter buildBarArray:jsonResponseString]];
}

@end

Answers including code example would be great.

EDIT: Post accepted answer I'd like to add that since I was able to subclass, the derived classes did not need to declare that they conformed to protocol:

@interface Async : NSObject <ModelBuilderProtocol> {
    id <ServiceClient开发者_如何转开发Protocol> clientDelegate;
}
- (void)buildFromJSON:(NSString*)jsonResponseString;
@end

@interface AsyncArtistById : Async
@end 


You don't normally retain your delegates as this can cause a retain cycle.

Knowing what I know from looking at your example I would probably implement like this:

The super class

// Async.h
@interface Async : NSObject

@property (nonatomic, assign) id<ServiceClientProtocol> delegate;

- (void)buildFromJSON:(NSString *)jsonResponseString;

@end

// Async.m
@implementation Async

@synthesize delegate = _delegate;

- (id)initWithDelegate:(id<ServiceClientProtocol>)delegate 
{
    self = [super init];
    if(self) {
        _delegate = delegate;
    }
    return self;
}

- (void)buildFromJSON:(NSString *)jsonResponseString
{
    // This will ensure that we over ride this method in a sub class
    [NSException raise:NSInternalInconsistencyException 
        format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)];
}

@end

Concrete subclass AsyncFoo

// AsyncFoo.h
@interface AsyncFoo : Async
@end

// AsyncFoo.m
@implementation AsyncFoo

- (void)buildFromJSON:(NSString *)jsonResponseString 
{
    [self.delegate serviceComplete:[RestAdapter buildFooArray: jsonResponseString]];
}

@end

Concrete subclass AsyncBar

// AsyncBar.h
@interface AsyncBar : Async
@end

// AsyncBar.m
@implementation AsyncBar

- (void)buildFromJSON:(NSString *)jsonResponseString {
    [self.delegate serviceComplete:[RestAdapter buildBarArray:jsonResponseString]];
}

@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜