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.
What is Composition? Composition comes down to combining multiple parts to create a new outcome. You can see an app using numerous frameworks as an outcome of composing frameworks together. We can define composition as an instance providing some or all of its functionality by using another object.
Composition is one of the fundamental concepts in object-oriented programming. It describes a class that references one or more objects of other classes in instance variables. This allows you to model a has-a association between objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With