Pull members up, push members down
The Pull Members Up refactoring allows you to move class members to a superclass or protocol.
The Push Members Down refactoring helps you clean up the class hierarchy by moving class members to a subclass. The members are then relocated into the direct subclasses only.
Select an item you want to pull up or push down.
From the main or context menu, select
or .In the dialog that opens, depending on your refactoring, specify a destination object, members you want to move, and other additional information.
Click Refactor.
Code examples
Pull members up
Before | After |
---|---|
// All methods of the Dog class
// will be moved to a superclass
// Dog.h
@interface Dog : NSObject
- (void)run;
- (void)eat;
@end
// Dog.m
@implementation Dog {
}
-(void)run {
NSLog(@"I am running");
}
- (void)eat {
NSLog(@"I am eating");
}
@end
|
// New superclass
// Animal.h
@interface Animal : NSObject
- (void)run;
- (void)eat;
@end
// Animal.m
@implementation Animal {
}
-(void)run {
NSLog(@"I am running");
}
- (void)eat {
NSLog(@"I am eating");
}
@end
// The Dog class is now a subclass
// of the Animal class
// Dog.h
@interface Dog : Animal
@end
// Dog.m
@implementation Dog {
}
@end
|
Push members down
Before | After |
---|---|
// Animal.h
@interface Animal : NSObject
- (void)meow;
- (void)run;
- (void)eat;
@end
// Animal.m
@implementation Animal {
}
// This method will be moved
// to a subclass
- (void)meow{
NSLog(@"Meow!");
}
- (void)run {
NSLog(@"I am running");
}
- (void)eat {
NSLog(@"I am eating");
}
@end
|
// New subclass
// Cat.h
@interface Cat : Animal
- (void)meow;
@end
// Cat.m
@implementation Cat {
}
- (void)meow{
NSLog(@"Meow!");
}
@end
// The Animal class
// Animal.h
@interface Animal : NSObject
- (void)run;
- (void)eat;
@end
// Animal.m
@implementation Animal {
}
- (void)run {
NSLog(@"I am running");
}
- (void)eat {
NSLog(@"I am eating");
}
@end
|
Last modified: 26 August 2021