We help IT Professionals succeed at work.
Get Started

What is the use of Forwarding Class in java

Rohit Bajaj
Rohit Bajaj asked
on
520 Views
Last Modified: 2017-03-28
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
Comment
Watch Question
IT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE