Thinking in Java Fourth Edition Bruce Eckel Chapter 6 Exercise 11

Exercise 11: Modify Detergent.java so that it uses delegation.

class Cleanser
{
    private String s = "Cleanser";
    public void append(String a)
    {
        s += a;
    }
    public void dilute()
    {
        append(" dilute()");
    }
    public void apply()
    {
        append(" apply()");
    }
    public void scrub()
    {
        append(" scrub()");
    }
    public String toString()
    {
        return s;
    }
    public static void main(String[] args)
    {
        Cleanser x = new Cleanser();
        System.out.println(x);
    }
}

public class DetergentDelegation
{
    private String s = "DetergentDelegation";
    Cleanser c = new Cleanser();
    public void append(String a)
    {
        s += a;
    }
    // two methods delegated entirely to Cleanser c:
    public void dilute()
    {
        c.dilute();
    }
    public void apply()
    {
        c.apply();
    }
    // method delegated in part to Cleanser c:
    public void scrub()
    {
        append(" DetergentDelegation.scrub()");
        c.scrub();
    }
    public void foam()
    {
        append(" foam()");
    }
    public String toString()
    {
        return s + " " + c;
    }  
    public static void main(String[] args)
    {
        DetergentDelegation x = new DetergentDelegation();
        x.dilute();
        x.apply();
        x.scrub();
        x.foam();
        System.out.println(x);
        System.out.println("Testing base class:");
        Cleanser.main(args);
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *