Imagine that you have two objects: Car and Motorboat, which implement the CanDrive and CanFloat interfaces, respectively. Now you want to have a third object that implements both of these interfaces and that reuses the logic from Car and Motorboat. In languages like Groovy, Ruby, and Scala, you can solve this using mixin. In Java, however, there is no such thing. Of course, you can use, for example. An adapter design pattern, but in many cases (especially when creating frameworks) a dynamic proxy will come in handy. Consider an example using the cglib library:
CanDrive car = new Car(); CanFloat motorboat = new Motorboat(); net.sf.cglib.proxy.Mixin amphibian = net.sf.cglib.proxy.Mixin.create(new Object[] { car, motorboat }); TestCase.assertEquals("bzzz bzzz bzzz ...", ((CanFloat) amphibian)._float()); TestCase.assertEquals("pyr pyr pyr pyr ...", ((CanDrive) amphibian).drive());
rarry source share