Search code examples
objective-cioscomposition

How does class composition work in iOS?


Let's say I have property A on classA and property B on classB and I want classAB to have both properties A and B. I still don't understand how to make this all work with composition.

I realize this can be done with inheritance, but I want to learn how to do this with composition. I've looked at examples and I still don't get how it all works.


Solution

  • You make a new class, which has instances of classA and classB as member variables. Then you implement the properties by passing through the get/set methods.

    @interface ClassAB
    {
        ClassA *objectA;
        ClassB *objectB;
    }
    @property (nonatomic,strong) id propertyA;
    @property (nonatomic,strong) id propertyB;
    @end
    
    @implementation ClassAB
    - (id)propertyA { return objectA.propertyA; }
    - (void)setPropertyA:(id)value { objectA.propertyA = value; }
    - (id)propertyB { return objectB.propertyB; }
    - (void)setPropertyB:(id)value { objectB.propertyB = value; }
    @end
    

    And that's what composition is. Some languages have special syntax to do this (e.g., in Ruby you can include a set of methods from one class/module in another), but Objective-C doesn't allow this.

    One thing you can do in Objective-C is catch messages sent to your object which do not have an associated method, and forward them to another object. This trick is useful if you are writing a class that will pose as another class, or if there are many different messages to forward and you don't want to write them all out manually.

    The downside to using message forwarding is that you give up some control and it can be a harder to predict when a message will be handled by your class or the forwarding class. For example, if a superclass implements a method, that method will be executed and the forwarding code will not be called.