This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What  are delegates Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "What  are delegates " Watch "What  are delegates " New topic
Author

What are delegates

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 973

what is delegates in core java


http://www.lifesbizzare.blogspot.com || OCJP:81%
Hariharan Ram Kumar
Greenhorn

Joined: Oct 19, 2009
Posts: 3
When an object receives a request, the object can either handle the request itself or pass the request on to a second object to do the work. If the object decides to pass the request on, you say that the object has forwarded responsibility for handling the request to the second object.

The following Stack class provides a simple example of composition and forwarding:

public class Stack {
private java.util.ArrayList list;

public Stack() {
list = new java.util.ArrayList();
}

public boolean empty() {
return list.isEmpty();
}

public Object peek() {
if( !empty() ) {
return list.get( 0 );
}
return null;
}

public Object pop() {
if( !empty() ) {
return list.remove( 0 );
}
return null;
}

public Object push( Object item ) {
list.add( 0, item );
return item;
}

}



Through composition, Stack holds on to an ArrayList instance. As you can see, Stack then forwards the requests to the ArrayList instance. Simple composition and request forwarding (such as that of the Stack class presented above) is often mistakenly referred to as delegation.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 973

private java.util.ArrayList list;

is this statement right i mean i have usually seen while importing ArrayList??
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

That's legal Java syntax, Vishal.

You can either import for example java.util.ArrayList and then use the simple name 'ArrayList', or not import it and use the fully qualified name 'java.util.ArrayList'.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: What are delegates
 
Similar Threads
What is delegates ? could you explain with small program?
RequestProcessor class?
What is the design pattern of transition delegates?
post form data when slection changed in list
Can someone tell me if I disected the JTree UIDelegate paint() method right?