What is delegation?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: What is delegation?

  1. #1
    Join Date
    Mar 2004
    Location
    malaysia
    Posts
    150

    What is delegation?

    What is delegation? Could give an example in coding. The following is i found on java glossary.

    delegation
    An act whereby one principal authorizes another principal to use its identity or privileges with some restrictions.

  2. #2
    Join Date
    Jul 2005
    Location
    Antwerp - Belgium
    Posts
    29

    example for delegation

    A good example is found at
    Javaworld

    Look at real life : when your boss gives you a job, you can do it yourself or you can give it to someone else, so delegate it.
    It's the same in the next example (found at Javaworld and in the book of Deitel on Java). Here's a piece of the code :

    Code:
    public class Stack {
    
        private java.util.ArrayList list;
        
        public Stack() {
            list = new java.util.ArrayList();
        }
        
        public boolean empty() {
            return list.isEmpty();
        }
    You're implementing a Stack but this Stack contains an ArrayList.
    If you create a Stack, in fact this Stack asks an ArrayList to be created. If you ask the Stack if it is empty, the Stack asks the ArrayList if it's empty.
    So, the work is done by ArrayList after this work is delegated by Stack.

  3. #3
    Join Date
    Mar 2004
    Location
    malaysia
    Posts
    150

    Simple composition??

    What is the Simple composition means?

    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.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The idea of delegation is the basis of event-based programming. It allows you to separate logic from presentation. A user clicks his mouse pointer on a component in your GUI, or your user types some text into a TextField, or some other action occurs. That component generates a MouseEvent or a TextEvent or an ActionEvent. That event is dispatched to a listener - you have told the component who to send the event to in your code. The listener knows what to do if an event it is listening for arrives, and does its work. The component does not execute the code - it delegates the handling of the event to the listener.

  5. #5
    Join Date
    Jul 2005
    Location
    Antwerp - Belgium
    Posts
    29

    delegation or not

    Simple composition and request forwarding (such as that of the Stack class presented above) is often mistakenly referred to as delegation.
    Mind you, these examples are found at a website and in a book. It's by no means a self invented example.
    It's a bit disappointing if you hear you cannot trust any longer the sources on Java you were sure of.

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Javaworld is just people like us writing articles.

    Yes, in a sense, the work performed by a contained class is a "delegation" of work - using commonly used connotation of the English word - but is it what the "language masters" refer to as "delegation"? It's not for me to say.

    I've never read nor used any of Deitel's books but I have read many reviews which trash his (what the reviewers call) lack of technical knowledge.

Similar Threads

  1. Replies: 1
    Last Post: 04-19-2002, 12:15 PM
  2. Implements a Fully Functional Class in VB?
    By Alex L in forum VB Classic
    Replies: 8
    Last Post: 09-15-2000, 02:13 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center