mercredi 10 mai 2017

Jumping in the next Level of Decorator Pattern in Java

I have been observing many of the examples put by the community but all stack at the same point. They pass a single type of variable. in most of the cases its a price of a product. My problem is how can I pass a single variable that modifies the others.

Suppose the following example:

public abstract class Beverage {
    String description = "Unknown Beverage";

    public String getDescription() {
        return description;
    }

    public abstract double cost();
}


public abstract class CondimentDecorator extends Beverage {
    public abstract String getDescription();

}




public class DarkRoast extends Beverage {
    public DarkRoast() {
        description = "Dark Roast Coffee";
    }

    public double cost() {
        return .99;
    }
}



public class Decaf extends Beverage {
    public Decaf() {
        description = "Decaf coffee";
    }

    public double cost() {
        return 1.05;
    }
}



public class Expresso extends Beverage {

    public Expresso() {
        description = "Expresso";
    }

    public double cost() {
        return 1.99;
    }
}


public class HouseBlend extends Beverage {

    public HouseBlend() {
        description = "House Blend Coffee";
    }

    public double cost() {
        return .89;
    }
}


public class Mocha extends CondimentDecorator {
    Beverage beverage;

    public Mocha(Beverage beverage) {
        this.beverage = beverage;
    }

    public String getDescription() {
        return beverage.getDescription() +", Mocha";
    }

    public double cost() {
        return .20 + beverage.cost(); 
    }
}



public class Soy extends CondimentDecorator {
    Beverage beverage;

    public Soy(Beverage beverage) {
        this.beverage = beverage; 
    }

    public String getDescription() {
        return beverage.getDescription() + ", Soy"; 
    }

    public double cost() {
        return .15 + beverage.cost();
    }

}



public class StarbuzzCoffee {

    public static void main(String args[]) {
        Beverage beverage = new Expresso();
        System.out.println(beverage.getDescription()
                   + "$" + beverage.cost());

        Beverage beverage2 =new DarkRoast();
        beverage2 = new Mocha( beverage2 );
        beverage2 = new Mocha( beverage2 );
        beverage2 = new Whip( beverage2 );
        System.out.println(beverage2.getDescription()
                   + "$" + beverage2.cost());

        Beverage beverage3 = new HouseBlend();
        beverage3 = new Soy( beverage3 );
        beverage3 = new Mocha( beverage3 );
        beverage3 = new Whip( beverage3 );
        System.out.println(beverage3.getDescription()
                   + "$" + beverage3.cost());
    }
}



public class SteamedMilk extends CondimentDecorator {
    Beverage beverage;

    public SteamedMilk(Beverage beverage) {
        this.beverage = beverage;
    }

    public String getDescription() {
        return beverage.getDescription() + ", SteamedMilk";
    }

    public double cost() {
        return .10 + beverage.cost(); 
    }

}

public class Whip extends CondimentDecorator {
    Beverage beverage;

    public Whip(Beverage beverage) {
        this.beverage = beverage;
    }

    public String getDescription() {
        return beverage.getDescription() +", Whip";
    }

    public double cost() {
        return .10 + beverage.cost();
    }

}

As long I understand the example. And I know how it works. But I get stuck when I try to create a variable ("Lets say you can modify the size of the drink"). which modefies the price of the condiments based on what size you pick. For more clear.

I want to understand, if you create 2 methods (setSize() and getSize()) that returns 3 different types of variables (lets say, small, medium and large) how they will modify the price of the condiments depending on the size you pick.

Thanks, I will apreciate any help.

Aucun commentaire:

Enregistrer un commentaire