Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Cloudizza

Cloudizza

Decorator Design Pattern (Wrapper)

DecoratorPattern.jpeg

DrinkDecorator

public abstract class DrinkDecorator extends Drink {
public Drink drink;

}


Drink


public abstract class Drink { 
    String description;


    int cost;



    abstract int getCost();



    String getDesc() {

        return this.description;


    }


}



Vodka



public class Vodka extends Drink {

    String description = " Vodka ";



    int cost = 100;



    @Override

    int getCost() {


        return this.cost;


    }


}




OrangeJuice & like wise LemonJuice


public class OrangeJuice extends DrinkDecorator {

    String description = " fresh orenge juice ";



    int cost = 5;



    public OrangeJuice(Drink myVodka) {

        this.drink = myVodka;


    }



    @Override

    int getCost() {


        return this.cost + this.drink.getCost();


    }


}



Client Code



//Make Vodka with orange



        //make vodka

        Drink myVodka = new Vodka();


        //mix orenge and vodka


        DrinkDecorator orengeVodka= new OrangeJuice(myVodka);


        //mix lime juie in the orengeVodka


        DrinkDecorator LimeOrengeVodka= new LimeJuice(orengeVodka);


        //the cost of the final vodka should be that of lime


        //orenge and vodka cost


        System.out.println(LimeOrengeVodka.getCost());

No comments:

Post a Comment

Search 24 Bytes