Adding own functionality into iOS library

62 views Asked by At

I am using a a library which has quite complicated inherited structure (it consists of a couple of classes, inherited e.g. UITableViewController, UIView, UITableViewCell and others, where some of the classes are used to create custom objects.

I need to add some functionality (to be precise, to implement tap gesture recognizers). The easy solutions is to put a few lines of code into some of the classes of the library.
Generally, I like to have my code separated from libraries code. Is it possible somehow "override" these classes without rewriting them, or to add some extension?

Or the only idea is to write overrides of all the classes generating own classes and tons of useless code?

Or simply add own code to the library?

Additions:
It seems, that categories are right direction, but not particularly, what I want. Here's what I want exactly:
I have a in Class1:

- (void)someMethod {
doThis;
}

And without subclassing // editing the class I would like to transform this to:

- (void)someMethod {
doThis;
andThisToo;
}    

Categories add other methods to a class, while I need to add some functionality in already existing method.

1

There are 1 answers

4
Daniel T. On

You need to use the decorator pattern.

The decorator pattern is used to extend or alter the functionality of objects at run- time by wrapping them in an object of a decorator class. This provides a flexible alternative to using inheritance to modify behaviour.

Since the example you give is pretty sketchy, I can't provide a code example, but check out Wikipedia (http://en.wikipedia.org/wiki/Decorator_pattern)

EDIT

I thought about what you gave and here would be an example of a decorator:

@interface Decorator
@property (strong, nonatomic) id decoratedObject;
- (void)someMethod;
@end

@implementation Decorator
- (void)someMethod {
    [self.decoratedObject doThis];
    [self andDoThisToo];
}

@end