上拉重构,Objective-C [英] Pull-up refactoring, Objective-C
问题描述
我有两个相似的类, MultiSlotBlock
和 SingleSlotBlock
。他们已经开始共享许多通用代码,因此我决定进行一些重构,并将一些方法提升到新的超类中,我们将其称为 Block
。
现在,我举起来的一种方法(为示例简化)看起来像这样:
//(Block.mm)
-(void)doACommonBehaviour
{
// ..做一些事情
[self doAUniqueBehaviour];
}
这里的问题是 [self doAUniqueBehaviour]
正在显示警告,因为我的超类当然没有在任何地方实现此方法。
我认为这两个解决方案听起来并不好我。一种是像这样使用协议(我目前正在使用的方式):
//(Block.mm)
-(void)doACommonBehaviour
{
// ..如果([selfconformsToProtocol:@protocol(UniqueBehaviourProtocol)])有东西
b $ b {
id< UniqueBehaviourProtocol>块=(id< UniqueBehaviourProtocol>)自身;
[block doAUniqueBehaviour];
}
}
另一种方法是在我的方法主体中留空超类(在这种情况下会很多),只返回 doesNotRespondToSelector
。
后面有些东西刺痛我的想法是我应该使用策略模式,但我可能还遥遥无期,而且我还没有考虑过如何实现该策略。
有什么想法吗?谢谢。
编辑:我知道 doAUniqueBehaviour
将在所有子类中实现的事实,实施会有所不同。
超类不应该知道其子类。您应该在每个子类中实现
-(void)doACommonBehaviour
方法:
-(void)doACommonBehaviour
{
[super doACommonBehaviour];
[self doAUniqueBehaviour];
}
编辑-澄清:
如果所有子类都将实现 -doAUniqueBehaviour
,则应在超类中实现(甚至为空),并且每个子类都会根据需要覆盖它。
如果subclass1实现 -doAUniqueBehaviour1
,subclass2实现 -doAUniqueBehaviour2
等然后按照我上面的建议做;例如。在子类1中:
-(void)doACommonBehaviour
{
[super doACommonBehaviour];
[self doAUniqueBehaviour1];
}
I have two similar classes, MultiSlotBlock
and SingleSlotBlock
. They have started to share a lot of common code so I have decided to do some refactoring and pull some of the methods up to a new superclass, let's call it Block
.
Now one of the methods that I pull up, simplified for the example, looks like this:
// (Block.mm)
- (void)doACommonBehaviour
{
// .. does some stuff
[self doAUniqueBehaviour];
}
The problem here is that [self doAUniqueBehaviour]
is showing a warning because of course my superclass doesn't implement this method anywhere.
The two solutions I thought of don't sound great to me. One is to use a protocol (the way I am currently doing it) like so:
// (Block.mm)
- (void)doACommonBehaviour
{
// .. does some stuff
if ([self conformsToProtocol:@protocol(UniqueBehaviourProtocol)])
{
id<UniqueBehaviourProtocol> block = (id<UniqueBehaviourProtocol>)self;
[block doAUniqueBehaviour];
}
}
The other is to have a blank method body in my superclass (in this case there would be a lot) and just return doesNotRespondToSelector
.
Something is tingling at the back of my mind that I should be using the Strategy Pattern, but I might be way off, and I haven't thought through how that would be implemented.
Any ideas? Thanks.
EDIT: I know for a fact that doAUniqueBehaviour
will be implemented in all subclasses, it is just the implementation that will differ.
The superclass should not know about its subclasses. You should implement the
- (void)doACommonBehaviour
method in every subclass and there:
- (void)doACommonBehaviour
{
[super doACommonBehaviour];
[self doAUniqueBehaviour];
}
EDIT - clarification:
If all the subclasses are going to implement -doAUniqueBehaviour
then it should be implemented in the superclass (even empty) and each subclass will override it to its needs.
If subclass1 implements -doAUniqueBehaviour1
, subclass2 implements -doAUniqueBehaviour2
etc then do what I propose above; eg. in subclass1:
- (void)doACommonBehaviour
{
[super doACommonBehaviour];
[self doAUniqueBehaviour1];
}
这篇关于上拉重构,Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!