私はdecorator patternを使ってUIKitクラスの機能を拡張しようと考えていました。私が直面している問題は、私が他の言語で見た例から、パターンが私に装飾されたオブジェクトのインタフェースを複製させることです。ここで私は、パターン実装見るであろう方法は次のとおりです。だから、要約するObjective-Cのデコレータパターン
// Inheritance used for compile time check only
@interface UIScrollViewDecorator : UIScrollView
{
UIScrollview *decoratedScrollView;
}
- (id)initWithScrollView:(UISCrollView*)scrollView;
@end
@implementation UIScrollViewDecorator
- (id)initWithScrollView:(UISCrollView*)scrollView
{
self = [super init];
if(self != nil)
{
decoratedScrollView = [scrollView retain];
// maybe set up some custom controls on decoratedScrollView
}
}
// all methods for UIScrollView need to be manually passed to the decorated object
// -- this is the problem
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
{
// use "overwritten methods" to mess with the input for the decorated scroll view
// most of the time though I have no need to make any adjustments here; I still need
// to pass all these messages through so that outside users can interact with me just
// like with a real UIScrollView
[decoratedScrollView scrollRectToVisible:rect animated:animated];
}
@end
を、問題は、私はそこに何かを変更する必要がない場合でも、飾られたオブジェクトのメソッドの重複です。私が "上書き"する必要がないメソッドに沿って渡す簡単な方法はありますか?これにNSProxyのようなものを使用できますか?
EDIT:私の実際の問題を解決するためにデコレータパターンが必要ではないことがわかって以来、これは主に理論上の問題になっています。しかし、将来私は再びそれを使うようになるかもしれないので、あなたの答えにはまだ非常に興味があります。
私は、スウィージングがObjective-Cでデコレータを行う適切な方法だと思います。 –