Results 1 to 4 of 4

Thread: Basic concept of delegates in java

  1. #1
    Join Date
    May 2008
    Posts
    72

    Basic concept of delegates in java

    Hi Guys,

    I have sound knowledge about java programming. But the one concept of java I am not able to understand i.e delegates. Is delegated in java is similar as compare to C#?

    I read lots of book for this topics but it was too complicated.
    Can anybody able to sort-out this problem?...

  2. #2
    Join Date
    May 2008
    Posts
    2,302

    Re: Basic concept of delegates in java

    When an object receives a request, the object can handle the request itself or forward the request to next object . If the object forward the request to next object, It means that the object has passed responsibility for handling the request to the another object.


    See the below example of stack composition:


    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;
    }

    }
    In above example ,Stack holds on to an instance of ArrayList . Stack then passes the requests to the ArrayList instance. Simple composition and request forwarding is called as delegation.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,394

    Re: Basic concept of delegates in java

    Hi Friends
    I am also have difficulties to understand the delegates concept in the java. I think this is because it is very complicated.
    But the only thing which I know is basic concepts of delegate in java and C# is same, but the way of implementing delegates is different. If you know more than this please let me know.

  4. #4
    Join Date
    May 2008
    Posts
    2,393

    Re: Basic concept of delegates in java

    In JAVA , delegate is like to a listener . A delegate is one class which is referenced by another class. A class that has a delegate will call methods on the delegate to identify how and when to do certain task. see following example:

    - (void)tableView: (NSTableView *)tv shouldSelectRow: (int)row
    - (void)tableView: (NSTableView *)tv shouldSelectTableColumn: (NSTableColumn *)tc
    - (BOOL)tableView: (NSTableView *)tv shouldEditTableColumn: (NStableColumn *)tc row: (int)row
    - (void)tableViewSelectionDidChange: (NSNotification *)notification
    Last edited by opaper; 17-11-2009 at 08:14 AM.

Similar Threads

  1. Basic Java question
    By colby02184 in forum Software Development
    Replies: 2
    Last Post: 11-11-2011, 04:08 PM
  2. Use of delegates in C sharp
    By opaper in forum Software Development
    Replies: 5
    Last Post: 25-01-2010, 12:44 PM
  3. Concept of Scrollpane Container in Java
    By Adrina_g in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 08:23 AM
  4. Concept of java beans
    By Jabeen in forum Software Development
    Replies: 4
    Last Post: 01-12-2009, 09:59 AM
  5. threads concept in java
    By vijji191 in forum Software Development
    Replies: 2
    Last Post: 30-11-2009, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •