This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Decorator Pattern. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Decorator Pattern." Watch "Decorator Pattern." New topic
Author

Decorator Pattern.

Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
Hi All

This is Decorator Pattern Example In which Object wrapping takes Place.
I just want to know that what actually is happening in last 3 lines.I know
Object Wrapping is being done here but please expalin me that what mechanism
is working here.
please elaborate ?


Beverages beverage1 = new DarkRoast();

beverage1 = new Mocha(beverage1);
beverage1 = new Mocha(beverage1);
beverage1 = new Whip(beverage1);
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Punya Pratap Singh wrote: Hi All

Beverages beverage1 = new DarkRoast();

beverage1 = new Mocha(beverage1);
beverage1 = new Mocha(beverage1);
beverage1 = new Whip(beverage1);


In the above example- You are adding- Mocha to a DarkRoast beverage and then is Whipped with cream or something. So you see we are adding more to the beverage.

Edit: I was explaining it in a real like situation. If you could provide us the class definitions then we can as well explain with from the Code perspective.


Mohamed Sanaulla | My Blog
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80


Thanks

I am agree with that,actually I want to know that if you are returning a address to a abstract class 4 times so,how does it does wrapping of those objects.here Beverages is a abstract class.
as you can see from code DarkRoast extends Beverages and also Mocha nad Whip extends Beverages.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Punya Pratap Singh wrote:as you can see from code DarkRoast extends Beverages and also Mocha nad Whip extends Beverages.


where is the code?

Punya Pratap Singh wrote:I am agree with that,actually I want to know that if you are returning a address to a abstract class 4 times so,how does it does wrapping of those objects.here Beverages is a abstract class.

You are not creating an instance of an Abstract Class. Instead you are creating an instance of a concrete class (which I suppose they are). But you are assigning the objects to a Abstract class type. This way you can assign objects of all subtypes of Beverage to the reference which is declared as Beverage type.
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
Thanks..

I do know that I am not creating Instance of abstarct class because It is not possible. Its simply a assignment,but I am asking that the same instance
of abstact class is storing 4 addresses,In what way this wrapping is being done?what is the internal mechanism ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Punya Pratap Singh wrote:Thanks..

I do know that I am not creating Instance of abstarct class because It is not possible. Its simply a assignment,but I am asking that the same instance
of abstact class is storing 4 addresses,In what way this wrapping is being done?what is the internal mechanism ?


If you could show us the code we can explain what's happening.
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
public class DarkRoast extends Beverages{
public DarkRoast(){

description = "Dark Roast";

}
public double cost() {

return 122;
}

}



public class Mocha extends CondimentsDecorator{
Beverages beverage;

public Mocha(Beverages beverage){

this.beverage = beverage;
}

public String getDescription() {

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


public double cost() {

return 20 + beverage.cost();
}

}


public class Whip extends CondimentsDecorator{
Beverages beverage;

public Whip(Beverages beverage){
this.beverage = beverage;
}
public String getDescription() {

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


public double cost() {

return 33+beverage.cost();
}

}



public abstract class CondimentsDecorator extends Beverages{

public abstract String getDescription();

}



public abstract class Beverages {

String description ="unknown Beverage";

public String getDescription(){
return description;


}
public abstract double cost();

}




public class TestDecorator {

public static void main(String [] args){


//order is - DarkRoast with double Mocha,Whip
//make a DarkRoast ,wrap it with Mocha.again Wrap it with mocha,again Wrap it with Whip.
Beverages beverage1 = new DarkRoast();

beverage1 = new Mocha(beverage1);
beverage1 = new Mocha(beverage1);
beverage1 = new Whip(beverage1);


System.out.println("Description "+beverage1.getDescription() + " Cost :" +beverage1.cost() );
}
}



this is the code for this pattern..It Prints things very Fine,But I am confused with mechanism..please Help ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

You can see that they are keeping the reference to the original Beverage and then just adding more to it- Increasing the Price, Changing the Description.
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
Thanks Mohamed,

But I wanted it In step wise Mechanism,what actually is happening internally.

Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
public class Test{
public static void main(String[] args) {
Foo foo1 = new Foo(1);
Foo foo2 = new Foo(2);
System.out.print(foo1.equals(foo2));
}
}
class Foo {
Integer code;
Foo(Integer c) {
code = c;
}
public boolean equals(Foo f) {
return false;
}
public boolean equals(Object f) {
return true;
}
}

Its Returning False,Why ? why didn't it go to overridden version of equals(Object).
and when we remove Foo parameter version of equals ,In that case it prints true by executing overridden version of equals(Object)

Please Explain ?
Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Punya, please UseCodeTags next time.

Punya Pratap Singh wrote:Its Returning False,Why ? why didn't it go to overridden version of equals(Object).
and when we remove Foo parameter version of equals ,In that case it prints true by executing overridden version of equals(Object)

The overloaded equals(Foo) method is more specific than the overridden equals(Object) method, and therefore that will be called if you call foo1.equals(foo2). By removing the Foo parameter, there is only one equals method that matches - the overridden equals(Object) method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
Thanks Rob.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Decorator Pattern.
 
Similar Threads
Proxy Vs Decorative pattern
Building a Decorator Question
Decorator Pattern Not Clear
Removing Decorator From The Decorated Object
HeadFirst Design Patterns - Decorator Chapter