Delegation in Java

We all know that reusability is one of the special feathers of every OOPs language including Java. We can achieve reusability by reusing the same class multiple times, which can be accomplished in three ways in Java.

The first one is quite straightforward: we simply have to create object of the existing class inside another new class. This method is called composition, because of the composition of object of the existing class.

The second one is better. Here we create a new class as a type of an existing class. Actually, we will take the form of the existing class and add some code without modifying the existing class. This method is known as inheritance, which is one of the important feathers of Java.

On the third one, we have Delegation, which is not directly supported by Java, so not much mentioned anywhere. We can say it as the mixture of above two. Because we place a member object in the class we are building, as we did in composition but at the same time we expose all the methods from the member object in the new class, just like Inheritance.

Here we have a class SpaceShipControl.java, which can be expanded on another class by two ways:

//SpaceShipControl.java
class SpaceShipControl
{
    void up(int velocity){}
    void down(int velocity){}
    void left (int velocity){}
    void right(int velocity){}
    void forward(int velocity){}
    void backward(int velocity){}
    void turboboost(int velocity){}
}

Here we are extending the SpaceShipControl.java to build SpaceShip.java. Here all the method has been extended to the child class and as well as it is expanded.

class SpaceShip extends SpaceShipControl
{
    private String name;
    public SpaceShip(String name)
    {
        this.name = name;
    }
    public String toString()
    {
        return name;
    }
    public static void main(String[] args) {
        SpaceShip protector = new SpaceShip("NSEA Protector");
        protector.forward(100);
    }
}

In some cases Inheritance is not a good tool to used, this point I will try to describe in my next post. Any way in such case we will use delegation. Here the above inheritance can be achieved from delegation too.

// SpaceShipDelegation.java
public class SpaceShipDelegation
{
    private String name;
    private SpaceShipControl controls = new SpaceShipControl();
    public SpaceShipDelegation(String name)
    {
        this.name=name;
    }
    //Delegated methods
    void up(int velocity)
    {
        controls.up(velocity);
    }
    void down(int velocity)
    {
        controls.down(velocity);
    }
    void left (int velocity)
    {
        controls.left(velocity);
    }
    void right(int velocity)
    {
        controls.right(velocity);
    }
    void forward(int velocity)
    {
        controls.forward(velocity);
    }
    public void backward(int velocity)
    {
        controls.backward(velocity);
    }
    void turboboost(int velocity)
    {
        controls.turboboost(velocity);
    }
}

Here we can see that all the methods are referred to control object, and the interface is working as inheritance. However we can add more control with delegation.

Actually Java language doesn’t support delegation, but sometimes it is useful than inheritance so development tools often use it.

Ref: Thinking in Java 4th Ed

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: