Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on 

What is the use of Forwarding Class in java

HI,
In Item16: Favor composition over inheritance  in Joshua Bloch her mentions an example like :
public class InstrumentedSet<E> extends ForwardingSet<E> {
    private int addCount = 0;
    public InstrumentedSet(Set<E> s) {
        super(s);
}
    @Override public boolean add(E e) {
        addCount++;
        return super.add(e);
    }
@Override public boolean addAll(Collection<? extends E> c) { addCount += c.size();
return super.addAll(c);
    }
    public int getAddCount() {
        return addCount;
    }
}
// Reusable forwarding class
public class ForwardingSet<E> implements Set<E> {
    private final Set<E> s;
    public ForwardingSet(Set<E> s) { this.s = s; }
    public void clear()               { s.clear();            }
    public boolean contains(Object o) { return s.contains(o); }
public boolean isEmpty()
public int size()
public Iterator<E> iterator()
public boolean add(E e)
public boolean remove(Object o)
public boolean containsAll(Collection<?> c)
                                   { return s.containsAll(c); }
    public boolean addAll(Collection<? extends E> c)
                                   { return s.addAll(c);      }
    public boolean removeAll(Collection<?> c)
                                   { return s.removeAll(c);   }
    public boolean retainAll(Collection<?> c)
                                   { return s.retainAll(c);   }
    public Object[] toArray()          { return s.toArray();  }
    public <T> T[] toArray(T[] a)      { return s.toArray(a); }
    @Override public boolean equals(Object o)
                                       { return s.equals(o);  }
    @Override public int hashCode()    { return s.hashCode(); }
    @Override public String toString() { return s.toString(); }
}

Open in new window



I could have achieved the same using the following class :
public class InstrumentedSet<E>  {
    private Set set;
    private int addCount = 0;
    public InstrumentedSet(Set<E> s) {
        set = s;
}
  public boolean add(E e) {
        addCount++;
       return  set.add(e);
    }
public boolean addAll(Collection<? extends E> c) { 
addCount += c.size();
return set.addAll(c);
    }
    public int getAddCount() {
        return addCount;
    }
}

Open in new window


I mean instead of using inheritance i could have still used composition by including the Set object inside the class directly...
Rather than implementing forwarding class etc...
What are the pros and cons of each of these approach ?

Thanks
Java

Avatar of undefined
Last Comment
mccarl
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo